I'm currently trying to use the OGR python bindings to process some shapefiles. I want to obtain the number of interior rings in a polygon feature. Unfortunately I'm unable to use the documented function to get that information.
Here is my code:
from osgeo import ogr
field = ogr.Open("testExtract.shp")
lyr = field.GetLayer("testExtract")
feat = lyr.GetNextFeature()
geom = feat.GetGeometryRef()
featName = geom.GetGeometryName()
print featName
geoCount = geom.GetGeometryCount()
print "geoCount: " + str(geoCount)
print geom.getNumInteriorRings()
print geom.GetNumInteriorRings()
print geom.getNumInteriorRing()
print geom.GetNumInteriorRing()
This is what those print commands output:
POLYGON
geoCount: 6
AttributeError: 'Geometry' object has no attribute 'getNumInteriorRings'
AttributeError: 'Geometry' object has no attribute 'GetNumInteriorRings'
AttributeError: 'Geometry' object has no attribute 'getNumInteriorRing'
AttributeError: 'Geometry' object has no attribute 'GetNumInteriorRing'
Now, the only polygon in that shapefile has interior rings. And the python code is telling me that geom is a polygon with 6 interior and exterior rings, which is correct. And the documentation, http://www.gdal.org/ogr/classOGRPolygon.html#a3b996195adcf9fcd4f33570a37753dde, is saying getNumInteriorRings()
is a valid function for an OGRPolygon
.
So is the problem that GetNumInteriorRings()
is not accessible through the python API? The python documention doesn't mention OGRPolygon
as a class, just Geometry
. http://gdal.org/python/osgeo.ogr.Geometry-class.html
Edit: I realize that each polygon only has one exterior polygon, so I can get that information. But I'm also interested in a couple of the other functions in OGRPolygon
such as getInteriorRing (int)
and getExteriorRing ()
so I am curious if those public member functions for OGRPolygon
are accessible in Python.
Best Answer
When you start with a Python module, there are several solutions to find the available functions. One of them is
dir
:and no
getNumInteriorRings
: apparently this method is not exposed in the python bindings.But if you look at [gdal-dev] Polygon topology:
So a polygon only:
For a polygon whith one hole:
and
But if you look at the geo_interface protocol (a python object property that returns a geojson dictionary, look at Python Geo_interface applications):
it is easier with fiona (also based on GDAL/OGR)