пятница, 6 декабря 2013 г.

Google Earth DEM Python parser

There should have been posted a source code of simple Python application that performs:
  1. Google Earth DEM parsing through the Google Elevation API service;
  2. Storing the received elevation geodata locally in shapefiles.
But when this script has been already implemented, I began to doubt if the terms of Google Maps license allow me to do it. Expert said no, so I'am washing my hands of this.

Nevertheless I had to test nice but quite a buggy tool - pyshp, because I used a local shapefile as a cache being appended every time the Google Elevation API passes the new portion of data (daily limit is 2500 requests). In case if you will follow this way, don't forget to make a backup every time :) and also consider this example provided by Joel Lawhead:
import shapefile
# Polygon shapefile we are updating.
# We must include a file extension in
# this case because the file name
# has multiple dots and pyshp would get
# confused otherwise.
file_name = "ep202009.026_5day_pgn.shp"
# Create a shapefile reader
r = shapefile.Reader(file_name)
# Create a shapefile writer
# using the same shape type
# as our reader
w = shapefile.Writer(r.shapeType)
# Copy over the existing dbf fields
w.fields = list(r.fields)
# Copy over the existing dbf records
w.records.extend(r.records())
# Copy over the existing polygons
w._shapes.extend(r.shapes())
# Add a new polygon
w.poly(parts=[[[-104,24],[-104,25],[-103,25],[-103,24],[-104,24]]])
# Add a new dbf record for our polygon making sure we include
# all of the fields in the original file (r.fields)
w.record("STANLEY","TD","091022/1500","27","21","48","ep")
# Overwrite the old shapefile or change the name and make a copy 
w.save(file_name)