|
| 1 | +#!/usr/bin/env python |
| 2 | +"""convert logged positions to KML""" |
| 3 | +import numpy as np |
| 4 | +from simplekml import Kml |
| 5 | + |
| 6 | +def makekml(t, lonLatAlt, ofn): |
| 7 | + """ |
| 8 | + write KML track/positions |
| 9 | +
|
| 10 | + t: vector of times |
| 11 | + lonLatAlt: longitude, latitude, altitude or just lon,lat |
| 12 | + ofn: KML filename to create |
| 13 | + """ |
| 14 | + lonLatAlt = np.asarray(lonLatAlt) |
| 15 | + assert lonLatAlt.ndim==2 and lonLatAlt.shape[1] in (2,3), 'Expect Nx2 or Nx3 array' |
| 16 | + |
| 17 | + kml = Kml(name='My Kml',open=1) |
| 18 | + |
| 19 | + if t is not None: # track over time |
| 20 | + trk = kml.newgxtrack(name='My Track') |
| 21 | + trk.newwhen(t) |
| 22 | + trk.newgxcoord(lonLatAlt.tolist()) #list of lon,lat,alt, NOT ndarray! |
| 23 | + else: # just a bunch of points |
| 24 | + for i,p in enumerate(lonLatAlt): # iterate over rows |
| 25 | + kml.newpoint(name=str(i), coords=[p]) |
| 26 | + |
| 27 | + print('writing',ofn) |
| 28 | + kml.save(ofn) |
| 29 | + |
| 30 | + |
| 31 | +if __name__ == '__main__': |
| 32 | + from argparse import ArgumentParser |
| 33 | + p = ArgumentParser() |
| 34 | + p.add_argument('logfn',help='csv logfile to read') |
| 35 | + p.add_argument('kmlfn',help='kml filename to write') |
| 36 | + p = p.parse_args() |
| 37 | + |
| 38 | + dat = np.fliplr(np.loadtxt(p.logfn, usecols=(1,2))) |
| 39 | + |
| 40 | + makekml(None,dat,p.kmlfn) |
0 commit comments