[Gdal-dev] ogr getnextfeature

Mateusz Loskot mateusz at loskot.net
Fri Jan 19 08:51:04 EST 2007

mbaye wrote:
> Wath are doing  Geometry.GetNextFeature in the following cases 
> Geometry  = polygon 
> and
> Geometry = multipolygon 


I'm sorry I don't understand your question.

1. What do you mean as "Geometry.GetNextFeature"?

There is no GetNextFeature() member function in Geometry class, as well
as there is no Geometry class in OGR.


2. There is OGRLayer::GetNextFeature() and here you can read
how it works:


3. Please, check the OGR tutorial, section "Reading from OGR" and check
what GetNextFeature() returns and how to use it to iterate through
features in data sources.


4. As it's described in resoruces I'm giving above, the GetNextFeature()
returns reference (pointer, in C/C++) to object of type of OGRFeature.


Next, you can ask for reference to geometry stored in fetched
feature using GetGeometryRef()


As the GetGeometryRef() returns pointer to base class of geometries
hierarchy, you need to cast it to one of specialized type - *based* on
type of geometries stored in the data soure using
OGRGeometry::getGeometryType() function:


Check the OGR tutorial for example.

If the type of geometry is Polygon, the pointer returned by
GetGeometryRef() points in fact to OGRPolygon, so you need to cast it:

OGRPolygon *poPoly = (OGRPolygon *) poGeometry;

or if you prefer C++ casting operators:

OGRPolygon *poPoly = static_cast<OGRPolygon *>(poGeometry);

After this operation, you can use poPoly pointer to access interface of
OGRPolygon class which is described here:


You can take similar steps for geometries of type of
OGRMultiPolygon class.

I'm still not sure if you're asking for explanation as given above,
but I hope it helps.

Mateusz Loskot

More information about the Gdal-dev mailing list