1

I want to find out the geometry column and then want to export the geometry to WKB format from a shapefile. I wrote a code in C to find the geometry column. But a run-time error occurred. Can you please explain?

#include <stdio.h>
#include <stdlib.h>
#include <ogr_api.h>
#include <cpl_conv.h>
#include <cpl_port.h>
#include <gdal.h>
#include <string.h>
#include <gdal_utils.h>
#include <cpl_progress.h>
#include <cpl_minixml.h>
#include <ogr_core.h>
int main()
{
    char path1[200],filename1[]="/1.shp";
    GDALDatasetH inShapeDS;
    OGRLayerH inShapeLayer;
    OGRFeatureH inShapeFeature;

    GDALAllRegister();

    getcwd(path1, sizeof(path1));
    strcat(path1,filename1);

    inShapeDS = GDALOpenEx(path1, GDAL_OF_VECTOR, NULL, NULL, NULL );
    if( inShapeDS == NULL )
    {
        printf( "Open failed.\n" );
        exit( 1 );
    }

    inShapeLayer = GDALDatasetGetLayerByName(inShapeDS, path1);

    printf("number of layers:%d\n",GDALDatasetGetLayerCount(inShapeDS));    

    printf("geom col name:%s",OGR_L_GetGeometryColumn(inShapeLayer));
    GDALClose(inShapeDS);

    return 0;
}

Error:

number of layers:1 
ERROR 10: Pointer 'hLayer' is NULL in 'OGR_L_GetGeometryColumn'.


geom col name:(null)
2
  • is it possible that inShapeLayer is null?
    – Andy C
    Commented Feb 16, 2017 at 15:58
  • GDALDatasetGetLayerCount function gave the number of layer is 1. So, it is not null.
    – LSG
    Commented Feb 16, 2017 at 16:02

1 Answer 1

1

I would guess that the name of the layer getting passed into GDALDatasetGetLayerByName is incorrect. With a shapefile the layer name is the filename without the extension. In this case the layer name should be 1.

inShapeLayer = GDALDatasetGetLayerByName(inShapeDS, "1");

It is easier to just fetch the first layer (since there will only ever be one layer in a shapefile) instead.

inShapeLayer = GDALDatasetGetLayer(inShapeDS, 0);

GDALDatasetGetLayerCount is called against the inShapeDS not the actual layer so this call will work regardless of whether the call to GDALDatasetGetLayer* returned the layer or not.

1
  • Thank you for your answer. Now it is running without error. But I have another problem. When I am trying to get the geometry column name by OGR_L_GetGeometryColumn(inShapeLayer) function, it is showing nothing by %s print. Some unknown character by %c print. Can you help in this regard?
    – LSG
    Commented Feb 17, 2017 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.