Download a map picture with python

Hi

I would like to generate offline maps for some sets of pictures I have taken.
The pictures exif contain GPS data.

Now I would like to download a simple static map picture which covers a specified area.
e.g. what you see when open https://www.openstreetmap.org/#map=7/39.981/14.381

I tried overpass but not sure how to get a map picture, looks complicated to me.
http://printmaps-osm.de looks good, but not sure how long term available this service is and requires a property tool.

What I want:

  • normal map, with default labeling
  • specified resolution of picture e.g. 800x600px
  • query to adjust the view, best would be a limit for n,s,w,e
  • long term availability to avoid maintaining my script

Is there a simple way to download such an image with python?

Thanks a lot for your help and ideas!

br Lukas

Maybe the API for my maposmatic instance could be of use for you?

https://print.get-map.org/about/api/

I have been running that maposmatic instance for over 5 years now (although the API is a bit younger than that), and don’t have any plans to discontinue it any time soon. But you could also run your own instance, I also provide a Vagrant setup for this on github that you can throw a OSM data extract (or full planet, if you have the resources) in PBF format and it should start up a full working local instance form there: https://github.com/hholzgra/maposmatic-vagrant/

Cool, sounds great.
But if I enter https://api.get-map.org/apis/ to my browser I get “Page not found”

import requests
import shutil
data = {'title': 'Python Test',
    'bbox_bottom':  '52.00',
    'bbox_top': '52.02', 
    'bbox_left' : '8.50',
    'bbox_right' :  '8.52'}
    
r = requests.post("https://api.get-map.org/apis/v1/", json={'json_payload': data}, stream=True)
with open('test', 'wb') as out_file:
    shutil.copyfileobj(r.raw, out_file)
del r

I’m not a coding expert, so maybe I do something completely wrong.

Thanks a lot!

Most basic Python example I can offer for now would be:

#! /usr/bin/python3

import json
import urllib3
from pprint import pprint
from time import sleep

base_url = 'https://api.get-map.org/apis/'

http = urllib3.PoolManager()

def api_call(url, data = None):
    if data is None:    
        r = http.request('GET', url)
    else:
        encoded_data = json.dumps(data).encode('utf-8')

        r = http.request(
            'POST',
            url,
            body = encoded_data,
            headers={'Content-Type': 'application/json'})
        
    return json.loads(r.data.decode('utf-8'))

data = {
    "title": "API Test",
    "osmid": -4220331,
}
    

r = api_call(base_url + 'jobs/', data)

job_id = r['id']

job_status = 0

while job_status < 2:
    sleep(15)
    r = api_call(base_url + 'jobs/%d' % job_id)
    job_status = r['status']

r = http.request('GET',
                 r['files']['png'],
                 preload_content=False)

with open('result.png', 'wb') as out:
    while True:
        data = r.read(4096)
        if not data:
            break
        out.write(data)

r.release_conn()

I just now used it to produce https://print.get-map.org/maps/184538

Thank you for this code. It works great.

Still is there a way to speed the upload of the map ? On my machine it takes like 40-50s to load the map. I do not know if this is normal. High fidelity is not so important in my use case.

Thanks

Hello Everyone,

Since a few weeks when running the same code as you presented, I get the following error

{'error': {'indexer': ['This field cannot be blank.']}}

I cannot find the reason for the issue. Could you please help me find out what’s happening ?

Thank you