Skip to content

Instantly share code, notes, and snippets.

@thampiman
Last active January 30, 2017 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thampiman/6244464c6ac8705f57a9e02225be6a04 to your computer and use it in GitHub Desktop.
Save thampiman/6244464c6ac8705f57a9e02225be6a04 to your computer and use it in GitHub Desktop.
import json
from shapely.geometry import shape, Point
def load_geojson(filename):
"""Function to load GeoJSON"""
with open(filename) as f:
js = json.loads(f)
return js
def reverse_geocode(point, js):
"""Function to reverse geocode"""
for feature in js['features']:
# Create polygon shape
polygon = shape(feature['geometry'])
# Check if point is contained in polygon
if polygon.contains(point):
# If found, return region name
return feature['properties']['name']
# If not found, return None
return None
def main():
js = load_geojson('<geojson_filename.py>')
longitude = -0.103457
latitude = 51.521683
point = Point(longitude, latitude)
region_name = reverse_geocode(point, js)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment