Fiona 访问 shapefile 中多个多边形的坐标
Fiona accessing coordinates of multiple polygons in a shapefile
我正在处理一个包含 4 个多边形的 shapefile,我需要在其中提取它们在元组或 numpy 数组中的坐标以供以后操作。虽然 fiona 识别了 4 个多边形,但使用 shapely 获取面积只给出了第一个多边形的值。
我想为所有多边形选择xy坐标,并将它们分别放入数组和元组中进行处理。任何帮助将不胜感激。
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)
执行结果如下:
4
面积:: 483.6379565269977
试试这个:
import fiona
import numpy as np
c = fiona.open('myShapeFile.shp')
coords = [np.array(poly['geometry']['coordinates'])
for poly in c.values()]
coords
是一个 4 项列表,其中包含根据 shapefile 中的多边形坐标构建的 numpy 数组:
[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]])]
我正在处理一个包含 4 个多边形的 shapefile,我需要在其中提取它们在元组或 numpy 数组中的坐标以供以后操作。虽然 fiona 识别了 4 个多边形,但使用 shapely 获取面积只给出了第一个多边形的值。
我想为所有多边形选择xy坐标,并将它们分别放入数组和元组中进行处理。任何帮助将不胜感激。
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)
执行结果如下:
4
面积:: 483.6379565269977
试试这个:
import fiona
import numpy as np
c = fiona.open('myShapeFile.shp')
coords = [np.array(poly['geometry']['coordinates'])
for poly in c.values()]
coords
是一个 4 项列表,其中包含根据 shapefile 中的多边形坐标构建的 numpy 数组:
[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]])]