简体   繁体   English

Python:具有简单解决方案的多边形 shapefile 中的轮廓特征

[英]Python: Contour features in a polygon shapefiles with a straightforward solution

Dear Members list,亲爱的会员名单,

First of all, I apologize to post this question modified and improved from a previous post.首先,我很抱歉发布这个问题,从以前的帖子修改和改进。 Recently i am working on shapefiles polygon in order to compute basic contour features:最近我正在研究 shapefiles 多边形以计算基本轮廓特征:

  1. Area,区域,
  2. Perimeter,周长,
  3. Area convex hull,面积凸包,
  4. Perimeter convex hull,周长凸包,
  5. major axis length = gives the length of major axis,主轴长度 = 给出主轴的长度,
  6. minor axis length = gives the length of minor axis,短轴长度 = 给出短轴的长度,

where major and minor axis length are computed following the Figure:其中长轴和短轴长度计算如下图:

在此处输入图片说明

Using osgeo.gdal, ogr and shapely is possible to load and calculate all indeces but not the major and minor axis length.使用osgeo.gdal,OGR和匀称的是可能加载和计算所有的indeces而不是长轴和短轴长度。 Reading online solution can be using在线阅读解决方案可以使用

  1. scikit-image = Measure region properties scikit-image = 测量区域属性
  2. OpenCV OpenCV

I am looking a straightforward solution in order to make my code easy and elegant.我正在寻找一个简单的解决方案,以使我的代码简单而优雅。 Some blogs suggest to make an ellipse approximation to my polygon in order to retrieve the major and minor axis length.一些博客建议对我的多边形进行椭圆近似,以检索长轴和短轴长度。 Is it the best solution?这是最好的解决方案吗?

Any references would be quite helpful.任何参考资料都会非常有帮助。 Thanks in advance提前致谢


import osgeo.gdal, ogr
from shapely.geometry import Polygon

shp = osgeo.ogr.Open('../examples/mypoly.shp')
layer = shp.GetLayer()
feature = layer.GetFeature(0)
geometry = feature.GetGeometryRef()
# get area
Area = geometry.GetArea()
pts = geometry.GetGeometryRef(0)
points = []
for p in range(pts.GetPointCount()):
   points.append((pts.GetX(p), pts.GetY(p)))
polygon = Polygon(points)
# get Perimeter
Perimeter = polygon.length
# convex Hull
ConvexHull = polygon.convex_hull
# get Perimeter convex Hull
PerimeterConvexHull = ConvexHull.length
# get Area convex Hull
AreaConvexHull = ConvexHull.area

these are the coordinate vertices of my polygon这些是我的多边形的坐标顶点

polygon = Polygon([(560023.4495758876400000 6362057.3904932579000000),(560023.4495758876400000 6362060.3904932579000000),(560024.4495758876400000 6362063.3904932579000000),(560026.9495758876400000 6362068.3904932579000000),(560028.4495758876400000 6362069.8904932579000000),(560034.9495758876400000 6362071.8904932579000000),(560036.4495758876400000 6362071.8904932579000000),(560037.4495758876400000 6362070.3904932579000000),(560037.4495758876400000 6362064.8904932579000000),(560036.4495758876400000 6362063.3904932579000000),(560034.9495758876400000 6362061.3904932579000000),(560026.9495758876400000 6362057.8904932579000000),(560025.4495758876400000 6362057.3904932579000000),(560023.4495758876400000 6362057.3904932579000000)])

in order to test my code from here:为了从这里测试我的代码:

polygon = Polygon(points)
# get Perimeter
Perimeter = polygon.length
# convex Hull
ConvexHull = polygon.convex_hull
# get Perimeter convex Hull
PerimeterConvexHull = ConvexHull.length
# get Area convex Hull
AreaConvexHull = ConvexHull.area

Hi,i have some work on shapefiles polygon likes your work.嗨,我在 shapefiles 多边形上有一些工作喜欢你的工作。 And i use the minimum_rotated_rectangle in shapely to define the majoraxis_length and minoraxis_length.我使用 minimum_rotated_rectangle 来定义majoraxis_length 和minoraxis_length。 and my code is it:我的代码是:

polygon = Polygon(points)  
ConvexHull = polygon.convex_hull  #  convex Hull
#minimum_ rotated_ Rectangle is the minimum boundary rectangle, exterior is the external point coordinates of the minimum boundary matrix, and coords is the coordinates of the points
p1 = Point(ConvexHull.minimum_rotated_rectangle.exterior.coords[0])
p2 = Point(ConvexHull.minimum_rotated_rectangle.exterior.coords[1])
p3 = Point(ConvexHull.minimum_rotated_rectangle.exterior.coords[2])
majoraxis_length  =  p1.distance(p2)
minoraxis_length = p2.distance(p3) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM