GithubHelp home page GithubHelp logo

komoot / staticmap Goto Github PK

View Code? Open in Web Editor NEW
276.0 18.0 64.0 592 KB

A small, python-based library for creating map images with lines, markers and polygons.

Home Page: https://www.komoot.com

License: Other

Python 100.00%

staticmap's Introduction

Static Map

A small, python-based library for creating map images with lines and markers.

Example

m = StaticMap(300, 400, 10)
m.add_line(Line(((13.4, 52.5), (2.3, 48.9)), 'blue', 3))
image = m.render()
image.save('map.png')

This will create a 300px x 400px map with a blue line drawn from Berlin to Paris.

Map with Line from Berlin to Paris

Installation

StaticMap is a small library, all it takes is python and two python packages: Pillow and request. Install staticmap via:

pip install staticmap

Usage

Create a new map instance:

m = StaticMap(width, height, padding_x, padding_y, url_template, tile_size)
parameter description
width width of the image in pixels
height height of the image in pixels
padding_x (optional) minimum distance in pixel between map features (lines, markers) and map border
padding_y (optional) minimum distance in pixel between map features (lines, markers) and map border
url_template (optional) the tile server URL for the map base layer, e.g. http://a.tile.osm.org/{z}/{x}/{y}.png
tile_size (optional) tile size in pixel, usually 256

Add a line:

line = Line(coordinates, color, width))
m.add_line(line)
parameter description
coordinate a sequence of lon/lat pairs
color a color definition Pillow supports
width the stroke width of the line in pixel
simplify whether to simplify coordinates, looks less shaky, default is true

Add a map circle marker:

marker = CircleMarker(coordinate, color, width))
m.add_marker(marker)
parameter description
coordinate a lon/lat pair: e.g. (120.1, 47.3)
color a color definition Pillow supports
width diameter of marker in pixel

Add a polygon:

polygon = Polygon(coordinates, fill_color, outline_color, simplify)
m.add_polygon(polygon)
parameter description
coordinate a lon/lat pair: e.g. [[9.628, 47.144], [9.531, 47.270], [9.468, 47.057], [9.623, 47.050], [9.628, 47.144]]
fill_color a color definition Pillow supports
outline_color a color definition Pillow supports
simplify whether to simplify coordinates, looks less shaky, default is true

Samples

Show Position on Map

from staticmap import StaticMap, CircleMarker

m = StaticMap(200, 200, url_template='http://a.tile.osm.org/{z}/{x}/{y}.png')

marker_outline = CircleMarker((10, 47), 'white', 18)
marker = CircleMarker((10, 47), '#0036FF', 12)

m.add_marker(marker_outline)
m.add_marker(marker)

image = m.render(zoom=5)
image.save('marker.png')

Position IconMarker on a Map

Show Ferry Connection

from staticmap import StaticMap, Line

m = StaticMap(200, 200, 80)

coordinates = [[12.422, 45.427], [13.749, 44.885]]
line_outline = Line(coordinates, 'white', 6)
line = Line(coordinates, '#D2322D', 4)

m.add_line(line_outline)
m.add_line(line)

image = m.render()
image.save('ferry.png')

Ferry Connection Shown on a Map

Show Icon Marker

from staticmap import StaticMap, IconMarker

m = StaticMap(240, 240, 80)
icon_flag = IconMarker((6.63204, 45.85378), './samples/icon-flag.png', 12, 32)
icon_factory = IconMarker((6.6015, 45.8485), './samples/icon-factory.png', 18, 18)
m.add_marker(icon_flag)
m.add_marker(icon_factory)
image = m.render()
image.save('icons.png')

Ferry Connection Shown on a Map

Alternatives

With papermap there is also a spin-off project dedicated to print maps.

Licence

StaticMap is open source and licensed under Apache License, Version 2.0

The map samples on this page are made with OSM data, © OpenStreetMap contributors

staticmap's People

Contributors

christophlingg avatar lukaswelte avatar lvonlanthen avatar narihira2000 avatar samr1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

staticmap's Issues

Samples not able to download tiles

Hi

I'm attempting to run the samples provided in the README.md with no url_template

The tileserer on http://a.tile.komoot.de/komoot-2/ appear to return 404 for all tiles.

For example this sample:

    m = StaticMap(200, 200, 80)

    coordinates = [[12.422, 45.427], [13.749, 44.885]]
    line_outline = Line(coordinates, 'white', 6)
    line = Line(coordinates, '#D2322D', 4)

    m.add_line(line_outline)
    m.add_line(line)

Raises this error:

    raise RuntimeError("could not download {} tiles: {}".format(len(tiles), tiles))
RuntimeError: could not download 2 tiles: [(16, 11, 'http://a.tile.komoot.de/komoot-2/5/16/11.png'), (17, 11, 'http://a.tile.komoot.de/komoot-2/5/17/11.png')]

Anyone experiencing the same error?

Otherwise I will have to look into another tileserver I suppose :)

Update breaks my production environment

While upgrading the python environment in my production server, I encountered the following fatal error:

  Downloading staticmap-0.5.2.tar.gz
    Complete output from command python setup.py egg_info:
    error in staticmap setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Expected version spec in futures;python_version<"3.2" at ;python_version<"3.2"
$ pip --version
pip 9.0.1 from ~/.virtualenvs/<>/lib/python3.5/site-packages (python 3.5)

In my home server, I only receive the following warning:

Ignoring futures: markers 'python_version < "3.2"' don't match your environment

$ pip --version
pip 9.0.1 from ~/.virtualenvs/<>/lib/python3.4/site-packages (python 3.4)

Add text to map

Hello! I want to add a text to a specific lon/lat. Is this possible?
For example, I want to add a marker with a name under it. For your reference, see the image below.

image

Thank you in advance

When pip installing library in an virtualenv an error message is shown

I am using a virtual environment with python 3 as base and i got the following error message:

(.venv) (test)[vagrant@fedora25-vbox charting_stuff]$ pip install staticmap
Collecting staticmap
  Downloading staticmap-0.5.3.tar.gz
    Complete output from command python setup.py egg_info:
    error in staticmap setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Expected version spec in futures;python_version<"3.2" at ;python_version<"3.2"
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ydv6vuoj/staticmap/ 

It has probably something to do with an syntax error in your requirements.txt and/or setup.py?

I get "ValueError: images do not match"

from staticmap import StaticMap, CircleMarker 
m = StaticMap(800, 600) 
marker = CircleMarker((7.21733093261719, 51.4838951030122), '#0036FF', 12)
m.add_marker(marker)
image = m.render(zoom=14)

python27
staticmap 0.5.4
pillow 6.2.0

ValueError: images do not match

Thank you for your package, which helps my so many efforts.
But, recently When I use this package with my OSM server display the following exception:
ValueError: images do not match

"DLL load failed" error.

Dear all,
I have an interesting problem :)
I'm very happy about the staticmap in my humble Python codes for earthquake mapping.

Recently I've started havin problem importing staticmap. Below is the warning message I receive. I've checked the PIL and found that it is installed. But "ImportError: DLL load failed: The specified module could not be found." does not ignite anything to this folk :)

import staticmap
Traceback (most recent call last):
File "", line 1, in
File "C:\ProgramData\Anaconda3\lib\site-packages\staticmap_init_.py", line 1, in
from .staticmap import StaticMap, CircleMarker, IconMarker, Line, Polygon
File "C:\ProgramData\Anaconda3\lib\site-packages\staticmap\staticmap.py", line 6, in
from PIL import Image, ImageDraw
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 58, in
from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.

Is there anyone out there with a solution/recommendation?

Best regards from Istanbul.

++Ahmet

Feature request: Tile caching

First of all, great library! Intuitive and simple - loving it.

One thing i noticed is that tiles are being loaded when calling the render() method - which makes sense, as we can add elements on the map, which may change the bounding box.

However, the retrieved tiles could be stored in some local user directory for caching, so not every render requests a tile from an online source, which would reduce the server load and the number of misses (i.e. request failed [?]).

Runtime error response

Hi there,
I've been using staticmap for long time in my programs. Your work is very much appreciated.

This morning, I've started having the following response and failed using staticmap.

Raise RuntimeError("could not download {} tiles: {}".format(len(tiles), tiles)) RuntimeError: could not download 4 tiles: [(16, 10, 'http://a.tile.osm.org/5/16/10.png'), (16, 11, 'http://a.tile.osm.org/5/16/11.png'), (17, 10, 'http://a.tile.osm.org/5/17/10.png'), (17, 11, 'http://a.tile.osm.org/5/17/11.png')]

The screenshot for what I see is below.
image

I wonder whether this is a critical issue or not?

Do you have any suggestion?

Cannot install with pip

I use Python 3.5.2 in a virtual environment on a machine with Lubuntu 16.04.6 LTS. I just tried to install staticmap in a brand new virtual environment with

pip install staticmap

and got the following message:

Collecting staticmap
Collecting requests (from staticmap)
  Downloading https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB)
    100% |████████████████████████████████| 61kB 441kB/s 
Collecting futures (from staticmap)
  Using cached https://files.pythonhosted.org/packages/47/04/5fc6c74ad114032cd2c544c575bffc17582295e9cd6a851d6026ab4b2c00/futures-3.3.0.tar.gz
    Complete output from command python setup.py egg_info:
    This backport is meant only for Python 2.
    It does not work on Python 3, and Python 3 users do not need it as the concurrent.futures package is available in the standard library.
    For projects that work on both Python 2 and 3, the dependency needs to be conditional on the Python version, like so:
    extras_require={':python_version == "2.7"': ['futures']}
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-vgfyrmqc/futures/

Custom Tile Server

If we are fetching tiles from a server that requires we pass a Bearer {token} is there a way to pass this to the library so it can correctly supply it in the request?

First example in the README not working when using m.render() with zoom >= 20 (it was working until some weeks ago)

The first example in the README

from staticmap import StaticMap, Line
m = StaticMap(300, 400, 10)
m.add_line(Line(((13.4, 52.5), (2.3, 48.9)), 'blue', 3))
image = m.render()
image.save('map.png')

does not work when m.render() is called using zoom >= 20.
For example this does not work

m = StaticMap(300, 400, 10)
m.add_line(Line(((13.4, 52.5), (2.3, 48.9)), 'blue', 3))
image = m.render(zoom=20)
image.save('map.png')

but this does work

from staticmap import StaticMap, Line
m = StaticMap(300, 400, 10)
m.add_line(Line(((13.4, 52.5), (2.3, 48.9)), 'blue', 3))
image = m.render(zoom=19)
image.save('map.png')

When using zoom >= 20 an error is shown saying request failed [400]: https://a.tile.openstreetmap.org/20/547152/352422.png although it was working until a few weeks ago.
Is the issue related to issue 33? Is there any workaround to this issue?

Negative Latitude Value

Hi,
when I create a map with a large set of cities, the library rase an errore like this one

Traceback (most recent call last):
  File "/xxx/pyxxx.py", line 71, in <module>
    image = m.render(zoom=1)
  File "/usr/local/lib/python2.7/dist-packages/staticmap/staticmap.py", line 268, in render
    extent = self.determine_extent(zoom=self.zoom)
  File "/usr/local/lib/python2.7/dist-packages/staticmap/staticmap.py", line 304, in determine_extent
    y = _lat_to_y(e[1], zoom)
  File "/usr/local/lib/python2.7/dist-packages/staticmap/staticmap.py", line 141, in _lat_to_y
    return (1 - log(tan(lat * pi / 180) + 1 / cos(lat * pi / 180)) / pi) / 2 * pow(2, zoom)
ValueError: math domain error

So, the math domain error is probably related to the log function, that with negative number is mathematically undefined.

How to import the module

I installed the module but I'm not able to use it.
Coud you help me please ?

In [1]: import staticmap

In [2]: staticmap.StaticMap(300,400,10)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-f766ae52cee2> in <module>()
----> 1 staticmap.StaticMap(300,400,10)

AttributeError: 'module' object has no attribute 'StaticMap'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.