• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A python module to create static map images with markers, geodesic lines, etc.

CI PyPI Package Format License MIT

py-staticmaps

A python module to create static map images (PNG, SVG) with markers, geodesic lines, etc.

Features

  • Map objects: pin-style markers, image (PNG) markers, polylines, polygons, (geodesic) circles
  • Automatic computation of best center + zoom from the added map objects
  • Several pre-configured map tile providers
  • Proper tile provider attributions display
  • On-disc caching of map tile images for faster drawing and reduced load on the tile servers
  • Non-anti-aliased drawing via PILLOW
  • Anti-aliased drawing via pycairo (optional; only if pycairo is installed properly)
  • SVG creation via svgwrite

Installation

SVG + non-anti-aliased PNG version

pip install py-staticmaps

SVG + anti-aliased PNG version (via Cairo)

pip install py-staticmaps[cairo]

py-staticmaps uses pycairo for creating anti-aliased raster-graphics, so make sure libcairo2 is installed on your system (on Ubuntu just install the libcairo2-dev package, i.e. sudo apt install libcairo2-dev).

Examples

Note: PNG support (e.g. context.render_cairo(...)) is only available if the pycairo module is installed.

Markers and Geodesic Lines

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 non-anti-aliased png
image = 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 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)

franfurt_newyork

Transparent Polygons

import staticmaps

context = 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) for lat, lng in freiburg_polygon],
        fill_color=staticmaps.parse_color("#00FF003F"),
        width=2,
        color=staticmaps.BLUE,
    )
)

# render non-anti-aliased png
image = 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 svg
svg_image = context.render_svg(800, 500)
with open("freiburg_area.svg", "w", encoding="utf-8") as f:
    svg_image.write(f, pretty=True)

draw_gpx

Drawing a GPX Track + Image Marker (PNG)

import sys

import gpxpy
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))

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 non-anti-aliased png
image = 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")

draw_gpx

US State Capitals

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 non-anti-aliased png
image = 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")

us_capitals

Geodesic Circles

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 non-anti-aliased png
image = 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")

geodesic_circles

Other Examples

Please take a look at the command line program which uses the staticmaps package: staticmaps/cli.py

Dependencies

py-staticmaps uses

  • PILLOW for rendering raster-graphics
  • pycairo for rendering antialiased raster-graphics (optional!)
  • svgwrite for writing SVG files
  • s2sphere for geo coordinates handling
  • geographiclib for geodesic computations
  • appdirs for finding the user's default cache directory
  • requests for downloading tile files

License

MIT Β© 2020-2021 Florian Pigorsch

More Repositories

1

GpxTrackPoster

Create a visually appealing poster from your GPX tracks
Python
358
star
2

go-staticmaps

A go (golang) library and command line tool to render static map images using OpenStreetMap tiles.
Go
312
star
3

go-findfont

A platform-agnostic go (golang) library to easily locate truetype font files in your system's user and system font directories
Go
106
star
4

FloppsMap

Online map with draggable markers, waypoint projection, geocaches, ...
JavaScript
27
star
5

activities

Your self-hosted activities overview (running, cycling, ...). Synced with Strava.
JavaScript
26
star
6

invisible-characters

A static website abou invisible Unicode characters
HTML
18
star
7

RandomWallpapers

Random geometric wallpaper generator
Python
14
star
8

unicode

A Flask-Based Web-App for Exploring Unicode
Python
11
star
9

DesktopJodel

A inofficial, Qt-based desktop client for the Jodel chat protocol
C++
11
star
10

alcazar-gen

SAT-based generator for Alcazar puzzles
C++
10
star
11

airports

Fullscreen satellite map randomly showing the world's airports
Python
8
star
12

QtAsciimage

QtAsciimage - Asciimage for Qt Applications
C++
7
star
13

fritz-switch-profiles

A (Python) script to remotely set device profiles of an AVM Fritz!Box
Python
7
star
14

dotter

Qt based viewer for GraphViz (.dot) files
C++
6
star
15

StravaExportToGPX

Convert the activites in a Strava export to GPX files
Python
6
star
16

map

TypeScript
5
star
17

exposure

A Leaflet-based heatmap showing exposure notification beacons
HTML
5
star
18

go-coordsparser

A library for parsing (geographic) coordinates in go (golang)
Go
4
star
19

QtDateTimeEditor

A user and keyboard friendly alternative to Qt's QDateTimeEdit
C++
4
star
20

pyqt5-cpp-widgets

PyQt5, Python3 and Custom C++ Widgets
Python
3
star
21

aoc2021

My solutions for Advent of Code 2021.
Go
2
star
22

camelsplit

Camel case aware word splitting.
Python
2
star
23

coordinates-toolbox

A single page web-app providing several (geographic) coordinate calculations
JavaScript
2
star
24

gpx-map

Online Map displaying GPX tracks and makers (client side only!)
JavaScript
2
star
25

RunalyzeVagrant

Vagrant configuration for a Runalyze development environment
Shell
2
star
26

mantis-enhancer-user-script

User script enhancing Mantis BT
JavaScript
1
star
27

gol-sat

A SAT-based forward/backwards solver for Conway's "Game of Life".
C++
1
star
28

tracks

A static map of your FIT/GPX tracks
Python
1
star
29

overpass-poi-map

Leaflet map with POIs from the Overpass API
JavaScript
1
star
30

git-repo-backup

Backup a git repository using rsync
Shell
1
star
31

aoc2022

Go solutions for Advent of Code 2022 (aoc2022)
Go
1
star
32

aoc2020

My solutions for Advent of Code 2020
Python
1
star
33

aoc2016

My solutions for 'Advent of Code' 2016
C++
1
star
34

ocdl.py

Python script for downloading opencaching.de's "saved queries"
Python
1
star
35

parkrun-milestones

Determine milestone candidates for a parkrun event
Go
1
star
36

whataweek-mastodon

Go
1
star
37

go-getfile

Go module to download files
Go
1
star