[GRASS-user] Setting GRASS environment variable in a C program - step two

rabotin rabotin at supagro.inra.fr
Wed May 26 06:31:47 EDT 2010


Dear all, continuing in my C program test to access grass data without launching a grass session and a great thanks to Glynn Clements who help me with the first step, I begin to write few c lines to access a vector map into a mapset (in this example the GU0507 vector in the Roujan Location and the simon9 Mapset). And it works ! Except the fact I can't access to it atttribute layer (I use the default driver dbf)...

I try to use the db_set_handle(&handle,Fi->database,NULL) command but the returns is a null value. Is it normal ?

After that , using db_open_database(driver,&handle) command launch a segmentation fault. Is it because my handle is a null  value ?

Are some commands missing to access or start dbf driver in my script ?

Thanks in advance for any help for a newbie in grass scripting !!


Here my script : 

 
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern "C" {

#include <grass/config.h>
#include <grass/gis.h>
#include <grass/Vect.h>
#include <grass/glocale.h>
#include <grass/dbmi.h>
}


int main(int argc, char *argv[])
{
	char name[GNAME_MAX];
	char * gisdbase, * location,* mapset, * database;
	const char * drivername, * databasename;
	struct field_info *Fi;
	struct Map_info In;

	dbString table_name,dbsql,valstr;
	dbHandle handle;
	dbDriver *driver;

	setenv("GISBASE","/usr/lib/grass64/dist.i486-pc-linux-gnu",1);
	setenv("GISRC","/home/rabotin/prov/config",1);
	setenv("PATH","$GISBASE/bin:$GISBASE/scripts:$PATH",1);
	setenv("DBF_DRIVER","dbf",1);
	setenv("DB_DATABASE","$GISDBASE/$LOCATION_NAME/$MAPSET/dbf",1);

	if ( getenv ("GISRC") ) {
           // Store default values
	    gisdbase = G_gisdbase();
	    location = G_location();
          mapset = G_mapset();
          drivername=db_get_default_driver_name();
          databasename=db_get_default_database_name();
	}

	G_gisinit(argv[0]);

	std::cout << gisdbase << std::endl; // return /home/rabotin/grassdata
	std::cout << location << std::endl; //return Roujan
	std::cout << mapset << std::endl; // return simon9
	std::cout << drivername << std::endl; //return dbf
	std::cout << databasename << std::endl; //return $GISDBASE/$LOCATION_NAME/$MAPSET/dbf

	strcpy(name,"GU0507");

	std::cout << G_find_vector(name, G_mapset()) << std::endl; //return simon9
	Vect_open_old(&In,name, G_mapset());
	std::cout << Vect_get_name(&In) << std::endl; //return GU0507
	Fi=Vect_get_field(&In,1);
	std::cout << &Fi << std::endl; //return 0xbfd2a294
	printf("Field %d;Name %s, Driver %s, database %s, table %s, key %s  \n",Fi->number,Fi->name, Fi->driver,Fi->database,Fi->table,Fi->key);
      // return       Field 1;Name (null), Driver dbf, database /home/rabotin/grassdata/Roujan/simon9/dbf/, table GU0507, key cat 

	db_init_string(&dbsql);
	db_init_string(&valstr);
	db_init_string(&table_name);
	db_init_handle(&handle);

	driver=db_start_driver(Fi->driver);
	std::cout << &driver << std::endl; //return 0xbfd2a290

	std::cout <<db_set_handle(&handle,Fi->database,NULL)<< std::endl; // return 0
      
      // the following lines return segmentation fault
	if (db_open_database(driver,&handle)!=DB_OK){
		printf("pb");
	}
}


My /home/rabotin/prov/config file is


GISDBASE: /home/rabotin/grassdata
LOCATION_NAME: Roujan
MAPSET: simon9
MONITOR: x0
GRASS_GUI: wxpython


-- 
*********************************

Michaël Rabotin
Ingénieur d'étude en géomatique

Laboratoire d'étude des Interactions Sol, Agrosystème et Hydrosystème
UMR LISAH SupAgro-INRA-IRD
Bat. 24
2 place Viala
34060 Montpellier cedex 1 
FRANCE

Téléphone :  33 (0)4 99 61 23 85
Secrétariat : 33 (0)4 99 61 22 61
Fax : 33 (0)4 67 63 26 14
E-mail : rabotin at supagro.inra.fr

*********************************



More information about the grass-user mailing list