[Gdal-dev] how to extract data from OGR

Frank Warmerdam warmerdam at pobox.com
Thu May 13 10:25:12 EDT 2004


Jacob Bensabat wrote:
> Hello
> I have started to use gdal and OGR and I have the following question,
> I am loading a mapinfo map into a OGR structure, and I would like to be 
> able to access all its components (lines, polygon, points ).
> The problem is that except functions like DumpReadable, I do not see any 
> public method in the code
> that allows access to the data.
>  
> Am I missing something ? Is there a way to get to the data ?
> an example (s) or pointer to examples would be very welcome.
> thanks

Jac,

You don't mention if you are using the C or C++ API to accomplish this.  I am
attaching example C code which converts an OGRGeometry into another form.  It
may be instructive as an example.

Things work somewhat similarly in C++.  Basically you need to fetch the
geometry object from the feature (OGRFeature::GetGeometryRef()), inspect the
type and then cast it to a pointer of the appropriate geometry type.  This
downcast pointer can then be used to interrogate information about the
geometry.  Many geometries (ie. polygon and the collections) are made of
other geometry objects which need to be fetched and queried.

Good luck,

-- 
---------------------------------------+--------------------------------------
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

/************************************************************************/
/*                      ogr_geometry_to_gv_shape()                      */
/************************************************************************/

static GvShape *ogr_geometry_to_gv_shape( OGRGeometryH hGeom )

{
     GvShape *gv_shape = NULL;
     OGRwkbGeometryType eType;

     if( hGeom == NULL )
     {
         // Use collection - which can be empty - to represent a geometryless
         // feature.

         return gv_shape_new( GVSHAPE_COLLECTION );
     }

     eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));

     if( eType == wkbPoint )
     {
         gv_shape = gv_shape_new( GVSHAPE_POINT );
         gv_shape_set_xyz( gv_shape, 0, 0,
                           OGR_G_GetX( hGeom, 0 ),
                           OGR_G_GetY( hGeom, 0 ),
                           OGR_G_GetZ( hGeom, 0 ) );
     }

     else if( eType == wkbLineString )
     {
         int    node, count = OGR_G_GetPointCount(hGeom);

         gv_shape = gv_shape_new( GVSHAPE_LINE );
         for( node = count-1; node >= 0; node-- )
             gv_shape_set_xyz( gv_shape, 0, node,
                               OGR_G_GetX( hGeom, node ),
                               OGR_G_GetY( hGeom, node ),
                               OGR_G_GetZ( hGeom, node ) );
     }

     else if( eType == wkbPolygon )
     {
         OGRGeometryH hRing;
         int        iRing = 0, nRingCount = OGR_G_GetGeometryCount( hGeom );

         gv_shape = gv_shape_new( GVSHAPE_AREA );

         for( iRing = 0; iRing < nRingCount; iRing++ )
         {
             int node;
             int vert_count = _getGeoPtCount( hGeom, iRing, &hRing );

             //hRing      = OGR_G_GetGeometryRef( hGeom, iRing );
             //vert_count = OGR_G_GetPointCount(hRing);

             for( node = vert_count - 1; node >= 0; node-- )
                 gv_shape_set_xyz( gv_shape, iRing, node,
                                   OGR_G_GetX( hRing, node ),
                                   OGR_G_GetY( hRing, node ),
                                   OGR_G_GetZ( hRing, node ) );
         }
     }

     else if( eType == wkbMultiPolygon )
     {
         int iPoly, nShapeRing = 0;

         gv_shape = gv_shape_new( GVSHAPE_AREA );

         for( iPoly = 0; iPoly < OGR_G_GetGeometryCount( hGeom ); iPoly++ )
         {
             //OGRGeometryH hPoly, hRing;
             OGRGeometryH hPoly;
             int         iRing, nRingCount;

             hPoly      = OGR_G_GetGeometryRef( hGeom, iPoly );
             nRingCount = OGR_G_GetGeometryCount( hPoly );

             for( iRing = 0; iRing < nRingCount; iRing++ )
             {
                 OGRGeometryH hRing;
                 int vert_count = _getGeoPtCount( hPoly, iRing, &hRing );
                 int node;

                 //hRing      = OGR_G_GetGeometryRef( hPoly, iRing );
                 //vert_count = OGR_G_GetPointCount(hRing);

                 for( node = vert_count - 1; node >= 0; node-- )
                     gv_shape_set_xyz( gv_shape, nShapeRing, node,
                                      OGR_G_GetX( hRing, node ),
                                      OGR_G_GetY( hRing, node ),
                                      OGR_G_GetZ( hRing, node ) );
                 nShapeRing++;
             }
         }
     }

     else if( eType == wkbGeometryCollection
              || eType == wkbMultiLineString
              || eType == wkbMultiPoint )
     {
         int         iGeom, nGeomCount;

         nGeomCount = OGR_G_GetGeometryCount( hGeom );

         gv_shape = gv_shape_new( GVSHAPE_COLLECTION );

         for( iGeom = 0; iGeom < nGeomCount; iGeom++ )
         {
             OGRGeometryH hSubGeom = OGR_G_GetGeometryRef( hGeom, iGeom );
             GvShape *sub_shape;

             sub_shape = ogr_geometry_to_gv_shape( hSubGeom );
             gv_shape_collection_add_shape( gv_shape, sub_shape );
         }
     }

     return gv_shape;
}




More information about the Gdal-dev mailing list