Last active
January 30, 2017 12:29
-
-
Save thampiman/6244464c6ac8705f57a9e02225be6a04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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