[gdal-dev] Polygon topology

Mike Toews mwtoews at gmail.com
Tue Mar 8 05:08:48 EST 2011


You could use the `interiors` length in Shapely:

from osgeo import ogr
from shapely.wkb import loads
source = ogr.Open('my_polygons.shp')
layer = source.GetLayer()
feature = layer.GetNextFeature()
num = 0
while feature:
    g = loads(feature.GetGeometryRef().ExportToWkb())
    if g.geom_type == 'Polygon':
        g.num_holes = len(g.interiors)
    elif g.geom_type == 'MultiPolygon':
        g.num_holes = sum([len(mp.interiors) for mp in g])
    else:
        raise Exception('Unexpected geom_type: '+g.geom_type)
    print g.geom_type, num, 'with', g.num_holes, 'hole'+('s' if
g.num_holes != 1 else '')
    feature = layer.GetNextFeature()
    num += 1


-Mike

On 8 March 2011 21:57, Simon Lyngby Kokkendorff <silyko at gmail.com> wrote:
> Hi List,
>
>   I am using ogr via the python bindings to construct various polygons.
> Here's just a simple question, to which someone might have some input. Is
> there anyway to determine the topology, i.e. the number of holes, of a
> polygon without e.g. having to export to WKT and examining the output
> string. There doesn't seem to be any methods exposed in the bindings to do
> this directly?
>
>   Cheers,
>   Simon Kokkendorff,
>   National Survey and Cadastre of Denmark
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>


More information about the gdal-dev mailing list