GPSの研究 その11


概要

GPSを理解したかった。
NMEAをパースして、KMLを作って見た。

写真

サンプルコード

import micropyGPS
from lxml import etree
from pykml.factory import KML_ElementMaker as KML
from pykml.factory import GX_ElementMaker as GX

gps = micropyGPS.MicropyGPS(9, 'dd')
f = open("test10.nmea")
pm = ""
while True:
    line = f.readline()
    if not line:
        break
    if (line[0 : 6] == "$GPGGA"):
        for x in line:
            gps.update(x)
        name = "{}:{}".format(gps.timestamp[1], gps.timestamp[2])
        lat, lon = gps.latitude[0], gps.longitude[0]
        alt = gps.altitude
        pm += "{},{},{} ".format(lon, lat, alt)

doc = KML.kml(
    KML.Placemark(
        KML.name("nmea"),
        KML.LineString(
            KML.extrude(1),
            GX.altitudeMode("relativeToSeaFloor"),
            KML.coordinates(
                pm
            )
        )
    )
)
print (etree.tostring(doc, pretty_print = True).decode())
f = open('micro6.kml', 'w')
f.write(etree.tostring(doc, pretty_print = True).decode())
f.close()

以上。