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
上一篇:AA2024-T3可成形性金属板英文文献和中文翻译
下一篇:同时定位和地图构建英文文献和中文翻译

性别影响幼儿与同伴冲突英文文献

会计师事务所任期与审计...

ADO.NET结构与概述英文文献和中文翻译

信息系统开发与数据库开...

机器人学入门力学与控制英文文献和中文翻译

园林植物景观的园林绿化...

船舶与海洋结构物水动力...

浅谈高校行政管理人员的...

从政策角度谈黑龙江對俄...

压疮高危人群的标准化中...

提高教育质量,构建大學生...

AES算法GPU协处理下分组加...

上海居民的社会参与研究

基于Joomla平台的计算机学院网站设计与开发

STC89C52单片机NRF24L01的无线病房呼叫系统设计

酵母菌发酵生产天然香料...

浅论职工思想政治工作茬...