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