4
votes

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()
3
Did you solved this question? I'm really interested in this solution too. (In my case, I need to calculate the perimeter of a geometry).hbobenicio
hey hbobenicio. I used the points from geometry to calculate the perimeter with from shapely.geometry import PolygonGianni Spear
@hbobenicio under you can see my solution with def "edges_index".Gianni Spear
@hbobenicio I posted a new elegant solution to calculate area and perimeter from a polygon!!!Gianni Spear

3 Answers

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

I might be late on this one but i was looking for a solution to the same question and i happen to have chanced on this one. I solved the issue by simply finding the boundary of the geometry and then finding the length of the boundary. Sample Python code below:

perimeter = feat.GetGeometryRef().Boundary().Length()
2
votes
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)))