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

时间:2012-11-24 13:16:31

标签: python math geometry polygon ellipse

亲爱的会员名单,

首先,我很抱歉在上一篇文章中修改和改进了这个问题。最近我正在研究shapefiles多边形以计算基本轮廓特征:

  1. 区,
  2. 周长,
  3. 区域凸包,
  4. 周长凸包,
  5. 长轴长度=给出长轴的长度,
  6. 短轴长度=给出短轴的长度,
  7. 其中长轴和短轴长度按照图形计算:

    enter image description here

    使用osgeo.gdal,ogr和shapely可以加载和计算所有的indeces而不是主轴和次轴的长度。阅读在线解决方案可以使用

    1. scikit-image =衡量地区属性
    2. OpenCV
    3. 我正在寻找一个直接的解决方案,以使我的代码简单而优雅。一些博客建议对我的多边形进行椭圆逼近,以便检索长轴和短轴长度。这是最好的解决方案吗?

      任何参考都会非常有帮助。 提前致谢


      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
      

      这些是我的多边形

      的坐标顶点
      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)])
      

      为了从这里测试我的代码:

      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
      

1 个答案:

答案 0 :(得分:0)

嗨,我有一些关于 shapefiles 多边形的工作,喜欢你的工作。 我使用 minimum_rotated_rectangle 来定义majoraxis_length 和minoraxis_length。 我的代码就是:

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)