【问题标题】:Python how calculate a polygon perimeter using an osgeo.ogr.Geometry objectPython 如何使用 osgeo.ogr.Geometry 对象计算多边形周长
【发布时间】:2012-11-22 16:59:57
【问题描述】:

首先,我很抱歉发布这个简单的问题。我需要计算一定数量的几何属性(面积、周长、圆度、长轴和短轴等)。我正在使用 GDAL/OGR 读取我的多边形的 shapefile 格式。我想问的是:

  1. 是否有使用 osgeo.ogr.Geometry 计算周长的方法?
  2. 是否有用于计算多边形指标的模块构建?

提前致谢

    import osgeo.gdal, ogr
    poly="C:\\\myshape.shp"
    shp = osgeo.ogr.Open(poly)
    layer = shp.GetLayer()
    # For every polygon
    for index in xrange(len(allFID)):
        feature = layer.GetFeature(index)
        # get "FID" (Feature ID)
        FID = str(feature.GetFID())
        geometry = feature.GetGeometryRef()
        # get the area
        Area = geometry.GetArea()

【问题讨论】:

  • 这个问题你解决了吗?我也对这个解决方案很感兴趣。 (就我而言,我需要计算几何的周长)。
  • 嘿hbobenicio。我使用几何中的点来计算周长 from shapely.geometry import Polygon
  • @hbobenicio 下面你可以看到我的解决方案 def "edges_index"。
  • @hbobenicio 我发布了一个新的优雅的解决方案来计算多边形的面积和周长!!!

标签: python geometry computational-geometry


【解决方案1】:
        ref_geometry = ref_feature.GetGeometryRef()
        pts = ref_geometry.GetGeometryRef(0)
        points = []
        for p in xrange(pts.GetPointCount()):
            points.append((pts.GetX(p), pts.GetY(p)))

def edges_index(points):
    """
    compute edges index for a given 2D point set

    1- The number of edges which form the polygon
    2- Perimeter
    3- The length of the longest edge in a polygon
    4- The length of the shortest edge in a polygon
    5- The average length of all of edges in a polygon
    6- The lengths of edges deviate from their mean value
    """
    Nedges = len(points)-1
    length = []
    for i in xrange(Nedges):
        ax, ay = points[i]
        bx, by = points[i+1]
        length.append(math.hypot(bx-ax, by-ay))
    edges_perimeter = numpy.sum(length)
    edges_max = numpy.amax(length)
    edges_min = numpy.amin(length)
    edges_average = numpy.average(length)
    edges_std = numpy.std(length)
    return (Nedges,edges_perimeter,edges_max,edges_min,edges_average,edges_std)

【讨论】:

    【解决方案2】:

    我可能在这个问题上迟到了,但我正在寻找同一个问题的解决方案,而我碰巧遇到了这个问题。我通过简单地找到几何的边界然后找到边界的长度来解决这个问题。示例 Python 代码如下:

    perimeter = feat.GetGeometryRef().Boundary().Length()
    

    【讨论】:

      【解决方案3】:
      poly = [(0,10),(10,10),(10,0),(0,0)]
      
      
      def segments(poly):
              """A sequence of (x,y) numeric coordinates pairs """
              return zip(poly, poly[1:] + [poly[0]])
      
      def area(poly):
          """A sequence of (x,y) numeric coordinates pairs """
          return 0.5 * abs(sum(x0*y1 - x1*y0
              for ((x0, y0), (x1, y1)) in segments(poly)))
      
      def perimeter(poly):
          """A sequence of (x,y) numeric coordinates pairs """
          return abs(sum(math.hypot(x0-x1,y0-y1) for ((x0, y0), (x1, y1)) in segments(poly)))
      

      【讨论】:

        猜你喜欢
        • 2017-03-18
        • 2023-01-26
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-17
        相关资源
        最近更新 更多