Python如何使用osgeo.ogr.Geometry对象计算多边形周长 [英] Python how calculate a polygon perimeter using an osgeo.ogr.Geometry object

查看:1412
本文介绍了Python如何使用osgeo.ogr.Geometry对象计算多边形周长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我很抱歉发布这个简单的问题。我需要计算一定数量的gemotrical属性(area,perimeters,Roundess,主轴和副轴等)。我正在使用 GDAL / OGR 读取我的多边形的shapefile格式。我想问的是:


  1. 是否有一种使用osgeo.ogr.Geometry计算周长的方法?

  2. 是否有模块构建来计算多边形上的度量?

预先感谢

  import osgeo.gdal,ogr 
poly =C:\\\myshape.shp
shp = osgeo.ogr .open(poly)
layer = shp.GetLayer()
#对于xrange(len(allFID))中索引的每个多边形

feature = layer.GetFeature(index )
#getFID(Feature ID)
FID = str(feature.GetFID())
geometry = feature.GetGeometryRef()
#获取区域
Area = geometry.GetArea()


解决方案

poly = [(0,10),(10,10),(10,0),(0,0)]


def segments(poly) :
一系列(x,y)数字坐标对
return zip(poly,poly [1:] + [poly [0]])
$ b $ (b)def area(poly):
一系列(x,y)数字坐标对
return 0.5 * abs(sum(x0 * y1 - x1 * y0
对于(x0,y0),(x1,y1))的分段(poly)))

def perimeter(poly):
(poly)中的((x0,y0),(x1,y1))的总和(math.hypot(x0-x1,y0-y1)))


First of all, I apologize to post this easy question. I need to compute a certain number of gemotrical attributes (area, perimeters, Roundess, major and minor axis, etc). I am using GDAL/OGR to read a shapefile format of my polygon. What i wish to ask is:

  1. is there a method to compute the perimeter using osgeo.ogr.Geometry?
  2. is there a module build to compute metrics on polygon?

thanks in advance

    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()

解决方案

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)))

这篇关于Python如何使用osgeo.ogr.Geometry对象计算多边形周长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆