[gdal-dev] Need help if OGR C-API

Even Rouault even.rouault at mines-paris.org
Fri Sep 3 13:43:11 EDT 2010


Le vendredi 03 septembre 2010 18:45:56, Stephen Woodbridge a écrit :
> Hi all,
> 
> Trying to convert a shapelib program over to OGR and have to say it is
> very complicated.

No, it's just that you are not yet accustomed to use it ;-)

> 
> I'm using: GDAL 1.7.2, released 2010/04/23
> 
> At the moment I stuck on:
> 
>    hDSin = OGROpen( "Streets.shp", FALSE, NULL );
>    hLayerIn = OGR_DS_GetLayerByName( hDSin, "Streets" );
> 
>    hFDefnIn = OGR_L_GetLayerDefn( hLayerIn );
>    layerGeomType = OGR_FD_GetGeomType( hFDefnIn );
> 
> And always returns:   layerGeomType == wkbUnknown

Well, this is really weird. I've just tried the following code that is really 
a stand-alone version of your code:

#include <ogr_api.h>

int main(int argc, char* argv[])
{
    OGRRegisterAll();
    OGRDataSourceH  hDSin = OGROpen( argv[1], FALSE, NULL );
    OGRLayerH hLayerIn = OGR_DS_GetLayer( hDSin, 0 );
    OGRFieldDefnH hFDefnIn = OGR_L_GetLayerDefn( hLayerIn );
    OGRwkbGeometryType layerGeomType = OGR_FD_GetGeomType( hFDefnIn );
    printf("%d\n", layerGeomType == wkbLineString);
    return 0;
}

when passed a shapefile with linestrings, it prints 1 as expected with both 
trunk and 1.7 branch. You should try it on your streets.shp. If it returns 1, 
so there must something subtly wrong on your code that perhaps valgrind could 
detect. If it returns 0, hum, well no idea...

> 
> And OGR_L_GetGeomType( hLayerIn )  does not appear until 1.8.

You don't need it. This is just a convenience method that can provide speed-up 
for some drivers. But as documented, it will be strictly equivalent to 
OGR_FD_GetGeomType(OGR_L_GetLayerDefn(hLayerIn)).

> 
> ogrinfo -so Streets.shp Streets
> INFO: Open of `Streets.shp'
>        using driver `ESRI Shapefile' successful.
> 
> Layer name: Streets
> Geometry: Line String
> Feature Count: 102958
> Extent: (-71.888528, 42.156701) - (-71.020913, 42.734315)
> Layer SRS WKT:
> (unknown)
> LINK_ID: Integer (10.0)
> NAME: String (80.0)
> ...
> 
> So how do I get the geometry type of the shapefile?
> 
> -Steve
> _______________________________________________
> 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