You are not logged in.

Announcement

*** NOTICE: forum.openstreetmap.org is being retired. Please request a category for your community in the new ones as soon as possible using this process, which will allow you to propose your community moderators.
Please create new topics on the new site at community.openstreetmap.org. We expect the migration of data will take a few weeks, you can follow its progress here.***

#1 2021-09-24 20:08:53

Lukas1000
Member
Registered: 2015-07-14
Posts: 4

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

Offline

#2 2021-09-25 08:37:42

Hartmut Holzgraefe
Member
Registered: 2015-04-13
Posts: 263

Re: Download a map picture with python

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/

Offline

#3 2021-09-25 09:41:44

Lukas1000
Member
Registered: 2015-07-14
Posts: 4

Re: Download a map picture with python

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!

Last edited by Lukas1000 (2021-09-25 09:43:27)

Offline

#4 2021-09-25 10:30:47

Hartmut Holzgraefe
Member
Registered: 2015-04-13
Posts: 263

Re: Download a map picture with python

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

Offline

#5 2022-06-02 15:49:10

Alaveau
New Member
Registered: 2022-06-02
Posts: 2

Re: Download a map picture with python

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

Offline

#6 2022-10-11 15:56:58

Alaveau
New Member
Registered: 2022-06-02
Posts: 2

Re: Download a map picture with python

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

Offline

Board footer

Powered by FluxBB