[OSM-dev] Downloading OSM data

Pierre Béland pierzenh at yahoo.fr
Fri Nov 8 01:52:55 UTC 2013


Hi  Phanindra,

This is a late answer but I took time to test the solution using the urllib2 module to send a request to a website and extract data. I give you an example with the Overpass API.

Using Overpass is a very interesting solution to extract for a bbox and pass the result to Python. It is possible to output in json and then convert easily to Python dictionary objects.  The python script example below uses the urllib2 module to send a request and read the result. 

The url contains the instructions sent to the Overpass API.  In this example, The nodes that have a name Tag are extracted for the given bbox.

In the dictionary, elements contains the list of elements extracted from OSM. The string function  .decode('utf8') let's decode and print correctly the UTF8 characters.
 
Pierre 


Python script
---------------------------------------------------------------------------------------------------------------------------------------------------

import urllib2,urllib,json
# space replaced with + sign
url='http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];(node[name](45.37301118463942,-72.80742645263672,45.416286468478475,-72.71095275878906););out+body;'

try:
    print "try request"
    overpass_response = urllib2.urlopen(url)
    print "info"
    print overpass_response.info()
    #overpass_response = urllib2.urlopen(url_o)
except urllib2.HTTPError as e:
    # list of error codes at http://docs.python.org/2/howto/urllib2.html
    print('The server couldn\'t fulfill the request.')
    print('Error code: ', e.code, responses[e.code])
    print urllib2.info()
except urllib2.URLError as e:
    print('We failed to reach a server.')
    print('Reason: ', e.reason)
else:
    # everything is fine
    #overpass_response = open(overpass_request)
    #print "apres open"
    overpass_txt=overpass_response.read().decode('utf8')
    overpass_response.close()
    overpass_json = json.loads(overpass_txt)

    print "dictionnary elements - we access and print each element of the list"
    elements=overpass_json["elements"]
    nb=0
    for element in elements:
        nb+=1
        print "\n",nb,"\t",element
        print "\t"+element["type"]+", id="+str(element["id"])+", lat="+str(element["lat"])+", lon="+str(element["lon"])
        if ("name" in element["tags"]):
            print "\tname="+element["tags"]["name"]




________________________________
 De : Phanindra Kuchipudi <phani.800 at gmail.com>
À : dev at openstreetmap.org 
Envoyé le : Lundi 4 novembre 2013 0h26
Objet : [OSM-dev] Downloading OSM data
 


Hi,

I want to write a python script to download OSM data based on a given bounding box (implementing XAPI). 
Can anyone help me how to develop this script, and also can you give information on any other scripts available for this downloading functionality?

Thanks,
Phani
_______________________________________________
dev mailing list
dev at openstreetmap.org
https://lists.openstreetmap.org/listinfo/dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20131108/c6c89773/attachment.html>


More information about the dev mailing list