Posts

Import elevations

Script to import nodes elevations from Google Maps.

# Script to extract elevation data from Google Maps using Google APIs
# By Elad Salomons

# Get your Google API key here: https://console.developers.google.com/apis/api/maps_backend/overview
API_KEY='GET YOUR OWN API KEY FROM GOOGLE'

number_of_coords_per_api_call = 500

from urllib2 import Request, urlopen
import xml.etree.ElementTree as ET

txt=''
n=0
i=0
junctions_list = session.project.junctions.value[:]
for j in junctions_list:
	n=n+1
	lat=str(j.y)
	lon=str(j.x)
	txt=txt+lat + ',' + lon + '|'
	j.elevation=0
	if (n % number_of_coords_per_api_call==0) or (n==len(junctions_list)):
		txt = txt[:-1]
		request = Request('https://maps.googleapis.com/maps/api/elevation/xml?locations=' + txt + '&key=' + API_KEY)
		response = urlopen(request).read()
		tree = ET.fromstring(response)
		k=0
		for ee in tree.iter('elevation'):
			junctions_list[i+k].elevation = ee.text
			k=k+1
		i=i+number_of_coords_per_api_call
		txt=''