[gdal-dev] Re: C GDAL beginner

brian rush at winkey.org
Wed Mar 3 21:12:08 EST 2010


I accedently replied off list so I will send it again with a couple of
additions.

deadpickle

One of c's drawbacks is its not safe unless you do it right, pointers
are very good at letting you access memory you don't own. To hunt down
such problems you can run your app under the valgrind utility on linux,
or another memory profiler on other systems.

You should use the doxygen docs to look up the functions you are calling
to see what there args are and what they return. 

http://www.gdal.org/ogr/ogr__srs__api_8h.html#6bfe88d5f18f2a8705f9e7a9586b795e

your discarding the return value and feeding it the wrong argument. 

source = OSRNewSpatialReference(NULL);

Chances are if it compiles with warnings you need to look real close at
that line.

Teaching a language is beyond the scope of this list. There are however
good resorces for that too. the irc networks freenode, efnet, and
undernet all have a #c channel, as mloskot mentioned there is also
comp.lang.c

Brian


On Wed, 2010-03-03 at 15:47 -0800, deadpickle wrote:
> Thanks for the replies. Just as a note, the warnings are caused by the
> variable being a const. Omitting the const solved it.
> I'm not sure if this is a C program or a GDAL thing, thats why I'm not
> sure where to post this at, but I get a segfault. I know segfaults are
> generally caused by ordinary, and sometimes strange, syntax but it
> involves a GDAL C API call, so I'll post it here. Basically, from what
> I understand from printing, the esgfault is caused by
> OSRSetFromUserInput(source, sprj);
> where char *source;
>  OSRNewSpatialReference(source);
>  and 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";.
>  what I'm wondering is if I'm calling these correctly? Is the proj4
> string right? If they are then I'll redirect my questions else where.
> Thanks.
> 
> #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);
> 
> 	OGRRegisterAll();
> 
> 	//~ get driver and create ds
> 	driver = OGRGetDriverByName("ESRI Shapefile");
> 	ds = OGR_Dr_CreateDataSource(driver, "testc.shp", NULL);
> 
> 	if(ds == NULL) {
> 		printf("Creation of output file failed.\n");
> 		exit(1);
> 	}
> 
> 	printf("HERE1\n");
> 
> 	//~ create spatrefs and trans
> 	OSRNewSpatialReference(source);
> 	OSRNewSpatialReference(target);
> 
> 	OSRSetFromUserInput(source, sprj);
> 
> 	printf("HERE2\n");
> 
> 	OSRSetFromUserInput(target, tprj);
> 
> 	printf("HERE3\n");
> 
> 	ctrans = OCTNewCoordinateTransformation(target, source);
> 
> 	//~ create the layer
> 	layer = OGR_DS_CreateLayer(ds, "testc", 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);
> }
> 
> 
> On Mar 3, 5:15 am, Mateusz Loskot <mate... at loskot.net> wrote:
> > You need to buy a book about C/C++ or ask on comp.lang.c or comp.lang.c++
> >
> > Best regards,
> >
> > -----
> > --
> > Mateusz Loskothttp://mateusz.loskot.net
> > --
> > View this message in context:http://n2.nabble.com/gdal-dev-C-GDAL-beginner-tp4665330p4666854.html
> > Sent from the GDAL - Dev mailing list archive at Nabble.com.
> > _______________________________________________
> > gdal-dev mailing list
> > gdal-... at lists.osgeo.orghttp://lists.osgeo.org/mailman/listinfo/gdal-dev
> _______________________________________________
> 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