[gdal-dev] Driver pointer NULL

deadpickle deadpickle at gmail.com
Wed Mar 3 15:43:49 EST 2010


I have finally compiled my C program and tried running it for the
first time. When I ran the file I got the error:
[thor at updraft ~/Programs]$ ./gdal_test_convert
ERROR 10: Pointer 'hDriver' is NULL in 'OGR_Dr_CreateDataSource'.
I'm trying to load the ESRI Shapefile driver. When I compiled it I
linked it to the libgdal.a library. Is there something I'm missing?

#include "ogr_api.h"
#include "ogr_srs_api.h"
#include "stdio.h"

int main()
{
	//~ variable declaration
	const char *name = "testc.shp", *dir = "C:\\Users\\deadpickle\\Desktop
\\case study\\verify\\";
	char *source, *target;
	const char *sprj = "+proj=lcc +lat_1=33.000000 +lat_2=45.000000
+lat_0=39.000000 +lon_0=-96.000000 +x_0=0.0 +y_0=0.0 +datum=NAD83";
	const char *tprj = "WGS84";
	char c[100];
	OGRSFDriverH driver;
	OGRDataSourceH ds;
	OGRCoordinateTransformationH ctrans;
	OGRLayerH layer;
	OGRFieldDefnH fieldDefn;
	OGRGeometryH line;
	OGRFeatureDefnH featureDefn;
	OGRFeatureH feature;
	FILE *file;

	//~ working directory
	chdir(dir);

	//~ get driver and create ds
	driver = OGRGetDriverByName("ESRI Shapefile");
	ds = OGR_Dr_CreateDataSource(driver, "testc.shp", NULL);

	//~ create spatrefs and trans
	OSRNewSpatialReference(source);
	OSRNewSpatialReference(target);
	OSRSetFromUserInput(source, sprj);
	OSRSetFromUserInput(target, tprj);
	ctrans = OCTNewCoordinateTransformation(target, source);

	//~ create the layer
	layer = OGR_DS_CreateLayer(ds, "test", source, wkbMultiLineString,
NULL);

	//~ add an id field
	fieldDefn = OGR_Fld_Create("id", OFTInteger);
	OGR_L_CreateField(layer, fieldDefn, FALSE);

	//~ create geometry
	line = OGR_G_CreateGeometry(wkbMultiLineString);

	//~ layer def and create feature
	featureDefn = OGR_L_GetLayerDefn(layer);
	feature = OGR_F_Create(featureDefn);

	//~ open file
	file = fopen("2006track.csv","r");

	if(file==NULL) {
		printf("Error: can't open file.\n");
		/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
		return 1;
	}
	else {
		printf("File opened successfully. Contents:\n\n");

		while(fgets(c, 100, file)!=NULL) {
			/* keep looping until NULL pointer... */
			printf("String: %s", c);
			/* print the file one line at a time  */
		}

		printf("\n\nNow closing file...\n");
	}

	//~ close file
	fclose(file);
}


More information about the gdal-dev mailing list