毕业论文

打赏
当前位置: 毕业论文 > 外文文献翻译 >

树莓π与GPS接收器英文文献和中文翻译(2)

时间:2019-03-31 20:39来源:毕业论文
a format we can use in a log file. Although the generic client, cgps, that we used here is useful for viewing coordinates, unfortunately its really difficult to get usable information from it. For thi


a format we can use in a log file. Although the generic client, cgps, that we used here is useful for viewing coordinates,
unfortunately it’s really difficult to get usable information from it. For this reason, we’ll use the Python gps module to
interact with the receiver.
Setting Up a Log File
When we get the stream from the GPS, we need to have a place to store it for later use, as it won’t do us much good if
we’re just printing it to a (nonconnected) screen during the flight. What we can do is set up a log file, using Python’s
logging module, and then when the Pi is back on the ground, we can parse the file and put it into a format we can use
in Google Earth.
Setting up the log file is very simple. Start by typing
import logging
logging.basicConfig(filename='locations.log', level=logging.DEBUG, format='%(message)s')
These two lines import the module, declare the log’s file name and what gets logged, and give the format of each
line. We’ll save each GPS call in three strings: the longitude, latitude, and altitude—the three coordinates used by
Google Earth. (They’re actually saved as floats, not strings, which means that we’ll have to convert them to strings
when we write them to the log file.) To write a line to the log file, the format is simply this:
logging.info("logged message or string or what-have-you")
It is not necessary to use the newline (\n) character, because each time you call the logging.info() function, it
begins on a new line.
In case you’re wondering, yes, we could simply write the GPS data to a regular file, but logging is an important,
useful concept that many programmers either don’t fully understand or skip over completely. In the case of Python’s
logging module, you can set log entries to be entered depending on the severity of the event being tracked. There are
five severities possible: DEBUG, INFO, WARNING, ERROR and CRITICAL.
To see the logging module in action, type python to start up a Python prompt and enter the following:
>>> import logging
>>> logging.warning("I am a warning.")
>>> logging.info("I am an info.")
The second line will output WARNING:root:I am a warning, while the third line, classified as an INFO level, will
not be output to the console. If, on the other hand, you enter
>>> logging.basicConfig(level=logging.DEBUG)
it sets the default level to DEBUG, meaning that every event will be logged or output, regardless of severity. Entering
the filename and format, as we did earlier, sets the log file and how events are written to it.
Formatting a KML File
A KML file is a special kind of XML (eXtensible Markup Language) used by Google Earth to delineate landmarks,
objects, and even paths. It looks similar to an HTML file, with opening and closing < > tags for different levels of
information, such as <Document> and </Document>, and <coordinates> and </coordinates>. Once we’ve got the log
file from the GPS, we need to format the included coordinates in a KML file that Google Earth can recognize. Luckily,
this is very easy, since we formatted the log file to just have longitude, latitude, and altitude, separated by
spaces—with format='%(message)s' and the logging.info() line. Now we can parse each line in the log file,
separate it by spaces with string.split, and write it into a preformatted .kml file. By using the write() function,
we can write each line to the new file, called 'kml' in the script, like so:
kml.write('<Document>blah blah blah</Document>\n')
Since we know how the final KML file needs to look for Google Earth to use it, we can actually write the program 树莓π与GPS接收器英文文献和中文翻译(2):http://www.youerw.com/fanyi/lunwen_31595.html
------分隔线----------------------------
推荐内容