Last active
April 30, 2019 13:26
-
-
Save ritvikmath/94155b3a753c10574ac5e9028f82a05a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #manipulate complex shapes | |
| from shapely.geometry import Polygon, MultiPolygon | |
| #manipulate json objects | |
| import json | |
| #open up the US States Geojson | |
| with open('us_states.geojson') as f: | |
| states = json.load(f) | |
| #initialize a dictionary to store the state shape info | |
| statePolygons = {} | |
| #for each state ... | |
| for stateInfo in states['features']: | |
| #get the state name | |
| stateName = stateInfo['properties']['NAME'] | |
| #the state geography is either a Polygon (some shape) or a MultiPolygon (a collection of polygons) | |
| stateGeographyType = stateInfo['geometry']['type'] | |
| #if it's a Polygon, convert the coordinates to a shapely Polygon object | |
| if stateGeographyType == 'Polygon': | |
| stateGeometry = Polygon(stateInfo['geometry']['coordinates'][0]) | |
| #if its a MultiPolygon, convert each contained polygon into a shapely Polygon object ... | |
| #and then store the list of Polygons in a shapely MultiPolygon object | |
| elif stateGeographyType == 'MultiPolygon': | |
| polygonsInMultipolygon = [Polygon(p[0]) for p in stateInfo['geometry']['coordinates']] | |
| stateGeometry = MultiPolygon(polygonsInMultipolygon) | |
| #store the state geography info in the dictionary | |
| statePolygons[stateName] = stateGeometry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment