GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddf66e9d5c
Loading...
Searching...
No Matches
field.c File Reference

Vector library - field (layer) related fns. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/dbmi.h>
#include <grass/vector.h>
#include "local_proto.h"
#include <gdal_version.h>
#include <ogr_api.h>
Include dependency graph for field.c:

Go to the source code of this file.

Macros

#define NOPG_UNUSED
 

Functions

struct dblinks * Vect_new_dblinks_struct (void)
 Create and init new dblinks structure.
 
void Vect_reset_dblinks (struct dblinks *p)
 Reset dblinks structure (number of fields)
 
int Vect_map_add_dblink (struct Map_info *Map, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
 Add new db connection to Map_info structure.
 
int Vect_map_del_dblink (struct Map_info *Map, int field)
 Delete db connection from Map_info structure.
 
void Vect_copy_map_dblinks (struct Map_info *In, struct Map_info *Out, int first_only)
 Copy DB links from input vector map to output vector map.
 
int Vect_map_check_dblink (struct Map_info *Map, int field, const char *name)
 Check if DB connection exists in dblinks structure.
 
int Vect_check_dblink (const struct dblinks *p, int field, const char *name)
 Check if DB connection exists in dblinks structure.
 
int Vect_add_dblink (struct dblinks *p, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
 Add new DB connection to dblinks structure.
 
struct field_info * Vect_default_field_info (struct Map_info *Map, int field, const char *field_name, int type)
 Get default information about link to database for new dblink.
 
struct field_info * Vect_get_dblink (struct Map_info *Map, int link)
 Get information about link to database.
 
struct field_info * Vect_get_field (struct Map_info *Map, int field)
 Get information about link to database (by layer number)
 
struct field_info * Vect_get_field_by_name (struct Map_info *Map, const char *field)
 Get information about link to database (by layer name)
 
struct field_info * Vect_get_field2 (struct Map_info *Map, const char *field)
 Get information about link to database (by layer number or layer name)
 
int Vect_get_field_number (struct Map_info *Map, const char *field)
 Get field number of given field.
 
void Vect_destroy_field_info (struct field_info *fi)
 Free a struct field_info and all memory associated with it.
 
int Vect_read_dblinks (struct Map_info *Map)
 Read dblinks to existing structure.
 
int Vect_write_dblinks (struct Map_info *Map)
 Write dblinks to file.
 
char * Vect_subst_var (const char *in, struct Map_info *Map)
 Substitute variable in string.
 
void Vect_set_db_updated (struct Map_info *Map)
 Rewrite 'dbln' file.
 

Detailed Description

Vector library - field (layer) related fns.

Higher level functions for reading/writing/manipulating vectors.

Todo:
see Vect_read_dblinks(); activate auto-FID detection once OGR_L_GetFIDColumn() is working or solution found if FID not available

SPDX-FileCopyrightText: 2001-2009, 2011-2012 GRASS Development Team SPDX-License-Identifier: GPL-2.0-or-later

Author
Original author CERL, probably Dave Gerdes or Mike Higgins.
Update to GRASS 5.7 by Radim Blazek and David D. Gray.
Various updates by Martin Landa <landa.martin gmail.com>, 2009-2011

Definition in file field.c.

Macro Definition Documentation

◆ NOPG_UNUSED

#define NOPG_UNUSED

Definition at line 34 of file field.c.

Function Documentation

◆ Vect_add_dblink()

int Vect_add_dblink ( struct dblinks *  p,
int  number,
const char *  name,
const char *  table,
const char *  key,
const char *  db,
const char *  driver 
)

Add new DB connection to dblinks structure.

Parameters
[in,out]ppointer to existing dblinks structure
numberlayer number (1 for OGR)
namelayer name (layer for OGR) - if not given use table name
tabletable name (layer for OGR)
keykey name
dbdatabase name (datasource for OGR)
driverdriver name (dbf, postgresql, ogr, ...)
Returns
0 on success
-1 error

Definition at line 278 of file field.c.

References _, dblinks::alloc_fields, dblinks::field, G_debug(), G_free(), G_realloc, G_store(), G_warning(), field_info::key, dblinks::n_fields, AMI_STREAM< T >::name(), name, NULL, field_info::number, field_info::table, and Vect_check_dblink().

Referenced by V1_close_nat(), and Vect_map_add_dblink().

◆ Vect_check_dblink()

int Vect_check_dblink ( const struct dblinks *  p,
int  field,
const char *  name 
)

Check if DB connection exists in dblinks structure.

Parameters
ppointer to existing dblinks structure
fieldlayer number
namelayer name
Returns
1 dblink for field exists
0 dblink does not exist for field

Definition at line 245 of file field.c.

References dblinks::field, G_debug(), dblinks::n_fields, AMI_STREAM< T >::name(), name, and NULL.

Referenced by Vect_add_dblink(), and Vect_map_check_dblink().

◆ Vect_copy_map_dblinks()

void Vect_copy_map_dblinks ( struct Map_info *  In,
struct Map_info *  Out,
int  first_only 
)

Copy DB links from input vector map to output vector map.

Parameters
Inpointer to Map_info structure (input)
Outpointer to Map_info structure (output)
first_onlyTRUE to copy only first link otherwise all DB links are copied

Definition at line 198 of file field.c.

References _, G_warning(), AMI_STREAM< T >::name(), Vect_get_dblink(), Vect_get_num_dblinks(), and Vect_map_add_dblink().

Referenced by Vect_close().

◆ Vect_default_field_info()

struct field_info * Vect_default_field_info ( struct Map_info *  Map,
int  field,
const char *  field_name,
int  type 
)

Get default information about link to database for new dblink.

Parameters
Mappointer to Map_info structure
fieldlayer number
field_namelayer name
typehow many tables are linked to map: GV_1TABLE / GV_MTABLE
Returns
pointer to allocated field_info structure

Definition at line 351 of file field.c.

References _, field_info::database, DB_DEFAULT_DRIVER, db_get_connection(), db_set_default_connection(), field_info::driver, G_asprintf(), G_debug(), G_fatal_error(), G_free(), G_important_message(), G_malloc, G_store(), G_zero(), GNAME_MAX, GV_1TABLE, GV_FORMAT_OGR_DIRECT, GV_KEY_COLUMN, field_info::key, AMI_STREAM< T >::name(), field_info::name, NULL, field_info::number, field_info::table, and Vect__get_element_path().

Referenced by Vect_copy_table_by_cats(), and Vect_rename().

◆ Vect_destroy_field_info()

◆ Vect_get_dblink()

struct field_info * Vect_get_dblink ( struct Map_info *  Map,
int  link 
)

Get information about link to database.

Variables are substituted by values, link is index to array of dblinks.

Parameters
Mappointer to Map_info structure
linklink id
Returns
pointer to new field_info structure

Definition at line 468 of file field.c.

References _, field_info::database, field_info::driver, G_debug(), G_malloc, G_store(), G_warning(), field_info::key, AMI_STREAM< T >::name(), field_info::name, NULL, field_info::number, field_info::table, and Vect_subst_var().

Referenced by V1_close_nat(), Vect__delete(), Vect_copy_map_dblinks(), Vect_copy_tables(), Vect_get_field(), Vect_get_field2(), Vect_get_field_by_name(), and Vect_rename().

◆ Vect_get_field()

struct field_info * Vect_get_field ( struct Map_info *  Map,
int  field 
)

Get information about link to database (by layer number)

Variables are substituted by values, field is number of requested field.

Parameters
Mappointer to Map_info structure
fieldlayer number
Returns
pointer to new field_info structure
NULL if not found

Definition at line 508 of file field.c.

References G_debug(), NULL, and Vect_get_dblink().

Referenced by Gp_load_sites_thematic(), Gv_load_vect_thematic(), IL_vector_input_data_2d(), NetA_get_node_costs(), NetA_init_timetable_from_db(), Vect_cats_set_constraint(), Vect_copy_table_by_cats(), Vect_get_column_names(), Vect_get_column_names_types(), Vect_get_column_types(), Vect_get_field2(), Vect_net_build_graph(), Vect_net_ttb_build_graph(), Vect_rename(), Vect_set_varray_from_db(), and Vect_write_ascii().

◆ Vect_get_field2()

struct field_info * Vect_get_field2 ( struct Map_info *  Map,
const char *  field 
)

Get information about link to database (by layer number or layer name)

Note: if field is -1 then the function returns the first dblink or NULL

Parameters
Mappointer to Map_info structure
fieldlayer number or name
Returns
pointer to new field_info structure
NULL if not found

Definition at line 564 of file field.c.

References G_debug(), NULL, Vect_get_dblink(), Vect_get_field(), Vect_get_field_by_name(), and Vect_get_num_dblinks().

Referenced by Vect_get_field_number().

◆ Vect_get_field_by_name()

struct field_info * Vect_get_field_by_name ( struct Map_info *  Map,
const char *  field 
)

Get information about link to database (by layer name)

Parameters
Mappointer to Map_info structure
fieldlayer name
Returns
pointer to new field_info structure
NULL if not found

Definition at line 534 of file field.c.

References G_debug(), AMI_STREAM< T >::name(), NULL, and Vect_get_dblink().

Referenced by Vect_get_field2().

◆ Vect_get_field_number()

int Vect_get_field_number ( struct Map_info *  Map,
const char *  field 
)

Get field number of given field.

Parameters
Mappointer to Map_info structure
fieldlayer name
Returns
layer number
-1 for all layers
0 if layer not found

Definition at line 601 of file field.c.

References G_debug(), field_info::number, Vect_get_field2(), and Vect_get_num_dblinks().

Referenced by vector2perimeters().

◆ Vect_map_add_dblink()

int Vect_map_add_dblink ( struct Map_info *  Map,
int  number,
const char *  name,
const char *  table,
const char *  key,
const char *  db,
const char *  driver 
)

Add new db connection to Map_info structure.

Parameters
Mappointer to Map_info structure
numberlayer number
namelayer name (if not given use table name)
tabletable name
keykey name
dbdatabase name
driverdriver name
Returns
0 on success
-1 on failure

Definition at line 113 of file field.c.

References _, G_warning(), GV_MODE_RW, GV_MODE_WRITE, name, Vect_add_dblink(), and Vect_write_dblinks().

Referenced by Vect_copy_map_dblinks(), Vect_copy_table_by_cats(), and Vect_rename().

◆ Vect_map_check_dblink()

int Vect_map_check_dblink ( struct Map_info *  Map,
int  field,
const char *  name 
)

Check if DB connection exists in dblinks structure.

Parameters
Mappointer to Map_info structure
fieldlayer number
namelayer name
Returns
1 dblink for field exists
0 dblink does not exist for field

Definition at line 230 of file field.c.

References name, and Vect_check_dblink().

◆ Vect_map_del_dblink()

int Vect_map_del_dblink ( struct Map_info *  Map,
int  field 
)

Delete db connection from Map_info structure.

Parameters
Mappointer to Map_info structure
fieldlayer number (-1 to delete all dblinks)
Returns
0 deleted
-1 error

Definition at line 153 of file field.c.

References _, dblinks::field, G_debug(), G_warning(), AMI_STREAM< T >::name(), and Vect_write_dblinks().

Referenced by Vect_close(), and Vect_rename().

◆ Vect_new_dblinks_struct()

struct dblinks * Vect_new_dblinks_struct ( void  )

Create and init new dblinks structure.

Returns
pointer to new dblinks structure
NULL on failure

Definition at line 45 of file field.c.

References G_malloc, and G_zero().

Referenced by V1_close_nat(), and Vect__open_old().

◆ Vect_read_dblinks()

int Vect_read_dblinks ( struct Map_info *  Map)

Read dblinks to existing structure.

Variables are not substituted by values.

Parameters
Mappointer to Map_info structure
Returns
number of links read
-1 on error

Definition at line 835 of file field.c.

References _, G_debug(), G_fatal_error(), GV_FORMAT_NATIVE, GV_FORMAT_OGR, GV_FORMAT_OGR_DIRECT, GV_FORMAT_POSTGIS, AMI_STREAM< T >::name(), and Vect_reset_dblinks().

Referenced by Vect__open_old().

◆ Vect_reset_dblinks()

void Vect_reset_dblinks ( struct dblinks *  p)

Reset dblinks structure (number of fields)

Parameters
ppointer to existing dblinks structure

Definition at line 94 of file field.c.

References dblinks::n_fields.

Referenced by Vect_read_dblinks().

◆ Vect_set_db_updated()

void Vect_set_db_updated ( struct Map_info *  Map)

Rewrite 'dbln' file.

Should be used by GRASS modules which update database tables, so that other applications know that tables were changed and can reload data.

Parameters
Mappointer to Map_info structure

Definition at line 980 of file field.c.

References _, G_fatal_error(), G_mapset(), G_strcasecmp(), and Vect_write_dblinks().

◆ Vect_subst_var()

char * Vect_subst_var ( const char *  in,
struct Map_info *  Map 
)

Substitute variable in string.

Parameters
incurrent string
Mappointer to Map_info structure
Returns
pointer to new string

Definition at line 921 of file field.c.

References G_debug(), G_free(), G_store(), G_str_replace(), AMI_STREAM< T >::name(), NULL, and strcpy.

Referenced by Vect_copy_table_by_cats(), Vect_get_dblink(), and Vect_rename().

◆ Vect_write_dblinks()

int Vect_write_dblinks ( struct Map_info *  Map)

Write dblinks to file.

Parameters
Mappointer to Map_info structure
Returns
0 on success
-1 on error

Definition at line 867 of file field.c.

References _, dblinks::field, G_debug(), G_fopen_new(), G_warning(), GPATH_MAX, GV_DBLN_ELEMENT, GV_FORMAT_NATIVE, dblinks::n_fields, AMI_STREAM< T >::name(), NULL, Vect__get_path(), and Vect_get_name().

Referenced by V1_close_nat(), Vect_map_add_dblink(), Vect_map_del_dblink(), and Vect_set_db_updated().