def plot_nine(h, tileprovider=tp, size=(800, 500)): context = Context() context.set_tile_provider(tileprovider) context.add_hash_poly(h, fill_color=staticmaps.parse_color('#00FF010F'), width=5, color=staticmaps.BLUE) context.add_neighbor_hash_polys( h, fill_color=staticmaps.parse_color('#00FF003F'), width=2, color=staticmaps.BLUE) return context.render_pillow(*size)
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 plot_cluster(cluster, tileprovider=tp, size=(800, 500)): context = Context() context.set_tile_provider(tileprovider) context.add_cluster(cluster, fill_color=staticmaps.parse_color('#00FF003F'), width=2, color=staticmaps.BLUE) return context.render_pillow(*size)
def plot_super_cluster(hashes, tileprovider=tp, size=(800, 500)): context = Context() context.set_tile_provider(tileprovider) for h in hashes: context.add_hash_poly(h, fill_color=staticmaps.parse_color('#00FF003F'), width=2, color=staticmaps.BLUE) return context.render_pillow(*size)
def test_parse_color() -> None: bad = [ "", "aaa", "midnightblack", "#123", "#12345", "#1234567", ] for s in bad: with pytest.raises(ValueError): staticmaps.parse_color(s) good = [ "0x1a2b3c", "0x1A2B3C", "#1a2b3c", "0x1A2B3C", "0x1A2B3C4D", "black", "RED", "Green", "transparent" ] for s in good: staticmaps.parse_color(s)
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
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([], [], color=color,
(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 svg_image = context.render_svg(800, 500)
def main() -> None: args_parser = argparse.ArgumentParser(prog="createstaticmap") args_parser.add_argument( "--center", metavar="LAT,LNG", type=str, ) args_parser.add_argument( "--zoom", metavar="ZOOM", type=int, ) args_parser.add_argument( "--width", metavar="WIDTH", type=int, required=True, ) args_parser.add_argument( "--height", metavar="HEIGHT", type=int, required=True, ) args_parser.add_argument( "--background", metavar="COLOR", type=str, ) args_parser.add_argument( "--marker", metavar="LAT,LNG", type=str, action="append", ) args_parser.add_argument( "--line", metavar="LAT,LNG LAT,LNG ...", type=str, action="append", ) args_parser.add_argument( "--area", metavar="LAT,LNG LAT,LNG ...", type=str, action="append", ) args_parser.add_argument( "--bounds", metavar="LAT,LNG LAT,LNG", type=str, ) args_parser.add_argument( "--tiles", metavar="TILEPROVIDER", type=str, choices=staticmaps.default_tile_providers.keys(), default=staticmaps.tile_provider_OSM.name(), ) args_parser.add_argument( "--file-format", metavar="FORMAT", type=FileFormat, choices=FileFormat, default=FileFormat.GUESS, ) args_parser.add_argument( "filename", metavar="FILE", type=str, nargs=1, ) args = args_parser.parse_args() context = staticmaps.Context() context.set_tile_provider(staticmaps.default_tile_providers[args.tiles]) if args.center is not None: context.set_center(staticmaps.parse_latlng(args.center)) if args.zoom is not None: context.set_zoom(args.zoom) if args.background is not None: context.set_background_color(staticmaps.parse_color(args.background)) if args.area: for coords in args.area: context.add_object( staticmaps.Area(staticmaps.parse_latlngs(coords))) if args.line: for coords in args.line: context.add_object( staticmaps.Line(staticmaps.parse_latlngs(coords))) if args.marker: for coords in args.marker: context.add_object( staticmaps.Marker(staticmaps.parse_latlng(coords))) if args.bounds is not None: context.add_bounds(staticmaps.parse_latlngs2rect(args.bounds)) file_name = args.filename[0] if determine_file_format(args.file_format, file_name) == FileFormat.PNG: image = context.render_cairo(args.width, args.height) image.write_to_png(file_name) else: svg_image = context.render_svg(args.width, args.height) with open(file_name, "w", encoding="utf-8") as f: svg_image.write(f, pretty=True) print(f"wrote result image to {file_name}")