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

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@supagro.inra.fr

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

rabotin wrote:

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

Possibly; change:

  std::cout << &driver << std::endl;

to:

  std::cout << driver << std::endl;

to confirm this.

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

I can't see anything wrong, other than failing to check the return
value from db_start_driver().

--
Glynn Clements <glynn@gclements.plus.com>

Thanks for helping me,
as you wrote I change

  std::cout << &driver << std::endl;
  

to:

  std::cout << driver << std::endl;

And I got a zero value return . So my driver isn't set ?
Is the problem from the following lines :
setenv("DBF_DRIVER","dbf",1);
setenv("DB_DATABASE","$GISDBASE/$LOCATION_NAME/$MAPSET/dbf",1);

I didn't write but I use grass 6.4 on ubuntu 9.10.
Do you know other commands to test or to set the dbf driver than db_start_driver command ?

Cheers

Mick

Glynn Clements a écrit :

rabotin wrote:

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

  std::cout << &driver << std::endl;
    
to:

  std::cout << driver << std::endl;
    
to confirm this.

Are some commands missing to access or start dbf driver in my script ?
    
I can't see anything wrong, other than failing to check the return
value from db_start_driver().

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

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@supagro.inra.fr

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

rabotin wrote:

> std::cout << driver << std::endl;
And I got a zero value return . So my driver isn't set ?
Is the problem from the following lines :
setenv("DBF_DRIVER","dbf",1);
setenv("DB_DATABASE","$GISDBASE/$LOCATION_NAME/$MAPSET/dbf",1);

These will be ignored. The default DBMI settings are taken from the
VAR file, not the environment. But you're use the settings from an
existing vector map.

I didn't write but I use grass 6.4 on ubuntu 9.10.
Do you know other commands to test or to set the dbf driver than
db_start_driver command ?

The code looks okay. Can you access the map's attribute table using
existing GRASS modules, e.g."v.db.connect -p ..."?

--
Glynn Clements <glynn@gclements.plus.com>

If (inside a GRASS session), I access with v.db.connect -p map=GU0507
it looks ok :

Vector map <GU0507@simon9> is connected by:
layer <1> table <GU0507> in database </home/rabotin/grassdata/Roujan/simon9/dbf/> through driver <dbf> with key <cat>

Do you know how to launch a grass command (e.g. v.db.connect) inside a C script
I try this line in my C progam

system(v.db.connect -p map=GU0507)

but I have an error message

sh: v.db.connect: not found

Michael R

Glynn Clements a écrit :

rabotin wrote:

  std::cout << driver << std::endl;
      

And I got a zero value return . So my driver isn't set ?
Is the problem from the following lines :
setenv("DBF_DRIVER","dbf",1);
setenv("DB_DATABASE","$GISDBASE/$LOCATION_NAME/$MAPSET/dbf",1);
    
These will be ignored. The default DBMI settings are taken from the
VAR file, not the environment. But you're use the settings from an
existing vector map.

I didn't write but I use grass 6.4 on ubuntu 9.10.
Do you know other commands to test or to set the dbf driver than db_start_driver command ?
    
The code looks okay. Can you access the map's attribute table using
existing GRASS modules, e.g."v.db.connect -p ..."?

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

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@supagro.inra.fr

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

rabotin wrote:

If (inside a GRASS session), I access with v.db.connect -p map=GU0507
it looks ok :

Vector map <GU0507@simon9> is connected by:
layer <1> table <GU0507> in database
</home/rabotin/grassdata/Roujan/simon9/dbf/> through driver <dbf> with
key <cat>

Do you know how to launch a grass command (e.g. v.db.connect) inside a C
script
I try this line in my C progam

system(v.db.connect -p map=GU0507)

but I have an error message

sh: v.db.connect: not found

That indicates the $GISBASE/bin isn't in $PATH.

For a list of envionment variables which may be useful, I suggest
reading this:

http://grass.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly

--
Glynn Clements <glynn@gclements.plus.com>

It finally works !
For information, the issue was here
setenv("GISBASE","/usr/lib/grass64/dist.i486-pc-linux-gnu",0);

I replace by this following line
setenv("GISBASE","/usr/lib/grass64/",0);
and it works

Thanks again for your help Glynn

Michael Rabotin

Glynn Clements a écrit :

rabotin wrote:

If (inside a GRASS session), I access with v.db.connect -p map=GU0507
it looks ok :

Vector map <GU0507@simon9> is connected by:
layer <1> table <GU0507> in database </home/rabotin/grassdata/Roujan/simon9/dbf/> through driver <dbf> with key <cat>

Do you know how to launch a grass command (e.g. v.db.connect) inside a C script
I try this line in my C progam

system(v.db.connect -p map=GU0507)

but I have an error message

sh: v.db.connect: not found
    
That indicates the $GISBASE/bin isn't in $PATH.

For a list of envionment variables which may be useful, I suggest
reading this:

http://grass.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly

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

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@supagro.inra.fr

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