def find_route(endpoint, data, color, staticmaps_context=None): node = nodelist[endpoint] while True: old_node = node node_index = [i for i, x in enumerate(data) if x['index'] == node][0] try: node = data[node_index]['pre'] except: break if staticmaps_context is not None: staticmaps_context.add_object( staticmaps.Line([ staticmaps.create_latlng( nodepl[nodelist.index(old_node)][0], nodepl[nodelist.index(old_node)][1]), staticmaps.create_latlng(nodepl[nodelist.index(node)][0], nodepl[nodelist.index(node)][1]) ], color=staticmaps.parse_color(color), width=4)) plt.plot([ nodepl[nodelist.index(old_node)][1], nodepl[nodelist.index(node)][1] ], [ nodepl[nodelist.index(old_node)][0], nodepl[nodelist.index(node)][0] ], c=color)
def test_creation() -> None: staticmaps.Line( [ staticmaps.create_latlng(48, 8), staticmaps.create_latlng(49, 9), staticmaps.create_latlng(50, 8) ], color=staticmaps.YELLOW, )
def test_bounds() -> None: line = staticmaps.Line( [ staticmaps.create_latlng(48, 8), staticmaps.create_latlng(49, 9), staticmaps.create_latlng(50, 8) ], color=staticmaps.YELLOW, ) assert not line.bounds().is_point()
def test_bad_creation() -> None: with pytest.raises(ValueError): staticmaps.Line([]) with pytest.raises(ValueError): staticmaps.Line([staticmaps.create_latlng(48, 8)]) with pytest.raises(ValueError): staticmaps.Line( [staticmaps.create_latlng(48, 8), staticmaps.create_latlng(49, 9)], width=-123)
def make_hash_poly_points(h): b = pygeodesy.geohash.bounds(h) sw = b.latS, b.lonW nw = b.latN, b.lonW ne = b.latN, b.lonE se = b.latS, b.lonE polygon = [sw, nw, ne, se, sw] return [staticmaps.create_latlng(lat, lon) for lat, lon in polygon]
def create_thumbnail(self, output_dir: str) -> None: if not self.track: return os.makedirs(output_dir, exist_ok=True) file_name = os.path.join(output_dir, f"{self.strava_id}.png") context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery) line = [staticmaps.create_latlng(*point) for point in self.track] context.add_object(staticmaps.Line(line)) image = context.render_cairo(800, 500) image.write_to_png(file_name)
def add_cluster(self, cluster, size=6, color=None, fill_color=None, width=2, colors=None): for lat, lon, id, day in zip(cluster.lats, cluster.lons, cluster.ids, cluster.days): if colors is not None: color = colors[id] point = staticmaps.create_latlng(lat, lon) self.add_object(staticmaps.Marker(point, color=color, size=size))
def create_area(*coords): context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_OSM) rectangle = [(coords[0], coords[1]), (coords[2], coords[1]), (coords[2], coords[3]), (coords[0], coords[3]), (coords[0], coords[1])] context.add_object( staticmaps.Area( [staticmaps.create_latlng(lat, lng) for lat, lng in rectangle], fill_color=staticmaps.parse_color("#FFFFFF00"), width=2, color=staticmaps.BLUE, )) return context
def test_creation() -> None: staticmaps.Marker(staticmaps.create_latlng(48, 8), color=staticmaps.YELLOW, size=8)
def test_bounds() -> None: marker = staticmaps.Marker(staticmaps.create_latlng(48, 8)) assert marker.bounds().is_point()
# Copyright (c) 2020 Florian Pigorsch; see /LICENSE for licensing information import sys import gpxpy # type: ignore import staticmaps context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery) with open(sys.argv[1], "r") as file: gpx = gpxpy.parse(file) for track in gpx.tracks: for segment in track.segments: line = [staticmaps.create_latlng(p.latitude, p.longitude) for p in segment.points] context.add_object(staticmaps.Line(line)) # add an image marker to the first track point for p in gpx.walk(only_points=True): pos = staticmaps.create_latlng(p.latitude, p.longitude) marker = staticmaps.ImageMarker(pos, "start.png", origin_x=27, origin_y=35) context.add_object(marker) break # render png via pillow image = context.render_pillow(800, 500) image.save("running.pillow.png") # render png via cairo if staticmaps.cairo_is_supported():
#!/usr/bin/env python # py-staticmaps # Copyright (c) 2020 Florian Pigorsch; see /LICENSE for licensing information import staticmaps context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_StamenToner) center1 = staticmaps.create_latlng(66, 0) center2 = staticmaps.create_latlng(0, 0) context.add_object( staticmaps.Circle(center1, 2000, fill_color=staticmaps.TRANSPARENT, color=staticmaps.RED, width=2)) context.add_object( staticmaps.Circle(center2, 2000, fill_color=staticmaps.TRANSPARENT, color=staticmaps.GREEN, width=2)) context.add_object(staticmaps.Marker(center1, color=staticmaps.RED)) context.add_object(staticmaps.Marker(center2, color=staticmaps.GREEN)) # render png via pillow image = context.render_pillow(800, 600) image.save("geodesic_circles.pillow.png")
def test_bounds() -> None: context = staticmaps.Context() assert context.object_bounds() is None context.add_object(staticmaps.Marker(staticmaps.create_latlng(48, 8))) bounds = context.object_bounds() assert bounds is not None assert bounds.is_point() context.add_object(staticmaps.Marker(staticmaps.create_latlng(47, 7))) assert context.object_bounds() is not None assert context.object_bounds() == s2sphere.LatLngRect( staticmaps.create_latlng(47, 7), staticmaps.create_latlng(48, 8)) context.add_object(staticmaps.Marker(staticmaps.create_latlng(47.5, 7.5))) assert context.object_bounds() is not None assert context.object_bounds() == s2sphere.LatLngRect( staticmaps.create_latlng(47, 7), staticmaps.create_latlng(48, 8)) context.add_bounds( s2sphere.LatLngRect(staticmaps.create_latlng(46, 6), staticmaps.create_latlng(49, 9))) assert context.object_bounds() is not None assert context.object_bounds() == s2sphere.LatLngRect( staticmaps.create_latlng(46, 6), staticmaps.create_latlng(49, 9)) context.add_bounds( s2sphere.LatLngRect(staticmaps.create_latlng(47.5, 7.5), staticmaps.create_latlng(48, 8))) assert context.object_bounds() is not None assert context.object_bounds() == s2sphere.LatLngRect( staticmaps.create_latlng(47, 7), staticmaps.create_latlng(48, 8))
#!/usr/bin/env python # py-staticmaps # Copyright (c) 2020 Florian Pigorsch; see /LICENSE for licensing information import staticmaps context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_StamenToner) frankfurt = staticmaps.create_latlng(50.110644, 8.682092) newyork = staticmaps.create_latlng(40.712728, -74.006015) context.add_object(staticmaps.Line([frankfurt, newyork], color=staticmaps.BLUE, width=4)) context.add_object(staticmaps.Marker(frankfurt, color=staticmaps.GREEN, size=12)) context.add_object(staticmaps.Marker(newyork, color=staticmaps.RED, size=12)) # render png via pillow image = context.render_pillow(800, 500) image.save("frankfurt_newyork.pillow.png") # render png via cairo if staticmaps.cairo_is_supported(): image = context.render_cairo(800, 500) image.write_to_png("frankfurt_newyork.cairo.png") # render svg svg_image = context.render_svg(800, 500) with open("frankfurt_newyork.svg", "w", encoding="utf-8") as f: svg_image.write(f, pretty=True)
(47.96796, 7.81701), (47.96768, 7.81300), (47.96724, 7.81219), (47.96592, 7.80894), (47.96425, 7.80478), (47.96369, 7.80338), (47.96430, 7.80115), (47.96484, 7.79920), (47.96712, 7.79471), (47.96883, 7.79090), (47.96881, 7.79045), ] context.add_object( staticmaps.Area( [staticmaps.create_latlng(lat, lng) for lat, lng in freiburg_polygon], fill_color=staticmaps.parse_color("#00FF007F"), width=2, color=staticmaps.BLUE, )) # render png via pillow image = context.render_pillow(800, 500) image.save("freiburg_area.pillow.png") # render png via cairo if staticmaps.cairo_is_supported(): image = context.render_cairo(800, 500) image.write_to_png("freiburg_area.cairo.png") # render svg
# Copyright (c) 2020 Florian Pigorsch; see /LICENSE for licensing information import json import requests import staticmaps context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_OSM) URL = ( "https://gist.githubusercontent.com/jpriebe/d62a45e29f24e843c974/" "raw/b1d3066d245e742018bce56e41788ac7afa60e29/us_state_capitals.json" ) response = requests.get(URL) for _, data in json.loads(response.text).items(): capital = staticmaps.create_latlng(float(data["lat"]), float(data["long"])) context.add_object(staticmaps.Marker(capital, size=5)) # render png via pillow image = context.render_pillow(800, 500) image.save("us_capitals.pillow.png") # render png via cairo if staticmaps.cairo_is_supported(): image = context.render_cairo(800, 500) image.write_to_png("us_capitals.cairo.png") # render svg svg_image = context.render_svg(800, 500) with open("us_capitals.svg", "w", encoding="utf-8") as f: svg_image.write(f, pretty=True)
def test_render_center_zoom() -> None: context = staticmaps.Context() context.set_tile_downloader(MockTileDownloader()) context.set_center(staticmaps.create_latlng(48, 8)) context.set_zoom(15) context.render_svg(200, 100)
color='w', mfc='#ff1515', mec='#ff1515', marker='o', markersize=8, label='Startpoint') ] colors = [ "#2db60a", "#0ab6b6", "#b60ab1", "#0a37b6", "#b6690a", "#0ab64f", "#0a6fb6", "#740ab6", "#2b6435", "#642b48", "#333" ] context = staticmaps.Context() context.add_object( staticmaps.Marker(staticmaps.create_latlng(startpoint[0], startpoint[1]), color=staticmaps.parse_color("#ff1515"), size=12)) context.add_object( staticmaps.Marker(staticmaps.create_latlng(min_lat, min_long), color=staticmaps.parse_color("#ffffff00"), size=0)) context.add_object( staticmaps.Marker(staticmaps.create_latlng(max_lat, max_long), color=staticmaps.parse_color("#ffffff00"), size=0)) for index, i in enumerate(goal_coordinates): color = colors[index % len(colors)] legend.append( mlines.Line2D([], [],
renderer.group().add(path) renderer.group().add(renderer.drawing().text( self._text, text_anchor="middle", dominant_baseline="central", insert=(x, y - self._arrow - h / 2), font_family="sans-serif", font_size=f"{self._font_size}px", fill="#000000", )) context = staticmaps.Context() p1 = staticmaps.create_latlng(48.005774, 7.834042) p2 = staticmaps.create_latlng(47.988716, 7.868804) p3 = staticmaps.create_latlng(47.985958, 7.824601) context.add_object(TextLabel(p1, "X")) context.add_object(TextLabel(p2, "Label")) context.add_object(TextLabel(p3, "This is a very long text label")) context.set_tile_provider(staticmaps.tile_provider_CartoDarkNoLabels) # render png via pillow image = context.render_pillow(800, 500) image.save("custom_objects.pillow.png") # render png via cairo if staticmaps.cairo_is_supported():
def plot(gps_txt, bt_txt, out_png): context = staticmaps.Context() context.set_tile_provider(staticmaps.tile_provider_OSM) gps_objs = [] with open(gps_txt, 'r') as f: for line in f.readlines(): loaded = json.loads(line) coords = loaded['coords'] latitude = coords['latitude'] longitude = coords['longitude'] latlng = staticmaps.create_latlng(latitude, longitude) marker = staticmaps.Marker(latlng, size=5) context.add_object(marker) timestamp = loaded['timestamp'] ts = timestamp_to_datetime_gps(timestamp) gps_objs.append({ 'ts': ts, 'latitude': latitude, 'longitude': longitude, }) i = 0 with open(bt_txt, 'r') as f: for line in f.readlines(): i += 1 print('progress:', i) loaded = json.loads(line) timestamp = loaded['Timestamp'] ts = timestamp_to_datetime_bt(timestamp) last = None current = None found = None for gps_obj in gps_objs: if gps_obj['ts'] > ts: if last is None: print('bad gps: no previous') else: found = last current = gps_obj break else: last = gps_obj if found is None: print('bad gps: not found!') break device_lat = (last['latitude'] + current['latitude']) / 2.0 device_long = (last['longitude'] + current['longitude']) / 2.0 lat_dist = abs(current['latitude'] - last['latitude']) long_dist = abs(current['longitude'] - last['longitude']) radius = (lat_dist + long_dist) / 2.0 multiplier = 100 latlng = staticmaps.create_latlng(device_lat, device_long) circle = staticmaps.Circle(latlng, radius * multiplier, fill_color=staticmaps.TRANSPARENT, color=staticmaps.BLUE, width=2) context.add_object(circle) image = context.render_cairo(1600, 900) image.write_to_png(out_png)