Skip to content

Instantly share code, notes, and snippets.

@dmofot
Created April 20, 2015 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmofot/dc46e33eb19ac67e0ecf to your computer and use it in GitHub Desktop.
Save dmofot/dc46e33eb19ac67e0ecf to your computer and use it in GitHub Desktop.
Add elevation to points

Add Elevation to Points

This is an example of how to obtain and add elevation to a csv file. If starting with a shapefile, you can convert to CSV using the ogr2ogr command:
ogr2ogr -f CSV bars_sm.csv bars_sm.shp -lco GEOMETRY=AS_XY

Once you have a CSV file, run the python script points_elev.py in the same directory as the CSV file with python points_elev.py. The script requires the python geocoder library to be installed.

The script should write out a new CSV file with an additional elevation column. If no elevation is found, the script will simply add a 0 in the elevation column.

The bars_sm.csv file included in this gist is a small subset of a much larger file, but I have no idea of the original source.

X Y zipcode city state name categories address rating
-88.007191 41.94008 60101 Addison IL American Tap Bar,Eatery 701 W Lake St 3.5
-88.029312 41.949416 60101 Addison IL Bigby's Pour House Bar,Eatery 1700 W Lake St 4.7
-88.000267 41.937169 60101 Addison IL Sal's Beverage World Store 434 W Lake St 4.05
-90.749255 41.200403 61231 Aledo IL La Belle Vie Martini & Spirits Bar,Eatery 205 S College Ave 0
-88.335485 42.164943 60102 Algonquin IL Binny's Beverage Depot Store 844 S Randall Rd 4.45
-88.283965 42.16106 60102 Algonquin IL Nero's Pizza & Pub Bar,Eatery 300 Eastgate Ct 0
-88.27425 42.160313 60102 Algonquin IL R & J Liquors Store 1336 E Algonquin Rd 4.18
-88.303139 42.171416 60102 Algonquin IL Scorched Earth Brewing Company Brewery,Bar 203 Berg St 4.5
-88.338095 42.144806 60102 Algonquin IL Village Vintner Winery & Brewery Brewery,Bar,Eatery 2380 Esplanade Dr 3.45
-89.32433 37.601734 62905 Alto Pass IL Von Jakob Brewery Brewery,Bar,Eatery 230 Highway 127 3.32
import geocoder
import csv
rows = []
fieldnames = ['X', 'Y', 'zipcode', 'city', 'state', 'name', 'categories', 'address', 'rating', 'elevation']
with open('bars_sm.csv') as f:
reader = csv.DictReader(f, delimiter=',')
for line in reader:
g = geocoder.elevation(line['Y'] + ", " + line['X'])
if g.status == 'OK':
result = g.json
result.update(line)
rows.append(result)
else:
result = {'elevation': 0}
result.update(line)
rows.append(result)
with open('bars_elev.csv', 'wb') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames, extrasaction='ignore')
writer.writeheader()
writer.writerows(rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment