GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
frmt.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: Vector library
5  *
6  * AUTHOR(S): Radim Blazek
7  *
8  * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
9  *
10  * COPYRIGHT: (C) 2001, 2012 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General
13  * Public License (>=v2). Read the file COPYING that
14  * comes with GRASS for details.
15  *
16  *****************************************************************************/
17 #include <string.h>
18 #include <stdio.h>
19 
20 #include <grass/vector.h>
21 #include <grass/glocale.h>
22 
23 /*!
24  \brief Read external vector format file
25 
26  \param dascii format file (frmt)
27  \param[out] finfo pointer to Format_info structure
28 
29  \return format code
30  \return -1 on error
31 */
32 int dig_read_frmt_ascii(FILE * dascii, struct Format_info *finfo)
33 {
34  char buff[2001], buf1[2001];
35  char *ptr;
36  int frmt = -1;
37 
38  G_debug(3, "dig_read_frmt_ascii()");
39 
40  /* read first line which must be FORMAT: */
41  if (G_getl2(buff, 2000, dascii)) {
42  G_chop(buff);
43 
44  if (!(ptr = strchr(buff, ':'))) {
45  G_warning(_("Vector format not recognized: %s"), buff);
46  return -1;
47  }
48 
49  strcpy(buf1, buff);
50  buf1[ptr - buff] = '\0';
51 
52  ptr++; /* Search for the start of text */
53  while (*ptr == ' ')
54  ptr++;
55 
56  if (G_strcasecmp(buf1, "FORMAT") == 0) {
57 #ifdef HAVE_OGR
58  if (G_strcasecmp(ptr, "ogr") == 0) {
59  frmt = GV_FORMAT_OGR;
60  }
61 #endif
62 #ifdef HAVE_POSTGRES
63  if (G_strcasecmp(ptr, "postgis") == 0) {
64  frmt = GV_FORMAT_POSTGIS;
65  }
66 #endif
67  }
68  }
69  if (frmt == -1) {
70  G_warning(_("Vector format not recognized: %s"), buff);
71  return -1;
72  }
73 
74  /* init format info values */
75 #ifdef HAVE_OGR
76  G_zero(&(finfo->ogr), sizeof(struct Format_info_ogr));
77 #else
78  if (frmt == GV_FORMAT_OGR) {
79  G_warning(_("Vector format '%s' not supported"), ptr);
80  return -1;
81  }
82 #endif
83 
84 #ifdef HAVE_POSTGRES
85  G_zero(&(finfo->pg), sizeof(struct Format_info_pg));
86 #else
87  if (frmt == GV_FORMAT_POSTGIS) {
88  G_warning(_("Vector format '%s' not supported"), ptr);
89  return -1;
90  }
91 #endif
92 
93  while (G_getl2(buff, 2000, dascii)) {
94  G_chop(buff);
95 
96  if (!(ptr = strchr(buff, ':'))) {
97  G_warning(_("Format definition is not correct: %s"), buff);
98  continue;
99  }
100 
101  strcpy(buf1, buff);
102  buf1[ptr - buff] = '\0';
103 
104  ptr++; /* Search for the start of text */
105  while (*ptr == ' ')
106  ptr++;
107 
108 #ifdef HAVE_OGR
109  if (frmt == GV_FORMAT_OGR) {
110  if (G_strcasecmp(buf1, "DSN") == 0)
111  finfo->ogr.dsn = G_store(ptr);
112  if (G_strcasecmp(buf1, "LAYER") == 0)
113  finfo->ogr.layer_name = G_store(ptr);
114  if (G_strcasecmp(buf1, "WHERE") == 0)
115  finfo->ogr.where = G_store(ptr);
116  }
117 #endif
118 #ifdef HAVE_POSTGRES
119  if (frmt == GV_FORMAT_POSTGIS) {
120  if (G_strcasecmp(buf1, "CONNINFO") == 0)
121  finfo->pg.conninfo = G_store(ptr);
122  if (G_strcasecmp(buf1, "SCHEMA") == 0)
123  finfo->pg.schema_name = G_store(ptr);
124  if (G_strcasecmp(buf1, "TABLE") == 0)
125  finfo->pg.table_name = G_store(ptr);
126  if (G_strcasecmp(buf1, "FID") == 0)
127  finfo->pg.fid_column = G_store(ptr);
128  if (G_strcasecmp(buf1, "WHERE") == 0)
129  finfo->pg.where = G_store(ptr);
130  }
131 #endif
132  }
133 
134 #ifdef HAVE_POSTGRES
135  /* if schema not defined, use 'public' */
136  if (frmt == GV_FORMAT_POSTGIS &&
137  !finfo->pg.schema_name) {
138  finfo->pg.schema_name = G_store("public");
139  }
140 
141  /* if fid column not defined, use default value */
142  if (frmt == GV_FORMAT_POSTGIS &&
143  !finfo->pg.fid_column) {
145  }
146 #endif
147 
148  return frmt;
149 }
150 
151 /* Write vector format, currently does not work
152  * Parse also connection string.
153  *
154  * Returns: 0 OK
155  * -1 on error
156  */
157 int dig_write_frmt_ascii(FILE * dascii, struct Format_info *finfo, int format)
158 {
159  G_debug(3, "dig_write_frmt_ascii()");
160 
161  G_fatal_error("Format not supported by dig_write_frmt_ascii()");
162 
163  return 0;
164 }
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition: getl.c:64
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define GV_PG_FID_COLUMN
GRASS-PostGIS data provider - default fid column.
Definition: dig_defines.h:262
#define GV_FORMAT_OGR
OGR format.
Definition: dig_defines.h:85
char * dsn
OGR datasource name.
Definition: dig_structs.h:525
char * table_name
Table name.
Definition: dig_structs.h:619
int dig_write_frmt_ascii(FILE *dascii, struct Format_info *finfo, int format)
Definition: frmt.c:157
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
Non-native format info (PostGIS)
Definition: dig_structs.h:602
struct Format_info_pg pg
PostGIS info.
Definition: dig_structs.h:726
Non-native format info (OGR)
Definition: dig_structs.h:516
struct Format_info_ogr ogr
OGR info.
Definition: dig_structs.h:722
#define GV_FORMAT_POSTGIS
PostGIS format.
Definition: dig_defines.h:89
char * G_chop(char *)
Chop leading and trailing white spaces.
Definition: strings.c:328
char * layer_name
OGR layer name.
Definition: dig_structs.h:529
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
char * conninfo
Connection string.
Definition: dig_structs.h:607
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
char * schema_name
Schema name.
Definition: dig_structs.h:615
char * where
SQL where statement (to filter features)
Definition: dig_structs.h:533
Non-native format info (currently only OGR is implemented)
Definition: dig_structs.h:713
int G_debug(int, const char *,...) __attribute__((format(printf
char * where
SQL where statement (fo filter features)
Definition: dig_structs.h:623
int dig_read_frmt_ascii(FILE *dascii, struct Format_info *finfo)
Read external vector format file.
Definition: frmt.c:32
char * fid_column
FID column.
Definition: dig_structs.h:627