Accessing vector geometry in shapefiles using Shapely and Fiona

Using Fiona, you can open a shapefile and access attribute data, such as geometries. For example, our Natural Earth dataset contains a shapefile with all of the states in the US with their vector geometries. Use the following code to open the shapefile and get all of the vector geometry of the first feature (starting from index number 0):

In:   import pprint, fiona      with fiona.open\                   (r"C:\data\gdal\NE\110m_cultural\ne_110m_admin_1_states_provinc             es.shp") as src:      pprint.pprint(src[0])

We can use the shape method and pass in all of the coordinates from Minnesota:

In:   from shapely.geometry import shape      minnesota = {'type': 'Polygon', 'coordinates':  [[(-89.61369767938538, 47.81925202085796), ...

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.