def action(dependencies, targets): """ :param dependencies: list of files to process :param targets: list of file to output :return: """ kml = Kml() ship = kml.newdocument(name='ship') camera = kml.newdocument(name='camera') inputs = list(dependencies) inputs.sort() for dep in inputs: log = pd.read_csv(dep, index_col='timestamp', parse_dates=['timestamp']) log = log.resample('10S').first() log['Time'] = log.index log['Depth'] = 0 item = log.iloc[0] track = ship.newgxtrack( name=f'operation {int(item.Operation):03d}', altitudemode=AltitudeMode.clamptoground, description=f'Site {item.Site}') track.stylemap.normalstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' track.stylemap.normalstyle.linestyle.color = Color.green track.stylemap.normalstyle.linestyle.width = 6 track.stylemap.highlightstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' track.stylemap.highlightstyle.iconstyle.scale = 1.2 track.stylemap.highlightstyle.linestyle.color = '99ffac59' track.stylemap.highlightstyle.linestyle.width = 8 track.newgxcoord( list( zip(log.ShipLongitude.interpolate().values, log.ShipLatitude.interpolate().values, log.Depth.interpolate()))) track.newwhen( list(log.Time.dt.strftime('%Y-%m-%dT%H:%M:%S').values)) track = camera.newgxtrack( name=f'operation {int(item.Operation):03d}', altitudemode=AltitudeMode.relativetoground, description=f'Site {item.Site}') track.newgxcoord( list( zip(log.UsblLongitude.interpolate(), log.UsblLatitude.interpolate(), -log.HSVPressure))) track.newwhen( list(log.Time.dt.strftime('%Y-%m-%dT%H:%M:%S').values)) track.stylemap.normalstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' track.stylemap.normalstyle.linestyle.color = Color.red track.stylemap.normalstyle.linestyle.width = 6 track.stylemap.highlightstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' track.stylemap.highlightstyle.iconstyle.scale = 1.2 track.stylemap.highlightstyle.linestyle.color = '99ffac59' track.stylemap.highlightstyle.linestyle.width = 8 kml.save(list(targets)[0])
def create_kml(start_dt, traces): mintime = datetime(2100,1,1) maxtime = datetime(1901,1,1) bbox_nw = (sys.maxint, sys.maxint) bbox_se = (-sys.maxint, -sys.maxint) kml = Kml(name="Tracer") doc = kml.newdocument() fol = doc.newfolder(name="Traces") i = 0 for id_, trace in traces: trace = list(trace) trk = fol.newgxtrack(name='Trace id: %s' % id_) times = [start_dt + timedelta(seconds=int(p['time'])) for p in trace] trk.newwhen([format_date(t) for t in times]) places = [ (float(p['lon']), float(p['lat']), 0) for p in trace ] trk.newgxcoord(places) m = min(places, key=operator.itemgetter(0)) if m[0] < bbox_nw[0] and not (m[0] == 0.0 or m[1] == 0.0): bbox_nw = m[:2] m = max(places, key=operator.itemgetter(0)) if m[0] > bbox_se[0] and not (m[0] == 0.0 or m[1] == 0.0): bbox_se = m[:2] mintime = min([mintime] + times) maxtime = max([maxtime] + times) trk.altitudemode = 'relativeToGround' trk.stylemap.normalstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' trk.stylemap.normalstyle.linestyle.color = CATEGORICAL_COLORS[i % len(CATEGORICAL_COLORS)] trk.stylemap.normalstyle.linestyle.width = 5 trk.stylemap.normalstyle.labelstyle.scale = 1 trk.stylemap.highlightstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' trk.stylemap.highlightstyle.iconstyle.scale = 1.2 trk.stylemap.highlightstyle.linestyle.color = CATEGORICAL_COLORS[i % len(CATEGORICAL_COLORS)] trk.stylemap.highlightstyle.linestyle.width = 8 i += 1 doc.lookat.gxtimespan.begin = format_date(mintime) doc.lookat.gxtimespan.end = format_date(maxtime) doc.lookat.longitude = bbox_nw[0] + (bbox_se[0] - bbox_nw[0]) / 2 doc.lookat.latitude = bbox_nw[1] + (bbox_se[1] - bbox_nw[1]) / 2 doc.lookat.range = 13000.00 #doc.lookat.longitude, doc.lookat.latitude = list(list(traces)[0][1])[0] return kml.kml()
def from_locations(self, doc_name, track_name, locations): when = {} for l in locations: try: if(l.bssid not in when): when[l.bssid]=[] when[l.bssid].append({"time": l.time.strftime("%Y-%m-%dT%H:%M:%SZ-05:00"), "coords": (l.lon,l.lat)}) except: continue kml = Kml(name=doc_name) doc = kml.newdocument(name=track_name)
coord = [(-122.207881, 37.371915, 156.000000), (-122.205712, 37.373288, 152.000000), (-122.204678, 37.373939, 147.000000), (-122.203572, 37.374630, 142.199997), (-122.203451, 37.374706, 141.800003), (-122.203329, 37.374780, 141.199997), (-122.203207, 37.374857, 140.199997)] cadence = [86, 103, 108, 113, 113, 113, 113] heartrate = [181, 177, 175, 173, 173, 173, 173] power = [327.0, 177.0, 179.0, 162.0, 166.0, 177.0, 183.0] # Create the KML document kml = Kml(name="Tracks", open=1) doc = kml.newdocument(name='GPS device', snippet=Snippet('Created Wed Jun 2 15:33:39 2010')) doc.lookat.gxtimespan.begin = '2010-05-28T02:02:09Z' doc.lookat.gxtimespan.end = '2010-05-28T02:02:56Z' doc.lookat.longitude = -122.205544 doc.lookat.latitude = 37.373386 doc.lookat.range = 1300.000000 # Create a folder fol = doc.newfolder(name='Tracks') # Create a schema for extended data: heart rate, cadence and power schema = kml.newschema() schema.newgxsimplearrayfield(name='heartrate', type=Types.int, displayname='Heart Rate') schema.newgxsimplearrayfield(name='cadence',
def write_track_kml(csvreader): """ Inputs: csv contains lon/lat Output: glider track kml file """ coord = [] timerange = [] lat_f = int(cfg.get(section1, "LAT_COLUMN")) lon_f = int(cfg.get(section1, "LON_COLUMN")) date_f = int(cfg.get(section1, "DATE_COLUMN")) date_fmt = cfg.get(section1, "DATE_FORMAT") kml_dir = cfg.get(section1, "KML_DIR") mission_date = cfg.get(section1, "MISSION_START_DATE") organization = cfg.get(section1, "ORGANIZATION") vehicle_name = cfg.get(section1, "VEHICLE_NAME") kml_title = cfg.get(section1, "KML_DOC_TITLE") kml_lookat_lon = float(cfg.get(section1, "KML_LOOKAT_LON")) kml_lookat_lat = float(cfg.get(section1, "KML_LOOKAT_LAT")) kml_lookat_range = float(cfg.get(section1, "KML_LOOKAT_RANGE")) kml_cdata_title = cfg.get(section1, "KML_CDATA_TITLE") plot_url = cfg.get(section1, "PLOT_URL") plot_width = int(cfg.get(section1, "PLOT_WIDTH")) plot_height = int(cfg.get(section1, "PLOT_HEIGHT")) plot_temp = cfg.get(section1, "PLOT_TEMP") plot_oxyg = cfg.get(section1, "PLOT_OXYG") plot_sali = cfg.get(section1, "PLOT_SALI") plot_chlo = cfg.get(section1, "PLOT_CHLO") plot_cdom = cfg.get(section1, "PLOT_CDOM") icon_url = cfg.get(section1, "ICON_URL") icon_normal_scale = cfg.get(section1, "ICON_NORMAL_SCALE") icon_normal_color = cfg.get(section1, "ICON_NORMAL_COLOR") icon_normal_width = cfg.get(section1, "ICON_NORMAL_WIDTH") icon_highlight_url = cfg.get(section1, "ICON_HIGHLIGHT_URL") icon_highlight_scale = cfg.get(section1, "ICON_HIGHLIGHT_SCALE") icon_highlight_color = cfg.get(section1, "ICON_HIGHLIGHT_COLOR") icon_highlight_width = cfg.get(section1, "ICON_HIGHLIGHT_WIDTH") path_line_color = cfg.get(section1, "PATH_LINE_COLOR") path_line_width = int(cfg.get(section1, "PATH_LINE_WIDTH")) csvheader = cfg.get(section1, "CSV_HEADER") if csvheader == "YES": csvreader.next() else: pass for row in csvreader: coord.append((row[lon_f - 1], row[lat_f - 1], 0.0)) # -1 for python order timestamp = time.strptime(row[date_f - 1], date_fmt) kmltime = time.strftime("%Y-%m-%dT%H:%M:%SZ", timestamp) # KML requires specific time format timerange.append(kmltime) # time stamp # This constructs the KML document from the CSV file. kml = Kml(name="%s %s" % (organization, vehicle_name)) doc = kml.newdocument(name="%s" % kml_title, snippet=Snippet(timerange[0])) doc.lookat.gxtimespan.begin = timerange[0] doc.lookat.gxtimespan.end = timerange[-1] doc.lookat.longitude = kml_lookat_lon doc.lookat.latitude = kml_lookat_lat doc.lookat.range = kml_lookat_range # Create a folder ge_dir = doc.newfolder(name="Tracks") # Create a schema for extended data: heart rate, cadence and power schema = kml.newschema() # Create a new track in the folder trk = ge_dir.newgxtrack(name="%s %s" % (organization, vehicle_name)) desc1 = "<![CDATA[\n%s<br />\n<br />\n" % kml_cdata_title desc2 = "<a href='%s/glider.html' target='_blank'>Link to Plot</a><br />\n" % plot_url desc_temp = "<img src='%s/%s' height='%d' width='%d' /><br />\n" % (plot_url, plot_temp, plot_height, plot_width) desc_oxyg = "<img src='%s/%s' height='%d' width='%d' /><br />\n" % (plot_url, plot_oxyg, plot_height, plot_width) desc_sali = "<img src='%s/%s' height='%d' width='%d' /><br />\n" % (plot_url, plot_sali, plot_height, plot_width) desc_chlo = "<img src='%s/%s' height='%d' width='%d' /><br />\n" % (plot_url, plot_chlo, plot_height, plot_width) desc_cdom = "<img src='%s/%s' height='%d' width='%d' /><br />\n" % (plot_url, plot_cdom, plot_height, plot_width) desc3 = "]]>\n" trk.description = desc1 + desc2 + desc_temp + desc_oxyg + desc_sali + desc_chlo + desc_cdom + desc3 # Apply the above schema to this track trk.extendeddata.schemadata.schemaurl = schema.id # Add all information to the track trk.newwhen(timerange) # Each item in the give nlist will become a new <when> tag trk.newgxcoord(coord) # Ditto # Style trk.stylemap.normalstyle.iconstyle.icon.href = icon_url trk.stylemap.normalstyle.iconstyle.scale = icon_normal_scale trk.stylemap.normalstyle.linestyle.color = icon_normal_color trk.stylemap.normalstyle.linestyle.width = icon_normal_width trk.stylemap.highlightstyle.iconstyle.icon.href = icon_highlight_url trk.stylemap.highlightstyle.iconstyle.scale = icon_highlight_scale trk.stylemap.highlightstyle.linestyle.color = icon_highlight_color trk.stylemap.highlightstyle.linestyle.width = icon_highlight_width # Create a path line gpath = kml.newlinestring(name="%s %s" % (organization, vehicle_name)) gpath.description = trk.description gpath.timespan.begin = timerange[0] gpath.timespan.end = "" gpath.coords = coord gpath.style.linestyle.color = path_line_color gpath.style.linestyle.width = path_line_width # Check if KML Directory exists if not os.path.exists(kml_dir): os.makedirs(kml_dir) # Save the KML kml.save("%s/Glider_%s_%s_%s.kml" % (kml_dir, organization, vehicle_name, mission_date)) print("Glider_%s_%s_%s.kml created in '%s' folder" % (organization, vehicle_name, mission_date, kml_dir))
def plot_the_files(self, results, issi, google, gps, firstplot): """ Receives the results and an issi's to plot :param firstplot: :param gps: :param google: :param results: :param issi: :return: """ when = [] coord = [] speeds = [] headings = [] times = [] year = results[issi][0][1][6:10] month = results[issi][0][1][3:5] day = results[issi][0][1][0:2] kml = Kml(name="{}_{}-{}-{}".format(issi, year, month, day), open=1) doc = kml.newdocument(name="{}".format(issi), snippet=Snippet('Created {}-{}-{}'.format( year, month, day))) for x in range(0, len(results[issi])): tup = (results[issi][x][3], results[issi][x][2]) theTime = results[issi][x][1][11:] when.append("{}-{}-{}T{}Z".format(year, month, day, theTime)) coord.append(tup) speeds.append(int(results[issi][x][4])) headings.append(int(results[issi][x][5])) times.append(results[issi][x][1]) # Create circle track if gps[0] != 0 and firstplot: R = 6378.1 d = float(gps[2]) # distance circle_coords = [] lat1 = radians(float(gps[0])) lon1 = radians(float(gps[1])) for b in range(1, 360): brng = radians(b) lat2 = asin( sin(lat1) * cos(d / R) + cos(lat1) * sin(d / R) * cos(brng)) lon2 = lon1 + atan2( sin(brng) * sin(d / R) * cos(lat1), cos(d / R) - sin(lat1) * sin(lat2)) lat2 = degrees(lat2) lon2 = degrees(lon2) circle_coords.append((lon2, lat2)) doc2 = kml.newdocument(name="Search Area", snippet=Snippet('{}-{}-{}'.format( gps[0], gps[1], gps[2]))) fol2 = doc2.newfolder(name='Search Area') trk2 = fol2.newgxtrack(name='search area') trk2.newgxcoord(circle_coords) trk2.stylemap.normalstyle.linestyle.color = '641400FF' trk2.stylemap.normalstyle.linestyle.width = 6 # Folder fol = doc.newfolder(name='Tracks') # schema for extra data schema = kml.newschema() schema.newgxsimplearrayfield(name='speed', type=Types.int, displayname='Speed') schema.newgxsimplearrayfield(name='heading', type=Types.int, displayname='Heading') schema.newgxsimplearrayfield(name='time', type=Types.string, displayname='Time') # New Track trk = fol.newgxtrack(name=issi) # Apply Schema trk.extendeddata.schemadata.schemaurl = schema.id # add all info to track trk.newwhen(when) trk.newgxcoord(coord) trk.extendeddata.schemadata.newgxsimplearraydata('time', times) trk.extendeddata.schemadata.newgxsimplearraydata('speed', speeds) trk.extendeddata.schemadata.newgxsimplearraydata('heading', headings) # Styling trk.stylemap.normalstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/track.png' trk.stylemap.normalstyle.linestyle.color = '99ffac59' trk.stylemap.normalstyle.linestyle.width = 6 trk.stylemap.highlightstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/track.png' trk.stylemap.highlightstyle.iconstyle.scale = 1.2 trk.stylemap.highlightstyle.linestyle.color = '99ffac59' trk.stylemap.highlightstyle.linestyle.width = 8 kml.save("results/{}_{}-{}-{}.kml".format(issi, year, month, day)) if google: try: os.system( "start " + "results/{}_{}-{}-{}.kml".format(issi, year, month, day)) except: pass
coord = [(-122.207881,37.371915,156.000000), (-122.205712,37.373288,152.000000), (-122.204678,37.373939,147.000000), (-122.203572,37.374630,142.199997), (-122.203451,37.374706,141.800003), (-122.203329,37.374780,141.199997), (-122.203207,37.374857,140.199997)] cadence = [86, 103, 108, 113, 113, 113, 113] heartrate = [181, 177, 175, 173, 173, 173, 173] power = [327.0, 177.0, 179.0, 162.0, 166.0, 177.0, 183.0] # Create the KML document kml = Kml(name="Tracks", open=1) doc = kml.newdocument(name='GPS device', snippet=Snippet('Created Wed Jun 2 15:33:39 2010')) doc.lookat.gxtimespan.begin = '2010-05-28T02:02:09Z' doc.lookat.gxtimespan.end = '2010-05-28T02:02:56Z' doc.lookat.longitude = -122.205544 doc.lookat.latitude = 37.373386 doc.lookat.range = 1300.000000 # Create a folder fol = doc.newfolder(name='Tracks') # Create a schema for extended data: heart rate, cadence and power schema = kml.newschema() schema.newgxsimplearrayfield(name='heartrate', type=Types.int, displayname='Heart Rate') schema.newgxsimplearrayfield(name='cadence', type=Types.int, displayname='Cadence') schema.newgxsimplearrayfield(name='power', type=Types.float, displayname='Power')
""" The very basics of simplekml. """ from __future__ import unicode_literals import os from simplekml import Kml, ColorMode, AltitudeMode, Style # Create an instance of Kml kml = Kml(name="Basics", open=1) # Create a new document doc = kml.newdocument(name="A Document") # Create a nested document nestdoc = doc.newdocument() nestdoc.name = "A Nested Document" nestdoc.description = "\u2013 This is the nested document's description with unicode." # Create a new folder at the top level fol = kml.newfolder() fol.name = "A Folder" fol.description = "Description of a folder" # Some sub folders fol = fol.newfolder(name='A Nested Folder', description="Description of a nested folder") fol = kml.newfolder(name='Point Tests', description="Description of Point Folder") # A folder containing points with style stpnt = fol.newpoint(name="Cape Town Stadium", description='The Cape Town stadium built for the 2010 world cup soccer.', coords=[(18.411102, -33.903486)]) vapnt = fol.newpoint()