def get_kml_doc(llhs): """Generates a KML document from a Pandas table of single point solutions. Requires columns lat, lon, and height. """ from pykml.parser import Schema from pykml.factory import KML_ElementMaker as KML from pykml.factory import GX_ElementMaker as GX center = llhs[['lat', 'lon', 'height']].mean() elts = lambda e: '%s,%s,%d' % (e['lon'], e['lat'], e['height']) coords = ' '.join(llhs.apply(elts, axis=1).values) xml_coords = KML.coordinates(coords) doc = KML.kml( KML.Placemark( KML.name("gx:altitudeMode Example"), KML.LookAt( KML.longitude(center.lon), KML.latitude(center.lat), KML.heading(center.height), KML.tilt(70), KML.range(6300), GX.altitudeMode("relativeToSeaFloor"),), KML.LineString( KML.extrude(1), GX.altitudeMode("relativeToSeaFloor"), xml_coords ) ) ) return doc
def build_test_kml(): """build a simple KML file with a simple LineString, for testing purposes""" from pykml.factory import KML_ElementMaker as KML from pykml.factory import GX_ElementMaker as GX from lxml import etree from django.http import HttpResponse kml = KML.kml( KML.Placemark( KML.name("build_test_kml output"), KML.LookAt( KML.longitude(146.806), KML.latitude(12.219), KML.heading(-60), KML.tilt(70), KML.range(6300), GX.altitudeMode("relativeToSeaFloor"), ), KML.LineString( KML.extrude(1), GX.altitudeMode("relativeToSeaFloor"), KML.coordinates( "146.825,12.233,400 " "146.820,12.222,400 " "146.812,12.212,400 " "146.796,12.209,400 " "146.788,12.205,400" ), ), ) ) kml_str = etree.tostring(kml) return HttpResponse(kml_str, content_type="application/vnd.google-earth.kml")
def task_to_kml_with_pykml(df_task, outdir, filename_base, disp): from lxml import etree from pykml.parser import Schema from pykml.factory import KML_ElementMaker as KML from pykml.factory import GX_ElementMaker as GX s_coords = task_to_string(df_task) (lat, lon) = calculate_center(df_task) #def turn_point_to_placemark(tp): # placemark = KML.Placemark( # KML.name(tp['Name']), # KML.description(tp['Name']), # KML.Point( # KML.coordinates(tp['Lon'], tp['Lat'], tp['Altitude']) # ), # ) # return(placemark) #placemarks = [turn_point_to_placemark(tp) for i, tp in df_task.iterrows()] doc = KML.kml( KML.Placemark( KML.name("Condor task '%s'" % filename_base), KML.LookAt( KML.longitude(lon), KML.latitude(lat), KML.heading(0), KML.tilt(60), KML.range(80000), GX.altitudeMode("relativeToSeaFloor"), #GX.altitudeMode("absolute"), ), KML.LineString( KML.extrude(1), GX.altitudeMode("relativeToSeaFloor"), #GX.altitudeMode("absolute"), KML.coordinates(s_coords), ), ), #*placemarks ) if disp: print(etree.tostring(doc, pretty_print=True)) # output a KML file (named based on the Python script) filename_out = os.path.join(outdir, filename_base + '.kml') print("Output '%s'" % filename_out) outfile = file(filename_out, 'w') outfile.write(etree.tostring(doc, pretty_print=True)) assert Schema('kml22gx.xsd').validate(doc)
def task_to_kml_with_pykml(df_task, outdir, filename_base, disp): from lxml import etree from pykml.parser import Schema from pykml.factory import KML_ElementMaker as KML from pykml.factory import GX_ElementMaker as GX s_coords = task_to_string(df_task) (lat, lon) = calculate_center(df_task) #def turn_point_to_placemark(tp): # placemark = KML.Placemark( # KML.name(tp['Name']), # KML.description(tp['Name']), # KML.Point( # KML.coordinates(tp['Lon'], tp['Lat'], tp['Altitude']) # ), # ) # return(placemark) #placemarks = [turn_point_to_placemark(tp) for i, tp in df_task.iterrows()] doc = KML.kml( KML.Placemark( KML.name("Condor task '%s'" % filename_base), KML.LookAt( KML.longitude(lon), KML.latitude(lat), KML.heading(0), KML.tilt(60), KML.range(80000), GX.altitudeMode("relativeToSeaFloor"), #GX.altitudeMode("absolute"), ), KML.LineString( KML.extrude(1), GX.altitudeMode("relativeToSeaFloor"), #GX.altitudeMode("absolute"), KML.coordinates(s_coords), ), ), #*placemarks ) if disp: print(etree.tostring(doc, pretty_print=True)) # output a KML file (named based on the Python script) filename_out = os.path.join(outdir, filename_base + '.kml') print("Output '%s'" % filename_out) outfile = file(filename_out,'w') outfile.write(etree.tostring(doc, pretty_print=True)) assert Schema('kml22gx.xsd').validate(doc)
def makeplacemark(self): if self.__pointcount == 0: # We have no points since last placemark. Don't do anything. return if not self.__isstride: self.__coordstr += ' {},{},0 {},{},0'.format( self.__prevplacemarklng, self.__prevplacemarklat, self.__currlng, self.__currlat) placemark = KML.Placemark() geometry = KML.MultiGeometry( KML.LineString(KML.tessellate('1'), GX.altitudeMode('clampToGround'), KML.coordinates(self.__coordstr))) if self.__styletag is not None: placemark.append(KML.styleUrl('#' + str(self.__styletag))) placemark.append(geometry) if self.__startime is not None and self.__endtime is not None: placemark.append(_maketimespan(self.__startime, self.__endtime)) self.__kmlcontainer.append(placemark) self.__coordstr = '' self.__pointcount = 0 self.__prevplacemarklat = self.__currlat self.__prevplacemarklng = self.__currlng
def get_track(self, model_url=None): track = GX.Track(id=self.track_id) track.append(GX.altitudeMode('absolute')) while self.has_next_packet(): self.get_next_packet().append_telemetry_to(track) #if model_url is not None: # track.append(KML.Model(KML.Link(KML.href(model_url)), KML.Orientation(KML.heading(180)))) return track
def SetView(self, lat=0.0, lon=0.0, heading=0.0, tilt=0.0, view_range=0.0): look_at_ = KML.LookAt( KML.longitude(lon), KML.latitude(lat), KML.heading(heading), KML.tilt(tilt), KML.range(view_range), GX.altitudeMode('absolute'), ) self.KmlDoc.Document.append(look_at_)
def addsegment(kmlcontainer, styletag, lata, lnga, latb, lngb, name='segment'): coordstr = ' {},{},0 {},{},0'.format(lnga, lata, lngb, latb) placemark = KML.Placemark( KML.styleUrl('#' + str(styletag)), KML.name(name), KML.MultiGeometry( KML.LineString(KML.tessellate('1'), GX.altitudeMode('clampToGround'), KML.coordinates(coordstr)))) kmlcontainer.append(placemark)
def addboundingbox(kmlcontainer, styletag, nelat, nelng, swlat, swlng): coordstr = ' {},{},0'.format(nelng, nelat) coordstr += ' {},{},0'.format(nelng, swlat) coordstr += ' {},{},0'.format(swlng, swlat) coordstr += ' {},{},0'.format(swlng, nelat) coordstr += ' {},{},0'.format(nelng, nelat) placemark = KML.Placemark( KML.styleUrl('#' + str(styletag)), KML.Polygon( KML.tessellate('1'), GX.altitudeMode('clampToGround'), KML.outerBoundaryIs(KML.LinearRing(KML.coordinates(coordstr))))) kmlcontainer.append(placemark)
def compute_kml_point_description(coord, name): coords_str = " " + ",".join(str(num) for num in coord) + " " description = KML.Placemark( KML.name(name), KML.Point( # https://developers.google.com/kml/documentation/altitudemode # possible values: # from the surface of the Earth(relativeToGround) # above sea level(absolute) # from the bottom of major bodies of water (relativeToSeaFloor) GX.altitudeMode("absolute"), KML.coordinates(coords_str), ), ) return description
def addpolygon(kmlcontainer, styletag, coordlist, name='polygon'): coordstr = '' first = None for coord in coordlist: if first is None: first = coord coordstr += ' {},{},0'.format(coord.lon, coord.lat) coordstr += ' {},{},0'.format(first.lon, first.lat) placemark = KML.Placemark( KML.name(name), KML.styleUrl('#' + str(styletag)), KML.Polygon( KML.tessellate('1'), GX.altitudeMode('clampToGround'), KML.outerBoundaryIs(KML.LinearRing(KML.coordinates(coordstr))))) kmlcontainer.append(placemark)
def compute_kml_line_3d_description(coord_list, name): coord_str_list = [" "] for coord in coord_list: coords_str = ",".join(str(num) for num in coord) + " " coord_str_list.append(coords_str) description = KML.Placemark( KML.name(name), KML.LineString( # https://developers.google.com/kml/documentation/altitudemode # possible values: # from the surface of the Earth(relativeToGround) # above sea level(absolute) # from the bottom of major bodies of water (relativeToSeaFloor) GX.altitudeMode("absolute"), KML.coordinates(*coord_str_list), ), ) return description
def append_linestring(timeDiff, dist, normalized, thisStyle, row): doc.append( KML.Placemark( KML.name("Point " + str(count/lineReduceNumBy)), KML.ExtendedData( KML.Data(KML.value(timeDiff), name="Time Length"), KML.Data(KML.value(dist), name="Distance Metres"), KML.Data(KML.value(dist/timeDiff.seconds), name="Average Speed m/s"), KML.Data(KML.value(normalized), name="Normalized Speed"), ), KML.styleUrl("#{}".format(thisStyle)), KML.LineString( KML.extrude('1'), GX.altitudeMode('relative'), KML.coordinates( previousPoint, "{},{},{}".format(row['LOCATION Longitude : '],row['LOCATION Latitude : '],0) ) ) ))
def addsegments(kmlcontainer, styletag, points, stride=50): pointiter = iter(points) try: initialpoint = next(pointiter) except StopIteration: raise ValueError('Number of points is 0') nsegmentsadded = 0 currlat = initialpoint[0] currlng = initialpoint[1] coordstr = '' for i, point in enumerate(pointiter): if (i + 1) % stride != 0: continue nextlat = point[0] nextlng = point[1] coordstr += ' {},{},0 {},{},0'.format(currlng, currlat, nextlng, nextlat) currlat = nextlat currlng = nextlng nsegmentsadded += 1 if nsegmentsadded == 0: coordstr += ' {},{},0 {},{},0'.format(initialpoint[1], initialpoint[0], currlng, currlat) placemark = KML.Placemark( KML.styleUrl('#' + str(styletag)), KML.MultiGeometry( KML.LineString(KML.tessellate('1'), GX.altitudeMode('clampToGround'), KML.coordinates(coordstr)))) kmlcontainer.append(placemark)
def output_data_to_file(road_data): stylename = 'blueLine' doc = KML.kml( KML.Document( KML.Name("yan an"), KML.Style(KML.LineStyle(KML.color('FFF00014'), KML.width('5.0')), id=stylename))) for road in road_data: pm = KML.Placemark( KML.name(road.road_id), KML.styleUrl('#{0}'.format(stylename)), KML.LineString( KML.extrude(0), GX.altitudeMode("absolute"), KML.coordinates(' '.join([ turn_3857_project_to_4326(p) for p in road.points_with_z_value_list ])))) doc.Document.append(pm) f = open("model/test_yan_an4.kml", 'w') f.write(str(etree.tostring(doc))) f.close()
def addcircle(kmlcontainer, styletag, node, radius, name='circle', nsegments=50): if nsegments < 3: raise ValueError('Circle must be made up of 3 or more segments.') arclength = 360.0 / float(nsegments) currdegree = 0.0 currnode = None coordstr = '' firstnode = None for i in range(nsegments): nextnode = node.destinationnode(currdegree, radius) if currnode is not None: coordstr += ' {},{},0 {},{},0'.format(currnode.lon, currnode.lat, nextnode.lon, nextnode.lat) else: firstnode = nextnode currnode = nextnode currdegree += arclength if firstnode is not None: coordstr += ' {},{},0 {},{},0'.format(currnode.lon, currnode.lat, firstnode.lon, firstnode.lat) placemark = KML.Placemark( KML.styleUrl('#' + str(styletag)), KML.name(name), KML.Polygon( KML.tessellate('1'), GX.altitudeMode('clampToGround'), KML.outerBoundaryIs(KML.LinearRing(KML.coordinates(coordstr))))) kmlcontainer.append(placemark)
from lxml import etree from pykml.parser import Schema from pykml.factory import KML_ElementMaker as KML from pykml.factory import GX_ElementMaker as GX doc = KML.kml( KML.Placemark( KML.name("gx:altitudeMode Example"), KML.LookAt( KML.longitude(146.806), KML.latitude(12.219), KML.heading(-60), KML.tilt(70), KML.range(6300), GX.altitudeMode("relativeToSeaFloor"), ), KML.LineString( KML.extrude(1), GX.altitudeMode("relativeToSeaFloor"), KML.coordinates("146.825,12.233,400 " "146.820,12.222,400 " "146.812,12.212,400 " "146.796,12.209,400 " "146.788,12.205,400")))) print etree.tostring(doc, pretty_print=True) # output a KML file (named based on the Python script) outfile = file(__file__.rstrip('.py') + '.kml', 'w') outfile.write(etree.tostring(doc, pretty_print=True))
from lxml import etree from pykml.parser import Schema from pykml.factory import KML_ElementMaker as KML from pykml.factory import GX_ElementMaker as GX doc = KML.kml( KML.Placemark( KML.name("gx:altitudeMode Example"), KML.LookAt( KML.longitude(146.806), KML.latitude(12.219), KML.heading(-60), KML.tilt(70), KML.range(6300), GX.altitudeMode("relativeToSeaFloor"), ), KML.LineString( KML.extrude(1), GX.altitudeMode("relativeToSeaFloor"), KML.coordinates( "146.825,12.233,400 " "146.820,12.222,400 " "146.812,12.212,400 " "146.796,12.209,400 " "146.788,12.205,400" ) ) ) )
route = json.loads(urllib2.urlopen(URL).read()) pList = '' for point in route: pList = pList + str(point['lon']) + ',' + str(point['lat']) + ',1,' pm = KML.Placemark( KML.name('Curiosity traversal'), KML.LookAt( KML.longitude(route[0]['lon']), KML.latitude(route[0]['lat']), KML.heading('0'), KML.tilt('40'), KML.range('2000'), GX.altitudeMode('relativeToSeaFloor'), ), KML.LineStyle(KML.color('#00FFFF'), KML.width(10)), KML.altitudeMode('clampToGround'), KML.LineString(KML.extrude('1'), GX.altitudeMode('relativeToSeaFloor'), KML.coordinates(pList))) folder = KML.Folder() folder.append(pm) # create a document element with a single label style kmlobj = KML.kml( KML.Document(KML.Style(KML.LabelStyle(KML.scale(1)), id="big_label"))) kmlobj.Document.append(folder) with io.open('MSLRoute.kml', 'w') as out: out.write(
def createKML(df, run, adjusted): for i, row in df.iterrows(): if adjusted == True: lon1 = row['r1sl_adjusted_latlon'][0] lat1 = row['r1sl_adjusted_latlon'][1] lon2 = row['r1sr_adjusted_latlon'][0] lat2 = row['r1sr_adjusted_latlon'][1] lon3 = row['r2sl_adjusted_latlon'][0] lat3 = row['r2sl_adjusted_latlon'][1] lon4 = row['r2sr_adjusted_latlon'][0] lat4 = row['r2sr_adjusted_latlon'][1] coordinates = (str(lon1)+','+str(lat1)+','+ str(row['r1sl_adjusted'][2] + 5) +' '+ str(lon3)+','+str(lat3)+','+ str(row['r2sl_adjusted'][2] + 5) +' '+ str(lon4)+','+str(lat4)+','+ str(row['r2sr_adjusted'][2] + 5 ) +' '+ str(lon2)+','+str(lat2)+','+ str(row['r1sr_adjusted'][2] + 5)) color = '#a00000ff' else: lon1 = row['r1sl_latlon'][0] lat1 = row['r1sl_latlon'][1] lon2 = row['r1sr_latlon'][0] lat2 = row['r1sr_latlon'][1] lon3 = row['r2sl_latlon'][0] lat3 = row['r2sl_latlon'][1] lon4 = row['r2sr_latlon'][0] lat4 = row['r2sr_latlon'][1] coordinates = (str(lon1)+','+str(lat1)+','+ str(float(row['r1sl_elev']) + 5) +' '+ str(lon3)+','+str(lat3)+','+ str(float(row['r2sl_elev']) + 5) +' '+ str(lon4)+','+str(lat4)+','+ str(float(row['r2sr_elev']) + 5 ) +' '+ str(lon2)+','+str(lat2)+','+ str(float(row['r1sr_elev']) + 5)) color = '#a0ff0000' polygon_kml = KML.kml( KML.Placemark( KML.Style( KML.PolyStyle( KML.color(color) ) ), KML.name(str(run)+'_'+str(row['Label'])), KML.Polygon( KML.extrude(1), GX.altitudeMode('absolute'), KML.outerBoundaryIs( KML.LinearRing( KML.coordinates( coordinates ) ) ) ) ) ) if adjusted == True: outfile = open('kmlFiles/viewDomains/'+str(run)+'/'+str(row['Label'])+'_adj.kml', 'w') else: outfile = open('kmlFiles/viewDomains/'+str(run)+'/'+str(row['Label'])+'.kml', 'w') outfile.write(etree.tostring(polygon_kml, pretty_print = True)) outfile.close()
), id=('pushpin1')), KML.Folder( KML.name(gmlname), KML.open('1'), ))) #loop for vertex for i in range(nodelen): doc.Document.Folder.append( KML.Placemark( KML.name(listLabel[i]), KML.LookAt(KML.longitude(listLong[i]), KML.latitude(listLat[i]), KML.altitude(0), KML.heading(0), KML.tilt(0), KML.range(1000), GX.altitudeMode('relativeToGround')), KML.styleUrl('#pushpinm'), KML.Point( GX.drawOrder(1), KML.coordinates( str(listLong[i]) + ',' + str(listLat[i]) + ',0'))), ) # loop for edge for i in range(len(listEdge)): doc.Document.Folder.append( KML.Placemark( KML.name('PATH' + str(i)), KML.styleUrl('#inline1'), KML.LineString( KML.tessellate(1), #GX.altitudeMode("relativeToSeaFloor"),
route = json.loads( urllib2.urlopen(URL).read() ) pList = '' for point in route: pList = pList + str(point['lon']) + ',' + str(point['lat']) +',1,' pm = KML.Placemark( KML.name('Curiosity traversal'), KML.LookAt( KML.longitude(route[0]['lon']), KML.latitude(route[0]['lat']), KML.heading('0'), KML.tilt('40'), KML.range('2000'), GX.altitudeMode('relativeToSeaFloor'), ), KML.LineStyle( KML.color('#00FFFF'), KML.width(10) ), KML.altitudeMode('clampToGround'), KML.LineString(KML.extrude('1'), GX.altitudeMode('relativeToSeaFloor'), KML.coordinates(pList)) ) folder = KML.Folder() folder.append(pm) # create a document element with a single label style kmlobj = KML.kml( KML.Document(