[Gdal-dev] OGR question

Frank Warmerdam warmerdam at pobox.com
Thu Feb 26 16:23:12 EST 2004


Clay, Bruce wrote:
> Frank:
>   thanks for the reply.  
> 
> I am using the following
> 
>         while( (feature = OGR_L_GetNextFeature( layer )) != NULL )
>         {
> 			OGRGeometryH geom_handl =
> OGR_F_GetGeometryRef(feature);
> 			OGRwkbGeometryType type =
> OGR_G_GetGeometryType(geom_handl);
>  more stuff
> 	}
> 
> The type is wkbPolygon.  When I look in the ogrpolygon.cpp file I don't
> find getNumPoints, getX or getY;
> 
> I started this task by copying OGRDump and modifying that.
> 
> Am I in the wrong ballpark?

Bruce,

You are getting close.  Polygons are considered to consist of rings, so
you need to get the ring count, and loop over them.  The rings are essentially
closed linestrings.

eg.

     while( (feature = OGR_L_GetNextFeature( layer )) != NULL )
     {
         OGRGeometryH geom_handl = OGR_F_GetGeometryRef(feature);
         OGRwkbGeometryType type = OGR_G_GetGeometryType(geom_handl);

         printf( "\nFeature %d\n", OGR_F_GetFID() );

         if( wkbFlatten(type) == wkbPolygon )
         {
             int iRing;

             for( iRing = 0;
                  iRing < OGR_G_GetGeometryCount(geom_handl);
                  iRing++ )
             {
                 int iPoint;
                 OGRGeometryH ring_handl
                     = OGR_G_GetGeometryRef( geom_handl, iRing );

                 printf( "Ring %d\n", iRing );
                 for( iPoint = 0;
                      iPoint < OGR_G_GetPointCount(ring_handl);
                      iPoint++ )
                 {
                     printf( "  %d: (%g,%g,%g)\n",
                             iPoint,
                             OGR_G_GetX( ring_handl, iPoint ),
                             OGR_G_GetY( ring_handl, iPoint ),
                             OGR_G_GetZ( ring_handl, iPoint ) );
                 }
             }
         }

         ...

The C API just uses generic OGRGeometryH handles for all geometries,
so it makes it harder to work out what sorts of information different
geometry types have.  Sometimes it is helpful to look through the C++
API to get a better idea of the relationships.  I really ought to improve
the C API docs.  Others have run into the same problem you are running
into.

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list