如何使用Shapely提取内部多边形坐标? [英] How to extract interior polygon coordinates using Shapely?
问题描述
我是Shapely的新手(但是对此很热心),最近我发现了一些障碍.
I am new to Shapely (but enthusiastic about it), and recently I've discovered a bit of a road bump.
我有一个要通过Fiona读取的多边形shapefile.此shapefile包含多边形和多多边形项,我需要为其内所有坐标的每个要素(即外部和/或内部)构建一个数组.值得注意的是,两个多边形项具有内部环(并且它们是有效的).
I have a polygon shapefile that I am reading in via Fiona. This shapefile contains BOTH polygon and multipolygon items and I need to build an array for each feature of all the coordinates within it (i.e. both exterior and/or interior). Notably, two of the polygon items have interior rings (and they are valid).
访问多边形/多多边形的外部坐标似乎没有问题...但是我没有为内部坐标提取任何东西.
I seem to have no problem accessing the exterior coordinates of the polygon(s)/multipolygon(s) ... but I am not pulling anything for the interior coordinates.
我是否需要在这里采用新方法(即LinearRings)...?
Do I need to take a new approach here (i.e. LinearRings)...?
def convert_polygons(inFile):
for polys in fiona.open(inFile):
myShape = shape(polys['geometry'])
exterior_poly = 0
interior_poly = 0
if isinstance(myShape, Polygon):
print "yes, I am a polygon"
# count how many points for each interior polygon
try:
interior_poly += len(myShape.interior.coords)
except:
pass
# count how many points for each exterior polygon
exterior_poly += len(myShape.exterior.coords)
geomArray = asarray(myShape.exterior)
print geomArray
print "number of interior points in polygon " + str(interior_poly)
print "number of exterior points in polygon " + str(exterior_poly)
elif isinstance(myShape, MultiPolygon):
print "yes, I am a MultiPolygon"
# count how many points for each interior polygon
try:
interior_poly += len(myShape.interior.coords)
except:
pass
try:
# count how many points for each exterior polygon
exterior_poly += len(myShape.exterior.coords)
except:
pass
try:
geomArray = asarray(myShape.interior)
except:
pass
try:
geomArray = asarray(myShape.exterior)
except:
pass
print geomArray
print "number of interior points in polygon " + str(interior_poly)
print "number of exterior points in polygon " + str(exterior_poly)
推荐答案
内圈和外圈的结构不同.对于任何多边形,总会有1个外环,其中零个或多个内环.
Interior and exterior rings are structured differently. For any polygon, there is always 1 exterior ring with zero or more interior rings.
因此,从几何结构来看,exterior
是一个LinearRing
对象,而interiors
是一个零个或多个LinearRing
对象的 list .任何LinearRing
对象都将具有coords
,您可以对其进行切片以使用coords[:]
查看坐标列表.
So looking at the structure of a geometry, exterior
is a LinearRing
object, and interiors
is a list of zero or more LinearRing
objects. Any LinearRing
object will have coords
, which you can slice to see a list of the coordinates with coords[:]
.
以下是返回外部和内部坐标列表的字典的函数:
The following is a function that returns a dict of lists of exterior and interior coordinates:
def extract_poly_coords(geom):
if geom.type == 'Polygon':
exterior_coords = geom.exterior.coords[:]
interior_coords = []
for interior in geom.interiors:
interior_coords += interior.coords[:]
elif geom.type == 'MultiPolygon':
exterior_coords = []
interior_coords = []
for part in geom:
epc = extract_poly_coords(part) # Recursive call
exterior_coords += epc['exterior_coords']
interior_coords += epc['interior_coords']
else:
raise ValueError('Unhandled geometry type: ' + repr(geom.type))
return {'exterior_coords': exterior_coords,
'interior_coords': interior_coords}
例如:
extract_poly_coords(myShape)
这篇关于如何使用Shapely提取内部多边形坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!