def main(args, options): global DEBUG DEBUG = options.debug global VERBOSE VERBOSE = options.verbose kml = Kml() kml.document.name = "GPX Extent Map created by pqmap.py on %s" % datetime.now() count = 0 for arg in args: if DEBUG or VERBOSE: print("arg: %s" % arg) for globname in glob(arg): count += process_arg(kml, globname) document_name = os.path.join(BASEDIR, DOCUMENT_NAME) kml.save(document_name) if not options.verbose: print() print("%d .gpx files processed" % count) print("Output is in %s" % document_name)
def process(): distances = sorted(compute_distances()) # thin out the result by taking every 50th distance THIN = 50 distances = distances[::THIN] pprint(distances) bearings = compute_bearings() locations = compute_locations(distances, bearings) points = compute_points(locations) kml = Kml() for distance, bearing, lat, lon in points: name = "d_%s_b_%s" % (distance, bearing) kml.newpoint( name=name, coords=[(lon, lat)] ) kml.save("ayhe11.kml")
def main(): """Compute longitude to fit with computed latitude and checksum.""" kml = Kml() kml.newpoint(name="Vitts Mill", coords=[(HOME[1], HOME[0])]) kml.newpoint(name="Vitts Mill WP2", coords=[(WP2[1], WP2[0])]) # known values for A, B, C lat = "N38 27.%d%d%d" % (A, B, C) clat = convert(lat) # all answers sum to 24 leftovers = 24 - (A + B + C) # compute all values for D, E and F for D in range(10): if D > leftovers: continue for E in range(10): if (D + E) > leftovers: continue for F in range(10): if D + E + F == leftovers: lon = "W91 00.%d%d%d" % (D, E, F) clon = convert(lon) here = (clat, clon) # compute distance from posted coordinates d = vincenty(HOME, here).miles print(d, lat, lon) name = "loc_%d%d%d%d%d%d" % (A, B, C, D, E, F) kml.newpoint(name=name, coords=[(clon, clat)]) kml.save(FILENAME) print("Output is in %s" % FILENAME)
def exportToKml2() : # KML kml = Kml() for aGroupedWayKey in subRoutes : aGroupedWay = subRoutes[aGroupedWayKey][0] lineNames = ','.join(aGroupedWay.lines) coords = list() for aNodeKey in aGroupedWay.nodesList : if type(aNodeKey) is str : aNode = nodesDict[aNodeKey] lat = aNode.lat lon = aNode.lon elif type(aNodeKey) is OsmNode: lat = aNodeKey.lat lon = aNodeKey.lon else : lat = aNodeKey[0] lon = aNodeKey[1] coords.append((lon,lat)) lin = kml.newlinestring(name="Pathway", description='-', coords= coords) r = lambda: random.randint(0,255) randomColor = '#ff%02X%02X%02X' % (r(),r(),r()) #random ARGB color lin.style.linestyle.color = randomColor lin.style.linestyle.width= 10 # 10 pixels kml.save("singlestyle.kml")
def exportToKml2(): # KML kml = Kml() for aGroupedWayKey in subRoutes: aGroupedWay = subRoutes[aGroupedWayKey][0] lineNames = ','.join(aGroupedWay.lines) coords = list() for aNodeKey in aGroupedWay.nodesList: if type(aNodeKey) is str: aNode = nodesDict[aNodeKey] lat = aNode.lat lon = aNode.lon elif type(aNodeKey) is OsmNode: lat = aNodeKey.lat lon = aNodeKey.lon else: lat = aNodeKey[0] lon = aNodeKey[1] coords.append((lon, lat)) lin = kml.newlinestring(name="Pathway", description='-', coords=coords) r = lambda: random.randint(0, 255) randomColor = '#ff%02X%02X%02X' % (r(), r(), r()) #random ARGB color lin.style.linestyle.color = randomColor lin.style.linestyle.width = 10 # 10 pixels kml.save("singlestyle.kml")
def density_kml(kml_path, city, dicts, borders, scaling=(lambda x: x)): def rgb_to_bgr(color): return "{rgb[4]}{rgb[5]}{rgb[2]}{rgb[3]}{rgb[0]}{rgb[1]}".format( rgb=color) def add_folder(kml, data): def cell_to_color(value, color, scaling): norm_value = scaling(value) return '{0:02x}{1}'.format(int(norm_value * 200), color) folder_dict = data['dict'] # normalizing maximum = data['max'] norm_scaling = lambda x: scaling(x / maximum) # make a kml of polygons folder = kml.newfolder(name=data['name']) color = rgb_to_bgr(data['color']) folder = dict_to_kml(folder, borders, folder_dict, cell_to_color, color, norm_scaling) return kml kml = Kml() for data in dicts: kml = add_folder(kml, data) kml.save(kml_path)
def exportToKml() : # KML kml = Kml() for aMergedWayTupleKey in tramRoutes : aMergedWayTuple = tramRoutes[aMergedWayTupleKey] aMergedWay = aMergedWayTuple[1] lineNames = ','.join(aMergedWayTuple[0]) coords = list() for aCoordTuple in aMergedWay : lat = aCoordTuple[0] lon = aCoordTuple[1] coords.append((lon,lat)) lin = kml.newlinestring(name="Pathway", description='-', coords= coords) r = lambda: random.randint(0,255) randomColor = '#ff%02X%02X%02X' % (r(),r(),r()) lin.style.linestyle.color = randomColor lin.style.linestyle.width= 10 # 10 pixels kml.save("singlestyle.kml")
def main(): global options, args if options.list_colors: create_color_map(COLOR_TABLE) sys.exit() output_filename = options.output kml = Kml() kml.document.name = "Test of kmldraw.py" for line in RAWINPUT[1:-1].split('\n'): text, quad = line.split("\t") name = text.strip() import re result = re.match( '\((\d+.\d+), (\d+.\d+), (-\d+.\d+), (-\d+.\d+)\)', quad) minlat, maxlat, minlon, maxlon = list(map(float, result.groups())) quad = (minlat, maxlat, minlon, maxlon) kmldraw(kml, name, quad) kml.save(output_filename) print("Wrote to: %s" % output_filename)
def prep_wind_kml_per_hour(data_to_show): ### create the kml file kml = Kml(open=1) ### name columns according to hour of day lon_col = "lon" lat_col = "lat" data_col = "data" ### create lines that represent thermals ### calculate centre of the small grid ### place the line segment in the centre ### take wind speed and direction and calculate coordinates ### of the end point of the line segment ### add the arrow pointer for i in range(1, len(data_to_show) - 1): x1 = -data_to_show[lon_col][i] y1 = data_to_show[lat_col][i] z1 = data_to_show[data_col][i] x2 = -data_to_show[lon_col][i + 1] y2 = data_to_show[lat_col][i + 1] z2 = data_to_show[data_col][i + 1] if z1 > 0 or z2 > 0: line_name = "line " + str(i) linestring = kml.newlinestring(name=line_name) linestring.coords = [(x1, y1), (x2, y2)] linestring.altitudemode = simplekml.AltitudeMode.relativetoground linestring.style.linestyle.width = 10 linestring.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' linestring.style.linestyle.color = '99ffac59' kml_file_name = "Roldanillo_wind.kml" kml.save(path2grid + "hours\\" + kml_file_name)
def google_earth_export(): print('ttt'*100, request.form) form = GoogleEarthForm(request.form) if 'POST' in request.method: kml_file = Kml() for node in filter(lambda obj: obj.visible, Node.query.all()): point = kml_file.newpoint(name=node.name) point.coords = [(node.longitude, node.latitude)] point.style = styles[node.subtype] point.style.labelstyle.scale = request.form['label_size'] for link in filter(lambda obj: obj.visible, Link.query.all()): line = kml_file.newlinestring(name=link.name) line.coords = [ (link.source.longitude, link.source.latitude), (link.destination.longitude, link.destination.latitude) ] line.style = styles[link.type] line.style.linestyle.width = request.form['line_width'] filepath = join(current_app.kmz_path, request.form['name'] + '.kmz') kml_file.save(filepath) return render_template( 'google_earth_export.html', form=form )
def prep_sink_kml_per_hour(hours, data_to_show): for hour in hours: ### create the kml file kml = Kml(open=1) ### name columns according to hour of day lon_col = "lon" + str(hour) lat_col = "lat" + str(hour) data_col = "data" + str(hour) ### create lines that represent thermals for i in range(len(data_to_show) - 1): x1 = -data_to_show[lon_col][i] y1 = data_to_show[lat_col][i] z1 = data_to_show[data_col][i] x2 = -data_to_show[lon_col][i + 1] y2 = data_to_show[lat_col][i + 1] z2 = data_to_show[data_col][i + 1] if z1 < -100 or z2 < -100: line_name = "line " + str(i) linestring = kml.newlinestring(name=line_name) linestring.coords = [(x1, y1), (x2, y2)] linestring.altitudemode = simplekml.AltitudeMode.absolute linestring.style.linestyle.width = 3 if z1 > -200 or z2 > -200: linestring.style.linestyle.color = simplekml.Color.green elif z1 > -400 or z2 > -400: linestring.style.linestyle.color = simplekml.Color.blue linestring.style.linestyle.width = 6 else: linestring.style.linestyle.color = simplekml.Color.black linestring.style.linestyle.width = 9 ### linestring.extrude = 1 kml_file_name = "Roldanillo_" + str(hour) + "_sinks.kml" kml.save(path2grid + "hours\\" + kml_file_name)
def makekml(t, lonLatAlt, lat0, lon0, ofn=None): assert isinstance( lonLatAlt, np.ndarray) and lonLatAlt.ndim == 2 and lonLatAlt.shape[1] == 3 kml = Kml(name='My Kml') # doc = kml.newdocument(name='My Doc',snippet=Snippet('snippet')) # doc.lookat.gxtimespan.begin = t[0] # doc.lookat.gxtimespan.end = t[-1] # doc.lookat.latitude = lat0 # doc.lookat.longitude = lon0 # doc.lookat.range = 1e3 #fol = kml.newfolder(name='My Tracks') trk = kml.newgxtrack(name='My Track') trk.newwhen(t) trk.newgxcoord(lonLatAlt.tolist()) #list of lon,lat,alt, NOT ndarray! # Styling (from simplekml docs) # trk.stylemap.normalstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' # trk.stylemap.normalstyle.linestyle.color = '99ffac59' # trk.stylemap.normalstyle.linestyle.width = 6 # 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 = '99ffac59' # trk.stylemap.highlightstyle.linestyle.width = 8 if not ofn: ofn = mkstemp(suffix='.kml')[1] print('writing to', ofn) kml.save(str(ofn))
def exportToKml(): # KML kml = Kml() for aMergedWayTupleKey in tramRoutes: aMergedWayTuple = tramRoutes[aMergedWayTupleKey] aMergedWay = aMergedWayTuple[1] lineNames = ','.join(aMergedWayTuple[0]) coords = list() for aCoordTuple in aMergedWay: lat = aCoordTuple[0] lon = aCoordTuple[1] coords.append((lon, lat)) lin = kml.newlinestring(name="Pathway", description='-', coords=coords) r = lambda: random.randint(0, 255) randomColor = '#ff%02X%02X%02X' % (r(), r(), r()) lin.style.linestyle.color = randomColor lin.style.linestyle.width = 10 # 10 pixels kml.save("singlestyle.kml")
def graph_kml( env, fname="graph.kml", icon="http://maps.google.com/mapfiles/kml/shapes/donut.png", size=0.5, scale=0.5, width=5, ): """Create a kml visualisation of graph. Env variable needs to contain graph.""" # create a kml file containing the visualisation kml = Kml() fol = kml.newfolder(name="Vessels") shared_style = Style() shared_style.labelstyle.color = "ffffffff" # White shared_style.labelstyle.scale = size shared_style.iconstyle.color = "ffffffff" # White shared_style.iconstyle.scale = scale shared_style.iconstyle.icon.href = icon shared_style.linestyle.color = "ff0055ff" # Red shared_style.linestyle.width = width nodes = list(env.FG.nodes) # each timestep will be represented as a single point for log_index, value in enumerate(list(env.FG.nodes)[0 : -1 - 1]): pnt = fol.newpoint( name="", coords=[ ( nx.get_node_attributes(env.FG, "Geometry")[nodes[log_index]].x, nx.get_node_attributes(env.FG, "Geometry")[nodes[log_index]].y, ) ], ) pnt.style = shared_style edges = list(env.FG.edges) for log_index, value in enumerate(list(env.FG.edges)[0 : -1 - 1]): lne = fol.newlinestring( name="", coords=[ ( nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][0]].x, nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][0]].y, ), ( nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][1]].x, nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][1]].y, ), ], ) lne.style = shared_style kml.save(fname)
def generate_kml(): """Generate KML file from geojson.""" uuid = request.values.get('uuid', None) campaign_name = request.values.get('campaign_name', None) campaign = Campaign(uuid) # Get json for each type. types = campaign.get_s3_types() if types is None: return Response(json.dumps({'error': 'types not found'}), 400) data = [] for t in types: data.append(campaign.get_type_geojsons(t)) if len(data) == 0: return Response(json.dumps({'error': 'Data not found'}), 400) features = [i['features'] for sublist in data for i in sublist] # for each type, we need to get geojson. kml = Kml(name=campaign_name) file_name = hashlib.md5( uuid.encode('utf-8') + '{:%m-%d-%Y}'.format(datetime.today()).encode( 'utf-8')).hexdigest() + '.kml' file_path = os.path.join(config.CACHE_DIR, file_name) # For now, let's work only with points. # TODO: include polygons in the kml file. features = [[f for f in sublist if f['geometry']['type'] == 'Point'] for sublist in features] features = [item for sublist in features for item in sublist] for feature in features: tags = feature['properties']['tags'] extended_data = ExtendedData() kml_name = '' if 'name' in tags.keys(): kml_name = tags['name'] elif 'amenity' in tags.keys(): kml_name = tags['amenity'] [ extended_data.newdata(k, escape(v)) for k, v in tags.items() if k != 'name' ] kml.newpoint(name=kml_name, extendeddata=extended_data, coords=[(feature['geometry']['coordinates'][0], feature['geometry']['coordinates'][1])]) kml.save(path=file_path) if kml: # Save file into client storage device. return Response(json.dumps({'file_name': file_name}))
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 export_to_kml(self, file_name): coords = list() for i in range(len(self._gps_latitude)): coords.append((self._gps_longitude[i], self._gps_latitude[i])) kml = Kml() lin = kml.newlinestring(name="Track", description="A track.", coords=coords) lin.style.linestyle.color = 'ff0000ff' # Red lin.style.linestyle.width = 2 # 10 pixels kml.save(file_name)
def create_color_map(color_list): kml = Kml() lat = 38.000 init_lon = lon = -90.000 extent = 0.02 for index, color in enumerate(color_list): add_patch(kml, lat, lon, extent, color) lon += extent if (index % 10) == 9: lon = init_lon lat -= extent kml.save("color_patches.kml") print("Output in color_patches.kml")
def serialize_list_point_lon_lat_index_as_kml(list_points, name='point', color=Color.green, scale=1, abolutefilepath ='points.kml'): kml = Kml() point_folder = kml.newfolder(name='points') for point in list_points: lon_lat = [point[0],point[1]] index = point[2] pnt_0 = point_folder.newpoint() pnt_0.name = str(index) pnt_0.coords = [lon_lat] pnt_0.labelstyle.color=color pnt_0.style.labelstyle.scale = scale pnt_0.style.iconstyle.icon.href='http://maps.google.com/mapfiles/kml/paddle/grn-circle-lv.png' kml.save(abolutefilepath)
def view(view_type): view_options_form = ViewOptionsForm(request.form) google_earth_form = GoogleEarthForm(request.form) labels = {'node': 'name', 'link': 'name'} if 'view_options' in request.form: # retrieve labels labels = { 'node': request.form['node_label'], 'link': request.form['link_label'] } elif 'google_earth' in request.form: kml_file = Kml() for node in filter(lambda obj: obj.visible, Node.query.all()): point = kml_file.newpoint(name=node.name) point.coords = [(node.longitude, node.latitude)] point.style = styles[node.subtype] point.style.labelstyle.scale = request.form['label_size'] for link in filter(lambda obj: obj.visible, Link.query.all()): line = kml_file.newlinestring(name=link.name) line.coords = [(link.source.longitude, link.source.latitude), (link.destination.longitude, link.destination.latitude)] line.style = styles[link.type] line.style.linestyle.width = request.form['line_width'] filepath = join(current_app.kmz_path, request.form['name'] + '.kmz') kml_file.save(filepath) return render_template( '{}_view.html'.format(view_type), view_options_form=view_options_form, google_earth_form=google_earth_form, labels=labels, names=pretty_names, subtypes=node_subtypes, node_table={ obj: OrderedDict([(property, getattr(obj, property)) for property in type_to_public_properties[obj.type]]) for obj in filter(lambda obj: obj.visible, Node.query.all()) }, link_table={ obj: OrderedDict([(property, getattr(obj, property)) for property in type_to_public_properties[obj.type]]) for obj in filter(lambda obj: obj.visible, Link.query.all()) })
def create_kml(self,name): """ create kml file from the HeatMap results :param name: filename """ file = Kml() counts = [s.count**0.5 for s in self.heatmap] c_min, c_max = min(counts), max(counts) divisor= (c_max-c_min)/9 for s in self.heatmap: single = file.newpolygon(name=str(s.count), outerboundaryis=s.get_coordinates()) single.style.polystyle.color = COL[int(((s.count**0.5)-c_min)//divisor)] file.save(name+'.kml')
def writekml(inarray, kmlfilename): kml = Kml() sharedstyle = Style() sharedstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/wht-blank.png' sharedstyle.iconstyle.color = "ffffffff" sharedstyle.iconstyle.ColorMode = "random" for indiv in numpy.nditer(inarray): desc = adddescription(indiv) lat =float(indiv[()]['Lat']) lon =float(indiv[()]['Long']) pnt = kml.newpoint(name=str(indiv[()]['GivenNames']) + " " +str(indiv[()]['FamilyName']), description=desc, coords = [(lon,lat)]) pnt.style = sharedstyle kml.save(kmlfilename)
def export_to_google_earth(): kml_file = Kml() for device in fetch_all('Device'): point = kml_file.newpoint(name=device.name) point.coords = [(device.longitude, device.latitude)] point.style = styles[device.subtype] point.style.labelstyle.scale = request.form['label_size'] for link in fetch_all('Link'): line = kml_file.newlinestring(name=link.name) line.coords = [(link.source.longitude, link.source.latitude), (link.destination.longitude, link.destination.latitude)] line.style = styles[link.type] line.style.linestyle.width = request.form['line_width'] filepath = app.path / 'google_earth' / f'{request.form["name"]}.kmz' kml_file.save(filepath) return True
def make_kml(parsed_users, output_file, verbosity): """This function reads the user data supplied by ``parsed_users``, it then generates KML output and writes it to ``output_file``. Args: parsed_users (list): A list of lists, each sub_list should have 4 elements: ``[latitude, longitude, name, comment]`` output_file (open): Location to save the KML output verbosity (int): If set to be >= ``1`` it will print out the string passed to ``message()`` """ kml = Kml() message("Making and writing KML to " + output_file, verbosity) for user in parsed_users: # Generate a KML point for the user. kml.newpoint(name=user[2], coords=[(user[1], user[0])], description=user[3]) kml.save(output_file)
def export_to_google_earth(): kml_file = Kml() for node in Node.query.all(): point = kml_file.newpoint(name=node.name) point.coords = [(node.longitude, node.latitude)] point.style = styles[node.subtype] point.style.labelstyle.scale = request.form['label_size'] for link in Link.query.all(): line = kml_file.newlinestring(name=link.name) line.coords = [(link.source.longitude, link.source.latitude), (link.destination.longitude, link.destination.latitude)] line.style = styles[link.type] line.style.linestyle.width = request.form['line_width'] filepath = join(current_app.ge_path, request.form['name'] + '.kmz') kml_file.save(filepath) return jsonify({})
def export_to_google_earth(): kml_file = Kml() for device in Device.query.all(): point = kml_file.newpoint(name=device.name) point.coords = [(device.longitude, device.latitude)] point.style = styles[device.subtype] point.style.labelstyle.scale = request.form['label_size'] for link in Link.query.all(): line = kml_file.newlinestring(name=link.name) line.coords = [(link.source.longitude, link.source.latitude), (link.destination.longitude, link.destination.latitude)] line.style = styles[link.type] line.style.linestyle.width = request.form['line_width'] filepath = join(current_app.path, 'google_earth', f'{request.form["name"]}.kmz') kml_file.save(filepath) return jsonify({'success': True})
def export_to_google_earth(self, **kwargs: Any) -> None: kml_file = Kml() for device in fetch_all("Device"): point = kml_file.newpoint(name=device.name) point.coords = [(device.longitude, device.latitude)] point.style = self.google_earth_styles[device.icon] point.style.labelstyle.scale = kwargs["label_size"] for link in fetch_all("Link"): line = kml_file.newlinestring(name=link.name) line.coords = [ (link.source.longitude, link.source.latitude), (link.destination.longitude, link.destination.latitude), ] line.style = Style() kml_color = f"ff{link.color[5:]}{link.color[3:5]}{link.color[1:3]}" line.style.linestyle.color = kml_color line.style.linestyle.width = kwargs["line_width"] filepath = self.path / "projects" / "google_earth" / f'{kwargs["name"]}.kmz' kml_file.save(filepath)
def job(): """Compute the possible final locations for the geocache""" # open a new KML file kml = Kml() print(" r c dist bear lat lon") # row checksum is 8 for rows in range(8, 100, 9): # compute the distance in feet distance = rows * FEET_PER_ROW # caps checksum is 10 for caps in range(10, 100, 9): # compute the bearing in degrees bearing = caps * DEGREES_PER_CAP # check for too far if bearing >= 360.: break # make the projection proj = projection(DOVE, distance, bearing) # get the components lat = proj.lat lon = proj.lon print("%2d %2d %8.2f %6.2f %f %f" % (rows, caps, distance, bearing, lat, lon)) # compute a name based on rows and caps name = "R%d_C%d" % (rows, caps) # add a new KML point kml.newpoint(name=name, coords=[(lon, lat)]) print kml.save('cachemas2015_day10.kml')
def fCreateLineKML(zoznam, n, f): kml = Kml() fol = kml.newfolder(name="spojnica") At = zoznam[0][3] L = zoznam[0][0] B = zoznam[0][1] for i in range(1, len(zoznam)): At = zoznam[i][3] if At == 0: continue for data in f: if At == data[1]: farba = data[0] Ld = zoznam[i][0] Bd = zoznam[i][1] pnt = fol.newlinestring(name="{0}".format(At), coords=[(L, B), (Ld, Bd)]) pnt.style.linestyle.color = farba kml.save("data/vystup/kruh/" + n + "spojnica.kml")
def prep_lift_kml_per_hour(hours, data_to_show): for hour in hours: ### create the kml file kml = Kml(open=1) ### name columns according to hour of day lon_col = "lon" + str(hour) lat_col = "lat" + str(hour) data_col = "data" + str(hour) ### calculate max lift max_lift = data_to_show[data_col].max() ### create lines that represent thermals for i in range(len(data_to_show) - 1): x1 = -data_to_show[lon_col][i] y1 = data_to_show[lat_col][i] z1 = data_to_show[data_col][i] / max_lift * 100 x2 = -data_to_show[lon_col][i + 1] y2 = data_to_show[lat_col][i + 1] z2 = data_to_show[data_col][i + 1] / max_lift * 100 if z1 > 1 or z2 > 1: line_name = "line " + str(i) linestring = kml.newlinestring(name=line_name) linestring.coords = [(x1, y1, z1), (x2, y2, z2)] linestring.altitudemode = simplekml.AltitudeMode.relativetoground linestring.style.linestyle.width = 3 if z1 < 20 or z2 < 20: linestring.style.linestyle.color = simplekml.Color.yellow elif z1 < 40 or z2 < 40: linestring.style.linestyle.color = simplekml.Color.orange linestring.style.linestyle.width = 5 else: linestring.style.linestyle.color = simplekml.Color.red linestring.style.linestyle.width = 7 ### linestring.extrude = 1 kml_file_name = "Roldanillo_" + str(hour) + "_thermals.kml" kml.save(path2grid + "hours\\" + kml_file_name)
def convert(self, output): output_filename = '{}.{}'.format(KMLParser.RESULT_FILENAME, output) if output in ['kml', 'kmz']: #check if value is in a list kmlinstance = Kml() folder = kmlinstance.newfolder() folder.name = "My Places" for name, lat, lon in self.content:#tuples can be decomposed in a for loop. This is the same as "for (x,y,z) in self.content" or "for t in self.content" and then using t[0] etc. folder.newpoint(name=name, coords=[(lat,lon)]) kmlinstance.save( output_filename ) elif output in ['terminal', 'txt']: newcontent = [ '%s\t->\t%.4f %.4f'%(name, float(lat),float(lon)) for name, lat, lon in self.content ] #list comprehensions rock!! if output == 'txt': f = open(output_filename, 'w') f.write( '\n'.join(newcontent) ) f.close() elif output is 'terminal': print '\n'.join(newcontent) elif output == 'json': newcontent = [ {'name': name, 'coordinates': {'latitude':lat, 'longitude':lon} } for name, lat, lon in self.content ] f = open(output_filename, 'w') json.dump(newcontent, f, indent=2) f.close()
def fCreatePointKML(zoznam, n): kml = Kml() fol = kml.newfolder(name="kruh") At = zoznam[0][3] L = zoznam[0][0] B = zoznam[0][1] pnt = fol.newpoint(name="{0}".format(At), coords=[(L, B)]) pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/ylw-blank-lv.png' pnt.style.iconstyle.scale = 0.5 pnt.style.labelstyle.color = 'ffffffff' pnt.style.labelstyle.scale = 1 for i in range(1, len(zoznam)): At = zoznam[i][3] if At == 0: continue L = zoznam[i][0] B = zoznam[i][1] pnt = fol.newpoint(name="{0}".format(At), coords=[(L, B)]) pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/grn-blank-lv.png' pnt.style.iconstyle.scale = 0.4 pnt.style.labelstyle.color = 'ffffffff' pnt.style.labelstyle.scale = 1 kml.save("data/vystup/kruh/" + n + "kruh.kml")
def save_edges_to_kml(edges, paths_save_file_path): line_count = 0 max_path_length = 0 kml = Kml() for idx, edge in enumerate(edges): coordinates_for_line = edge coordinates_for_line = tuple( map(lambda row: (row[1], row[0]), coordinates_for_line)) kml.newlinestring(name='Transmisssion Line %d' % idx, description='', coords=coordinates_for_line) max_path_length = max(max_path_length, len(coordinates_for_line)) line_count += 1 max_path_length = max(max_path_length, len(coordinates_for_line)) pp.pprint(kml) kml.save(paths_save_file_path, format=True) pp.pprint('paths_save_file_path: %s' % os.path.realpath(paths_save_file_path)) pp.pprint('max_path_length: %d' % max_path_length) pp.pprint('line_count: %d' % line_count)
def generate_kml(): """Generate KML file from geojson.""" features = request.values.get('location', None) uuid = request.values.get('uuid', None) campaign_name = request.values.get('campaign_name', None) if not features or not uuid: abort(404) features = json.loads(features) kml = Kml(name=campaign_name) file_name = hashlib.md5( uuid.encode('utf-8') + '{:%m-%d-%Y}'.format(datetime.today()).encode( 'utf-8')).hexdigest() + '.kml' file_path = os.path.join(config.CACHE_DIR, file_name) for feature in features: if feature['type'] == 'Point': kml_name = '' extended_data = ExtendedData() if 'name' in feature['tags']: kml_name = feature['tags']['name'] elif 'amenity' in feature['tags']: kml_name = feature['tags']['amenity'] for key, value in feature['tags'].items(): if key != 'name': extended_data.newdata(key, value) kml.newpoint(name=kml_name, extendeddata=extended_data, coords=[(feature['latlon'][1], feature['latlon'][0])]) kml.save(path=file_path) if kml: return Response(json.dumps({'file_name': file_name}))
class kmlManager(object): def init(self): self.kml_list_start_time = None self.kml_list = [] self.kml_interest_line_list = [] self.kml_interest_point_list = [] def __init__(self, line_limit_point, pathDirectory): self.kml = Kml() self.init() self.kml_interest_line_already_meeted = {} self.kml_interest_point_already_meeted = {} self.line_limit_point = line_limit_point self.pathDirectory = pathDirectory self.midday = (12,0,) def addPointOfInterest(self,point): if point.descr not in self.kml_interest_point_already_meeted: #not yet printed ? self.kml_interest_point_already_meeted[point.descr] = True self.kml_interest_point_list.append(point) def addLineOfInterest(self, line): if line.descr not in self.kml_interest_line_already_meeted: #not yet printed ? self.kml_interest_line_already_meeted[line.descr] = True self.kml_interest_line_list.append(line) def addLinePoint(self,gpoint, utcdatetime, colorMarker): if self.line_limit_point > 0:#don't want to write the line of the road ? if len(self.kml_list) == 0: #set start time self.kml_list_start_time = utcdatetime self.kml_list.append( (gpoint.lat, gpoint.lon,) ) if len(self.kml_list) >= self.line_limit_point: #kml process if len(self.kml_list) > 0: line = self.kml.newlinestring(name="From "+self.kml_list_start_time.isoformat()+" to "+utcdatetime.isoformat(), description="", coords=self.kml_list) #if past midday, colour the line in red if utcdatetime.hour < self.midday[0] or (utcdatetime.hour == self.midday[0] and utcdatetime.minute < self.midday[1]): line.style.linestyle.color = 'afff0000'#morning, blue line else: line.style.linestyle.color = 'af0000ff'#afternoon, red line #write the meeted line of interest for interest_line in self.kml_interest_line_list: #line line = self.kml.newlinestring(name=interest_line.descr, description="", coords=((interest_line.start.lat,interest_line.start.lon,), (interest_line.end.lat,interest_line.end.lon,),)) line.style.linestyle.color = 'af00ff00' #green #start point point = self.kml.newpoint(name="Start point of "+interest_line[2], description="",coords=(interest_line.start.lat,interest_line.start.lon,)) point.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/dd-start.png" #end point point = self.kml.newpoint(name="End point of "+interest_line[2], description="",coords=(interest_line.end.lat,interest_line.end.lon,)) point.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/dd-end.png" #write the meeted point of interest for interest_point in self.kml_interest_point_list: point = self.kml.newpoint(name=interest_point.name, description=interest_point.descr,coords=( (interest_point.lat, interest_point.lon,), )) point.style.iconstyle.icon.href = colorMarker.getColorPath(interest_point.name) #save the file (for every line written, overwrite the file) date = datetime.datetime.now() self.kml.save(self.pathDirectory+"skidump_"+str(date.day)+"_"+str(date.month)+"_"+str(date.year)+".kml") #reset list self.init() def addEventPointList(self,l): #get point of interest from proxy self.kml_interest_point_list.extend(l)
#Converts to KML kml = Kml() #folder = kml.newfolder(name="Gliders") #Defines the kmnl document name kml.document.name = "GliderProfile" #Makes a list of styles stylelist = [] for i in range(len(col)-1): #if (ma.getmask(x[idx]) == False and ma.getmask(y[idx]) == False and # ma.getmask(z[idx]) == False and ma.getmask(col[idx]) == False): sharedstyle = Style() sharedstyle.linestyle.color = str(colorValHex[i]) sharedstyle.linestyle.width = 10 stylelist.append(sharedstyle) #Looops over all for i in range(len(col)-1): #if (ma.getmask(x[idx]) == False and ma.getmask(y[idx]) == False and # ma.getmask(z[idx]) == False and ma.getmask(col[idx]) == False): print x[i],y[i],colorValHexKML[i],-z[i] lin = kml.newlinestring(name='',description='',coords=[(x[i],y[i],-z[i]),(x[i+1],y[i+1],-z[i+1])]) lin.style.linestyle.color = str(colorValHexKML[i]) #stylelist[i] lin.style.linestyle.width = 10 #50 pixels lin.altitudemode = simplekml.AltitudeMode.absolute kml.save("C:\Users\john.tenhoeve\Documents\stest3.kml")
def createRoute(request): """ Create Route is one of the main methods in the software because it creates the routes for each drone in the mission. It takes the POST['msg'] information. Then gets the square waypoints grid and divides it in many areas as drones we have. This method is triggered when the operator clicks on the play button. The areas are created and stored in the database and whe it's done we create a KML file/s and stores it in the KML_DIR path under the IBRI folder. @param request: Request petition that is handled by Django @type request: Django request object @return: Http response (JSON) @requires: utils.tsp @see: HttpResponse @see: AESCipher @see: JSON @see: csrf_extemp @see: WayPoint @see: Route @see: KML @see: utils.tsp.py @note: If you want to see how createRoute works, click on see source code. """ if(request.method == 'POST'): # Get the preshared key and decrypt the post message. preshared = u'' + PRESHAREDKEY dec = json.loads(AESCipher(preshared).decrypt(request.POST['msg'])) # Loads the json and stores the values in each variable base = json.loads(dec['base']) # Base point coords = json.loads(dec['wayPoints']) # Waypoints (lat, lng) nearPoint = json.loads(dec['nearPoint']) # NearPoint userPK = json.loads(dec['insearch']) # Users in search altitude = json.loads(dec['altitude']) # Altitude # Number of areas nAreas = len(coords) totalElements = 0 for i in coords: totalElements += len(i) size = int(sqrt(totalElements)) nElements = int(math.floor(math.sqrt(len(coords[0])))) searchArea = [[[] for _ in range(nElements)] for _ in range(nAreas)] ov1 = 0 ov2 = 0 # Area divison for i in range(nAreas): for j in range(nElements): col = j * size if(ov1 == 0): searchArea[i][j] = coords[i][(col-ov2):(col-ov2) + size] else: searchArea[i][j] = coords[i][0:ov1] ov2 = size - ov1 ov1 = 0 if(len(searchArea[i][j]) == 0): searchArea[i].pop() j -= 1 if(size - len(searchArea[i][j]) > 0): ov1 = size - len(searchArea[i][j]) ov2 = 0 else: ov1 = 0 ov2 = 0 coord = [] x = 0 # Assign coordenates to each area for i in range(nAreas): coord.append([]) for j in searchArea[i]: for k in j: coord[i].append([(x / size), (x % size)]) x += 1 ac = 0 route = [] # Create an optimal route using the A* algorithm following the TSP for i in range(nAreas): n, D = mk_matrix(coord[i], distL2) tour = nearest_neighbor(n, (((nearPoint[i][0] * size)-ac) + nearPoint[i][1]), D) for j in range(len(tour)): tmpValue = tour[j] + (size - len(searchArea[i][0])) col = tmpValue / size if col == 0 and i > 0: row = (tmpValue % size) - (size - len(searchArea[i][0])) if DEBUG: print(str(tour[j]) + " = " + str(col) + "," + str((tmpValue % size) - (size - len(searchArea[i][0])))) else: row = tmpValue % size if DEBUG: print(str(tour[j])+" = "+str(col)+","+str(tmpValue % size)) tour[j] = searchArea[i][col][row] ac += len(coord[i]) route.append(tour) m = Mission() m.save() for userId in range(0, len(userPK)): m.inSearch.add(Clients.objects.get(pk=userPK[userId])) try: drones = Drone.objects.all()[0] except: print colored("Oops! No drones found un database", 'red') return HttpResponse('configerror') colorArray = [ 'ff00008b', # darkred 'ff8b0000', # darkblue 'ff006400', # darkgreen 'ff008cff', # darkorange 'ff9314ff', # darkpink 'ffff0000', # blue 'ff2fffad', # greenyellow 'ff5c5ccd', # indianred 'ffcbc0ff', # pink 'ffe16941', # royalblue 'ff00ffff', # yellow ] drone_secuencial = 0 # Creates the KML file from os import mkdir for r in route: rm = Route(mission=m, drone=Drone.objects.all()[drone_secuencial], baseLat=base[0], baseLng=base[1], initialWp=0) rm.save() tmpRoute = [] tmpCounter = 0 kmlName = 'IBRI' + str(m.id) + 'R' + str(rm.id) kml = Kml(name=kmlName) kml.newpoint(name="Base", coords=[(base[1], base[0])]) pnt = kml.newlinestring(name='Route {}'.format(tmpCounter)) coords = [] for wp in r: coords.append((wp[1], wp[0], altitude)) tmpRoute.append(WayPoint(route=rm, lat=wp[0], lng=wp[1], ref=tmpCounter)) tmpCounter += 1 pnt.coords = coords pnt.style.linestyle.width = 6 pnt.style.linestyle.color = colorArray[drone_secuencial % len(colorArray)] pnt.altitudemode = AltitudeMode.relativetoground pnt.lookat.gxaltitudemode = GxAltitudeMode.relativetoseafloor pnt.lookat.latitude = coords[0][0] pnt.lookat.longitude = coords[0][1] pnt.lookat.range = 122 pnt.lookat.heading = 0.063 pnt.lookat.tilt = 0 tmpRoute.append(WayPoint(route=rm, lat=base[0], lng=base[1], ref=tmpCounter)) kml.newpoint(name="Back to base", coords=[(base[1], base[0])]) rm.initialWp = len(tmpRoute)-2 # -2 for the last tmp counter and array 0 position rm.save() WayPoint.objects.bulk_create(tmpRoute) place = KML_DIR + '/IBRI' + str(m.id) try: mkdir(place) except Exception as e: print colored(e, 'red') pass place = place + '/' + kmlName + '.kml' place.replace('//', '/') try: kml.save(place) # Saving except Exception as e: print colored(e, 'red') return HttpResponse('errorKmlPath') drone_secuencial += 1 preshared = u'' + PRESHAREDKEY return HttpResponse(AESCipher(preshared).encrypt(json.dumps([m.pk, route])))
pnt.iconstyle.color = 'ffff0000' # Blue pnt.iconstyle.scale = 3 # Icon thrice as big pnt.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/info-i.png' # Culry 'information i # A Point with a styleMap. The Text changes from blue to red on mouse over. pnt = kml.newpoint(name="Kirstenbosch StyleMap", coords=[(18.432314,-33.988862)]) pnt.stylemap.normalstyle.labelstyle.color = 'ffff0000' pnt.stylemap.highlightstyle.labelstyle.color = 'ff0000ff' # A red thick LineString lin = kml.newlinestring(name="Pathway", description="A pathway in Kirstenbosch", coords=[(18.43312,-33.98924), (18.43224,-33.98914), (18.43144,-33.98911), (18.43095,-33.98904)]) lin.linestyle.color = Color.red # Red lin.linestyle.width = 10 # 10 pixels # A Polygon with a hole. Half invisible. pol = kml.newpolygon(name="Atrium Garden", outerboundaryis=[(18.43348,-33.98985), (18.43387,-33.99004262216968), (18.43410,-33.98972), (18.43371,-33.98952), (18.43348,-33.98985)], innerboundaryis=[(18.43360,-33.98982), (18.43386,-33.98995), (18.43401,-33.98974), (18.43376,-33.98962), (18.43360,-33.98982)]) pol.polystyle.color = '990000ff' # Red pol.polystyle.outline = 0 # A Point showing off a BalloonStyle pnt = kml.newpoint(name="BallonStyle", coords=[(18.429191, -33.987286)]) pnt.balloonstyle.text = "These are trees and this text is blue with a green background." pnt.balloonstyle.bgcolor = Color.lightgreen pnt.balloonstyle.textcolor = Color.rgb(0, 0, 255) # Saving kml.save(os.path.splitext(__file__)[0] + ".kml")
# Check if path is valid directory or file if not os.path.exists(args.path): print "Invalid path. Quitting." exit(1) # Handle single JPG file or directory of JPG files if args.directory: files = [os.path.join(args.path, f) for f in os.listdir(args.path) if re.match(r'[A-Za-z0-9-_]+.*\.(jpg|JPG)$', f)] else: files = [ args.path ] ### EXTRACT GPS DATA AND BUILD KML ### kml = Kml(name=args.name) cnt = 0 for fname in files: image = Image.open(fname) exif = extract_gpsinfo_from_image(image) latitude, longitude = get_lat_long(exif['GPSInfo']) print 'Adding %s at %s, %s...' % ( os.path.basename(fname), longitude, latitude ) descr = '%s, %s' % ( longitude, latitude ) kml.newpoint(name=os.path.basename(fname), description=descr, coords=[(longitude, latitude)]) cnt = cnt + 1 kml.save(args.output) #print kml.kml() print '\nSuccessfully parsed %s files and generated %s' % ( cnt, args.output )
#!/usr/bin/python from simplekml import Kml import sys in_file, out_file = sys.argv[1], sys.argv[2]; data = list() kml = Kml(name = in_file, open = 1) with open(in_file) as file: lines = file.readlines() for x in lines: # Ignore lines that start with "//". if x.startswith("//"): continue elements = x.split(",")[:3] group = tuple(elements) data.append(group) path = kml.newlinestring(name = "Flight", description = in_file, coords = data) path.altitudemode = "absolute" path.extrude = 1 path.style.polystyle.color = "7fff575c" kml.save(out_file)
def main(): parser = argparse.ArgumentParser() parser.add_argument("pcap", help="path pcap file") parser.add_argument("-o", "--output", help="Output directory (must already exist)", default="./") parser.add_argument("-d", "--database", help="Database filename", default="sqlite.db") parser.add_argument("-k", "--kml", help="KML filename", default="results.kml") parser.add_argument("-r", "--report", help="Report filename", default="report.txt") args = parser.parse_args() # Check if pcap is a valid file if not os.path.exists(args.pcap): print "Invalid pcap file. Quitting." exit(1) # Check if output path is a valid file if not os.path.exists(args.output): print "Invalid output path. Quitting." exit(1) ### READ PCAP FILE ### pcap = rdpcap(args.pcap) files = extract_jpgs(pcap, args.output) ### INITIALIZE DATABASE ### conn = initdb(os.path.join(args.output,args.database)) ### INITIALIZE KML ### kml = Kml(name=args.kml) for fname in files: ### EXTRACT EXIF DATA ### print "[+] Extracting exif data from " + fname image = Image.open(fname) exif = extract_exif(conn, image) ### GET MD5 ### md5hash = get_md5(os.path.basename(fname)) print "[+] Getting md5 hash for " + os.path.basename(fname) + " => " + md5hash ### INSERT INTO DATABASE ### print "[+] Inserting record into database for " + fname insert_record(conn, os.path.basename(fname), md5hash, simplejson.dumps(exif)) ### WRITE GPS INFO TO KML ### if exif["GPSInfo"]: latitude, longitude = get_lat_long(exif['GPSInfo']) print "[+] Writing GPS info (%s, %s) to KML for %s" % (longitude, latitude, os.path.basename(fname)) descr = '%s, %s' % ( longitude, latitude ) kml.newpoint(name=os.path.basename(fname), description=descr, coords=[(longitude, latitude)]) ### SAVE KML TO FILE ### print "[+] Saving KML file to " + os.path.join(args.output, args.kml) kml.save(os.path.join(args.output, args.kml)) ### GENERATE REPORT ### print "[+] Generating final report as " + os.path.join(args.output, args.report) with open(os.path.join(args.output, args.report), 'w') as r: now = strftime("%Y-%m-%d %H:%M:%S", gmtime()) r.write("<==== Extraction Results ====>\n") r.write("Filename: %s\n" % args.pcap) r.write("Timestamp: %s GMT \n\n" % now) for row in conn.execute("SELECT * FROM results"): r.write("===================\n") r.write("Image: %s\n" % row[0]) r.write("MD5 Hash: %s\n" % row[1]) if row[2] and row[2] != "null": r.write("EXIF data:\n") json = simplejson.loads(row[2]) for i in json: r.write("\t%s: %s\n" % (i, json[i])) if i == "GPSInfo": latitude, longitude = get_lat_long(json['GPSInfo']) r.write("\t%s (translated): %s, %s\n" % (i, latitude, longitude)) else: r.write("No EXIF data found\n") r.write("===================\n\n") conn.close()
def csv_to_kml(input_filename): # open input file csv_file = open(input_filename,'rU') reader = csv.DictReader(csv_file) # preamble input_filename_base, input_filename_ext = os.path.splitext(input_filename) # open output file kml_file = open(input_filename_base + '.kml','w') kml_file.write(r"""<?xml version="1.0" encoding="utf-8" ?> <kml xmlns="http://www.opengis.net/kml/2.2"> """) kml_file.write("<Document><name>%s</name>" % input_filename_base) kml_file.write(r""" <Style id="grid1k"><IconStyle> <Icon> <color>ff0000</color> </Icon> </IconStyle></Style>""") kml_file.write(r""" <Schema name="sample" id="sample"> <SimpleField name="Name" type="string"></SimpleField> <SimpleField name="Description" type="string"></SimpleField> <SimpleField name="GID" type="string"></SimpleField> </Schema> """) gids_unique = set() gids = [] locs_1k = [] # main loop for line in reader: kml_file.write(' <Placemark>\n') kml_file.write(' <name>GID=%s</name>\n' % (line['GID_100m'])) kml_file.write('\t<ExtendedData><SchemaData schemaUrl=\"#sample\">\n') kml_file.write(' <SimpleField name="GID">%s</SimpleField>\n' % (line['GID_100m'])) kml_file.write('\t\t</SchemaData></ExtendedData>\n') kml_file.write(" <Point><coordinates>%s,%s</coordinates></Point>\n" % (line['x'], line['y'])) kml_file.write(' </Placemark>\n') gids_unique.add(line['GID_1k']) gids.append(line['GID_1k']) locs_1k.append([line['x_1k'], line['y_1k']]) # epilogue kml_file.write('\t</Document>\n\t</kml>') csv_file.close() kml_file.close() gids_unique = list(gids_unique) locs_1k_unique = [] for gid in gids_unique: locs_1k_unique.append([locs_1k[k] for k, x in enumerate(map(lambda x: x==gid, gids)) if x][0]) for i, loc in enumerate(locs_1k_unique): kml=Kml() proj_para = "+proj=laea +ellps=WGS84 +lon_0=20 +lat_0=5 +units=m +no_defs" project = pyproj.Proj(proj_para) loc_laea = list(project(loc[0], loc[1])) center_pt = kml.newpoint(name=gids_unique[i], description="1k by 1k grid", coords=[(loc[0], loc[1])]) pol = kml.newpolygon(name="1k grid", description="A pathway in Kirstenbosch", outerboundaryis=[project(loc_laea[0]-500, loc_laea[1]+500, inverse=True), project(loc_laea[0]+500, loc_laea[1]+500, inverse=True), project(loc_laea[0]+500, loc_laea[1]-500, inverse=True), project(loc_laea[0]-500, loc_laea[1]-500, inverse=True), project(loc_laea[0]-500, loc_laea[1]+500, inverse=True)], innerboundaryis=[project(loc_laea[0]-500, loc_laea[1]+500, inverse=True), project(loc_laea[0]+500, loc_laea[1]+500, inverse=True), project(loc_laea[0]+500, loc_laea[1]-500, inverse=True), project(loc_laea[0]-500, loc_laea[1]-500, inverse=True), project(loc_laea[0]-500, loc_laea[1]+500, inverse=True)]) pol.polystyle.color = 'ff0000ff' kml.save("csv/"+gids_unique[i]+".kml")
for lon in lons: for lat in lats: model_results = model.get_point(lat, lon) result_html = convert_to_html(model_results) pnt = fol.newpoint() pnt.name = "CRUST 1.0 (%.1f,%.1f)" % (lat, lon) pnt.description = result_html pnt.coords = [(lon, lat)] pnt.style = style # Save the KML print "Writing KML file..." outfile = 'CRUST_1.0' kml.save('../%s.kml' % outfile) # Try to compress it to KMZ, if we can, the delete the kml, otherwise # we're done and we'll show a message indicating the outcome. zlib is # required for this, otherwise the file size won't be reduced try: import zlib compression = zipfile.ZIP_DEFLATED print "zlib present: will be stored in compressed format" except: compression = zipfile.ZIP_STORED print "zlib not present: will be stored in uncompressed format" with zipfile.ZipFile('../%s.kmz' % outfile, 'w') as zf: print "Zipping KML file to KMZ..." zf.write('../%s.kml' % outfile, compress_type=compression)
# 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') # Create a new track in the folder trk = fol.newgxtrack(name='2010-05-28T01:16:35.000Z') # Apply the above schema to this track trk.extendeddata.schemadata.schemaurl = schema.id # Add all the information to the track trk.newwhen(when) # Each item in the give nlist will become a new <when> tag trk.newgxcoord(coord) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('heartrate', heartrate) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('cadence', cadence) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('power', power) # Ditto # Styling trk.stylemap.normalstyle.iconstyle.icon.href = 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' trk.stylemap.normalstyle.linestyle.color = '99ffac59' trk.stylemap.normalstyle.linestyle.width = 6 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 = '99ffac59' trk.stylemap.highlightstyle.linestyle.width = 8 # Save the kml to file kml.save("GxTrack.kml")
"E0162466025BF373" : "http://maps.gstatic.com/mapfiles/ms2/micons/purple-dot.png"} def sortNneaList(x,y): return int( (x.dateEvent.newTime - y.dateEvent.newTime).total_seconds() ) #warning, this method don care about milli and microseconds nmeaLogs = sorted(nmeaLogs,cmp=sortNneaList) from simplekml import Kml kml = Kml() currentNmeaLogDate = None for nmeaLog in nmeaLogs: if currentNmeaLogDate == None: currentNmeaLogDate = nmeaLog.dateEvent.newTime elif nmeaLog.dateEvent.newTime.day != currentNmeaLogDate.day or nmeaLog.dateEvent.newTime.month != currentNmeaLogDate.month or nmeaLog.dateEvent.newTime.year != currentNmeaLogDate.year: kml.save(kmlDirectory+"skidump_"+str(currentNmeaLogDate.day)+"_"+str(currentNmeaLogDate.month)+"_"+str(currentNmeaLogDate.year)+".kml") currentNmeaLogDate = nmeaLog.dateEvent.newTime kml = Kml() tab = [] firstPos = None for position in nmeaLog.NewPosition: if firstPos == None: firstPos = position #ne pas mettre les 0.0 if position.longitude == 0.0 and position.latitude == 0.0: continue tab.append( ( position.longitude,position.latitude) )
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))