“The Hitchhiker’s guide to the pick-up locations” is available online

My article “The Hitchhiker’s guide to the pick-up locations” has been published in the Journal of Open Geospatial Data, Software and Standards. It’s an open access journal, so feel free to distribute and share it: http://link.springer.com/article/10.1186/s40965-016-0012-1

Also, I published my travel map using Leaflet library for JavaScript here: http://people.eng.unimelb.edu.au/ovedernikov/travelmap.html

The code is available on GitHub: https://github.com/leshaved/travel_map

Advertisements
“The Hitchhiker’s guide to the pick-up locations” is available online

OSM way handling in Python

As it was mentioned in a previous post, we can get OSM way ID’s using Overpass API. Later, we can make a direct query to OSM server get an XML file with all information about the way.

For easy processing XML, I would recommend xmltodict library, which basically converts XML file to Python dictionary.

For HTTP requests, we will use urlib2 library.

Consider we have a list of way ID’s, and we want to print all tags (key-value pairs) together with coordinates of all nodes. The following code will perform this task:

from urllib2 import urlopen
from time import sleep
from sys import exc_info
from xmltodict import parse

ids = [143915511,33263784, 127123880]
for wayid in ids:
    try:
        response = urlopen('http://www.openstreetmap.org/api/0.6/way/{0}/full'.format(wayid))
        s = response.read()
        d = parse(s)
        way_nodes_coord = d['osm']['node']
        for j in way_nodes_coord:
            print 'lat = {0}, lon = {1}'.format(float(j['@lat']),float(j['@lon']))
        for tag in d['osm']['way']['tag']:
            print u'key = {0}, value = {1}'.format(tag['@k'], tag['@v'])
        sleep(1)
    except:
        print 'Got error for location {0}'.format(wayid)
OSM way handling in Python

How to get nearest roads from Open Street Map in Python

Suppose we have coordinates of locations, and we need to download closest roads to each of them. Particularly, this problem may be interesting to determine where points are on a road network, and then use graph algorithms, for example shortest path. Also, assigning a Point of Interest (POI) to the nearest road is a common query in Geographic Information Systems.

As a widespread free collaborative mapping solution, OpenStreetMap (OSM) contains various types of roads, from motorways and trunks to service and residential ways. Usually, the tag “highway” is used.  All important information is given about roads and streets in OSM can be found here. OSM for the whole planet or specific geographic regions can be downloaded from here; however, it may require substantial storage space and time to process.

To solve the problem, we will use Overpass API, which allow to query OSM data from servers. To process it, we need to install Overpass wrapper which helps us to process API queries in Python.

For the simplicity, let’s just download all roads which are closer than 50 meters to each of the point. Once we have to list of roads, we can easily process them in Python: for example, filter only motorways, or find the closest one etc. We’ll find road IDs, which can be handled later.

Once we have road IDs, we can have access to them using this link. In the next post, I’ll show you how to handle OSM pages like this.

Similar query can request only railroads (key “railway”) or bicycle lanes (key “cycleway”)

from overpass import API
from time import sleep
from sys import exc_info

api = API()
 
locations=[(50.406029,30.619727),(27.988056, 86.925278)]
for loc in locations:
    xx,yy=loc[0],loc[1]
    try:
        response = api.Get('way["highway"](around:50,{0},{1});'.format(loc[0],loc[1])) 
        print "For location with coordinates {0}, {1} found way IDs:".format(loc[0],loc[1])
        if len(response['elements'])>0:
            for way in response['elements']:
                print(way['id'])
        else:
            print "No ways"
        sleep(1)
    except:
        print "Got error: {0}".format(exc_info())[0]

 

How to get nearest roads from Open Street Map in Python

Getting altitude for locations using Google Maps Elevation API in Python

Suppose we have a set of geographical points represented by pairs of coordinates: (latitude, longitude), and we need to get elevation (or altitude) for each of them, i.e. a height above sea level. Even though a pair of (x,y) coordinates is usually representative enough, an altitude could be useful in many spatial data applications: making an elevation profile of a trajectory, estimating precise distances in mountains or ocean depth, river flow management etc.

Generally, most geospatial applications provide only coordinates data, so we need to find the elevation data manually. From a number of solutions to this problem, I decided to choose Google Maps Elevation API, which has thorough documentation about its usage. In a nutshell:

  1. You can perform up to 2500 requests per day with 512 locations per request
  2. You need to create a project in Google Developers Console and get a Server key to be able to make requests

For example, let’s try to use a simple Python code to get elevation for a set of geographical points, using urllib2 for HTTP requests, and JSON format to make it easier to handle the output. You need to paste the Server key into the URL.

from json import loads
from time import sleep
from urllib2 import Request, urlopen

locations=[(27.988056, 86.925278), (50.449561, 30.525366)] #(lat,lon) pairs
for loc in locations: 
    try:
        request = Request('https://maps.googleapis.com/maps/api/elevation/json?locations={0},{1}&key=INSERT_YOUR_KEY_HERE'.format(loc[0],loc[1]))
        response = urlopen(request).read() 
        places = loads(response)
        print 'At {0} elevation is: {1}'.format(loc, places['results'][0]['elevation'])
        sleep(1)
    except:
        print 'Error for location: {0}'.format(loc)

 

 

Getting altitude for locations using Google Maps Elevation API in Python

Hello world

Welcome!

I’m Oleksii (Alex) Vedernikov, and this blog will be mostly about my professional work. I’m doing a PhD in Computer Science at the University of Melbourne, and I’d be happy to share some of my findings.

My area is related to Spatial, or Geographical, Data. If you’re also a fan of maps, data, Python 2.7 and/or algorithms, hopefully you’ll find some useful information about these topics here. Probably, most of my posts would be something that I haven’t found on StackOverflow, so I’m happy to share.

Any feedback is very appreciated.

Hello world