1
votes

I am working on a shapefile that contains 4 polygons where I need to extract their coordinates in a tuple or numpy array for later manipulation. While fiona identifies the 4 polygons, using shapely to get the area only gives me the value for the first polygon.

I would like to pick the xy coordinate for all polygons and put them separately into arrays and tuples for processing. Any help would be appreciated.

import fiona
import pprint
from shapely.geometry import shape, Polygon

c = fiona.open("myShapeFile.shp", 'r')
pprint.pprint(len(c))
poly = Polygon(next(iter(c))["geometry"]["coordinates"][:])
print('Area  :: ',poly.area)

The execution gives me the following:

4

Area :: 483.6379565269977

1
I'm not completely sure what you're asking, but does building the polygon like this help? Polygon([p['geometry']['coordinates'] for p in c])Milan Cermak
Hi Milan. thanks for your comment, but using your the loop as you suggest gives me an error. I am expecting the code above to give me 4 areas for the contained polygons in the shapefile. What I get now is the afrea of the first polygon. I don't know how to access the coordinates in an array too.PolarXYZ
Can you share the shapefile?Milan Cermak
You also need to upload the .shx file, otherwise fiona.open fails.Milan Cermak
The shapefile you shared contains only one feature (i.e. len(c) == 1). However, you can iterate through the c object as you would a dict: for k, v in c.iteritems() and build polygons in the body of the loop: poly = Polygon(v['geometry']['coordinates']). HTHMilan Cermak

1 Answers

2
votes

Try this:

import fiona
import numpy as np

c = fiona.open('myShapeFile.shp')
coords = [np.array(poly['geometry']['coordinates'])
          for poly in c.values()]

coords is a 4 item list holding numpy arrays built from the coordinates of the polygons from the shapefile:

[array([[ 3.67739738e+05,  6.35697708e+06, -1.50000000e+03],
       [ 3.67709140e+05,  6.35875947e+06, -1.50000000e+03],
       [ 3.55633222e+05,  6.35932405e+06, -1.50000000e+03],
       [ 3.55340980e+05,  6.35724482e+06, -1.50000000e+03],
       [ 3.54012992e+05,  6.35713924e+06, -1.50000000e+03],
       [ 3.53169984e+05,  6.33306237e+06, -1.50000000e+03],
       [ 3.72936914e+05,  6.33219322e+06, -1.50000000e+03],
       [ 3.73638262e+05,  6.34799928e+06, -1.50000000e+03],
       [ 3.70536492e+05,  6.34865912e+06, -1.50000000e+03],
       [ 3.71030683e+05,  6.35672004e+06, -1.50000000e+03],
       [ 3.67739738e+05,  6.35697708e+06, -1.50000000e+03]]),
 array([[ 3.60619254e+05,  6.35486104e+06, -1.50000000e+03],
       [ 3.61702258e+05,  6.35460318e+06, -1.50000000e+03],
       [ 3.62578977e+05,  6.35362332e+06, -1.50000000e+03],
       [ 3.61805402e+05,  6.35192145e+06, -1.50000000e+03],
       [ 3.60412965e+05,  6.35114787e+06, -1.50000000e+03],
       [ 3.59072102e+05,  6.35171516e+06, -1.50000000e+03],
       [ 3.59020527e+05,  6.35279817e+06, -1.50000000e+03],
       [ 3.60309824e+05,  6.35295288e+06, -1.50000000e+03],
       [ 3.60000391e+05,  6.35408746e+06, -1.50000000e+03],
       [ 3.60619254e+05,  6.35486104e+06, -1.50000000e+03]]),
 array([[ 3.59072102e+05,  6.34439198e+06, -1.50000000e+03],
       [ 3.60928684e+05,  6.34537184e+06, -1.50000000e+03],
       [ 3.62424262e+05,  6.34480455e+06, -1.50000000e+03],
       [ 3.63868270e+05,  6.34361841e+06, -1.50000000e+03],
       [ 3.63146266e+05,  6.34155554e+06, -1.50000000e+03],
       [ 3.61341258e+05,  6.34036939e+06, -1.50000000e+03],
       [ 3.60258250e+05,  6.34109139e+06, -1.50000000e+03],
       [ 3.61650688e+05,  6.34227754e+06, -1.50000000e+03],
       [ 3.61289688e+05,  6.34356684e+06, -1.50000000e+03],
       [ 3.59897250e+05,  6.34294798e+06, -1.50000000e+03],
       [ 3.57989094e+05,  6.34346370e+06, -1.50000000e+03],
       [ 3.59072102e+05,  6.34439198e+06, -1.50000000e+03]]),
 array([[ 3.69489590e+05,  6.34294798e+06, -1.50000000e+03],
       [ 3.71397742e+05,  6.34103982e+06, -1.50000000e+03],
       [ 3.69438016e+05,  6.33928638e+06, -1.50000000e+03],
       [ 3.67581438e+05,  6.34052411e+06, -1.50000000e+03],
       [ 3.67787723e+05,  6.34222597e+06, -1.50000000e+03],
       [ 3.69489590e+05,  6.34294798e+06, -1.50000000e+03]])]