Package: py-staticmaps
py-staticmapsA python module to create static map images (PNG, SVG) with markers, geodesic lines, etc.FeaturesMap objects: pin-style markers, image (PNG) markers, polylines, polygons, (geodesic) circlesAutomatic computation of best center + zoom from the added map objectsSeveral pre-configured map tile providersProper tile provider attributions displayOn-disc caching of map tile images for faster drawing and reduced load on the tile serversNon-anti-aliased drawing viaPILLOWAnti-aliased drawing viapycairo(optional; only ifpycairois installed properly)SVG creation viasvgwriteInstallationSVG + non-anti-aliased PNG versionpipinstallpy-staticmapsSVG + anti-aliased PNG version (via Cairo)pipinstallpy-staticmaps[cairo]py-staticmapsusespycairofor creating anti-aliased raster-graphics, so make surelibcairo2is installed on your system (on Ubuntu just install thelibcairo2-devpackage, i.e.sudo apt install libcairo2-dev).ExamplesNote: PNG support (e.g.context.render_cairo(...)) is only available if thepycairomodule is installed.Markers and Geodesic Linesimportstaticmapscontext=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 non-anti-aliased pngimage=context.render_pillow(800,500)image.save("frankfurt_newyork.pillow.png")# render anti-aliased png (this only works if pycairo is installed)image=context.render_cairo(800,500)image.write_to_png("frankfurt_newyork.cairo.png")# render svgsvg_image=context.render_svg(800,500)withopen("frankfurt_newyork.svg","w",encoding="utf-8")asf:svg_image.write(f,pretty=True)Transparent Polygonsimportstaticmapscontext=staticmaps.Context()context.set_tile_provider(staticmaps.tile_provider_OSM)freiburg_polygon=[(47.96881,7.79045),(47.96866,7.78610),(47.97134,7.77874),...]context.add_object(staticmaps.Area([staticmaps.create_latlng(lat,lng)forlat,lnginfreiburg_polygon],fill_color=staticmaps.parse_color("#00FF003F"),width=2,color=staticmaps.BLUE,))# render non-anti-aliased pngimage=context.render_pillow(800,500)image.save("freiburg_area.pillow.png")# render anti-aliased png (this only works if pycairo is installed)image=context.render_cairo(800,500)image.write_to_png("freiburg_area.cairo.png")# render svgsvg_image=context.render_svg(800,500)withopen("freiburg_area.svg","w",encoding="utf-8")asf:svg_image.write(f,pretty=True)Drawing a GPX Track + Image Marker (PNG)importsysimportgpxpyimportstaticmapscontext=staticmaps.Context()context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)withopen(sys.argv[1],"r")asfile:gpx=gpxpy.parse(file)fortrackingpx.tracks:forsegmentintrack.segments:line=[staticmaps.create_latlng(p.latitude,p.longitude)forpinsegment.points]context.add_object(staticmaps.Line(line))forpingpx.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 non-anti-aliased pngimage=context.render_pillow(800,500)image.save("draw_gpx.pillow.png")# render anti-aliased png (this only works if pycairo is installed)image=context.render_cairo(800,500)image.write_to_png("draw_gpx.cairo.png")US State Capitalsimportjsonimportrequestsimportstaticmapscontext=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_,datainjson.loads(response.text).items():capital=staticmaps.create_latlng(float(data["lat"]),float(data["long"]))context.add_object(staticmaps.Marker(capital,size=5))# render non-anti-aliased pngimage=context.render_pillow(800,500)image.save("us_capitals.pillow.png")# render anti-aliased png (this only works if pycairo is installed)image=context.render_cairo(800,500)image.write_to_png("us_capitals.cairo.png")Geodesic Circlesimportstaticmapscontext=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 non-anti-aliased pngimage=context.render_pillow(800,500)image.save("geodesic_circles.pillow.png")# render anti-aliased png (this only works if pycairo is installed)image=context.render_cairo(800,600)image.write_to_png("geodesic_circles.cairo.png")Other ExamplesPlease take a look at the command line program which uses thestaticmapspackage:staticmaps/cli.pyDependenciespy-staticmapsusesPILLOWfor rendering raster-graphicspycairofor rendering antialiased raster-graphics (optional!)svgwritefor writing SVG filess2spherefor geo coordinates handlinggeographiclibfor geodesic computationsappdirsfor finding the user's default cache directoryrequestsfor downloading tile filesLicenseMIT© 2020-2021 Florian Pigorsch