GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
header_finfo.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/header_finfo.c
3
4 \brief Vector library - header manipulation (relevant for external
5 formats)
6
7 Higher level functions for reading/writing/manipulating vectors.
8
9 (C) 2001-2013 by the GRASS Development Team
10
11 This program is free software under the GNU General Public License
12 (>=v2). Read the file COPYING that comes with GRASS for details.
13
14 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
15 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
16 \author Update to GRASS 7 (OGR/PostGIS support) by Martin Landa <landa.martin
17 gmail.com>
18 */
19
20#include <string.h>
21
22#include <grass/vector.h>
23#include <grass/glocale.h>
24
25/*!
26 \brief Get datasource name (relevant only for non-native formats)
27
28 Returns:
29 - datasource name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
30 - database name for PostGIS format (GV_FORMAT_POSTGIS)
31
32 \param Map pointer to Map_info structure
33
34 \return string containing OGR/PostGIS datasource name
35 \return NULL on error (map format is native)
36 */
38{
39 if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
40 return Map->fInfo.ogr.dsn;
41 }
42 else if (Map->format == GV_FORMAT_POSTGIS) {
43#ifndef HAVE_POSTGRES
44 G_warning(_("GRASS is not compiled with PostgreSQL support"));
45#endif
46 return Map->fInfo.pg.db_name;
47 }
48
49 const char *mname = Vect_get_full_name(Map);
50 G_debug(1, "Native vector format detected for <%s>", mname);
51 G_free((void *)mname);
52
53 return NULL;
54}
55
56/*!
57 \brief Get layer name (relevant only for non-native formats)
58
59 Returns:
60 - layer name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
61 - table name for PostGIS format (GV_FORMAT_POSTGIS) including schema
62 (<schema>.<table>)
63
64 Note: allocated string should be freed by G_free()
65
66 \param Map pointer to Map_info structure
67
68 \return string containing layer name
69 \return NULL on error (map format is native)
70 */
72{
73 char *name;
74
75 name = NULL;
76 if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
77 name = G_store(Map->fInfo.ogr.layer_name);
78 }
79 else if (Map->format == GV_FORMAT_POSTGIS) {
80#ifndef HAVE_POSTGRES
81 G_warning(_("GRASS is not compiled with PostgreSQL support"));
82#endif
83 G_asprintf(&name, "%s.%s", Map->fInfo.pg.schema_name,
84 Map->fInfo.pg.table_name);
85 }
86 else {
87 const char *mname = Vect_get_full_name(Map);
88 G_debug(1, "Native vector format detected for <%s>", mname);
89 G_free((void *)mname);
90 }
91
92 return name;
93}
94
95/*!
96 \brief Get format info as string (relevant only for non-native formats)
97
98 \param Map pointer to Map_info structure
99
100 \return string containing name of OGR format
101 \return "PostgreSQL" for PostGIS format (GV_FORMAT_POSTGIS)
102 \return NULL on error (or on missing OGR/PostgreSQL support)
103 */
105{
106 if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
107 if (!Map->fInfo.ogr.ds)
108 return NULL;
109
110 return OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds));
111 }
112 else if (Map->format == GV_FORMAT_POSTGIS) {
113 return "PostgreSQL";
114 }
115
116 return NULL;
117}
118
119/*!
120 \brief Get geometry type as string (relevant only for non-native formats)
121
122 Note: All inner spaces are removed, function returns feature type in
123 lowercase.
124
125 \param Map pointer to Map_info structure
126
127 \return allocated string containing geometry type info
128 (point, linestring, polygon, ...)
129 \return NULL on error (map format is native)
130 */
132{
133 int dim;
134 char *ftype, *ftype_tmp;
135
136 ftype_tmp = ftype = NULL;
137 if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
140
141 if (!Map->fInfo.ogr.layer)
142 return NULL;
143
144 dim = -1;
145
146 Ogr_feature_defn = OGR_L_GetLayerDefn(Map->fInfo.ogr.layer);
148
150 }
151 else if (Map->format == GV_FORMAT_POSTGIS) {
152#ifndef HAVE_POSTGRES
153 G_warning(_("GRASS is not compiled with PostgreSQL support"));
154#else
155 char stmt[DB_SQL_MAX];
156
157 const struct Format_info_pg *pg_info;
158
159 PGresult *res;
160
161 pg_info = &(Map->fInfo.pg);
162 snprintf(stmt, sizeof(stmt),
163 "SELECT type,coord_dimension FROM geometry_columns "
164 "WHERE f_table_schema = '%s' AND f_table_name = '%s'",
165 pg_info->schema_name, pg_info->table_name);
166 G_debug(2, "SQL: %s", stmt);
167
168 res = PQexec(pg_info->conn, stmt);
170 PQntuples(res) != 1) {
171 G_debug(1, "Unable to get feature type: %s",
173 return NULL;
174 }
176 dim = atoi(PQgetvalue(res, 0, 1));
177
178 PQclear(res);
179#endif
180 }
181
182 if (!ftype_tmp)
183 return NULL;
184
185 ftype = G_str_replace(ftype_tmp, " ", "");
187 ftype_tmp = NULL;
189
190 if (dim == 3) {
191 size_t len = 3 + strlen(ftype) + 1;
192 ftype_tmp = (char *)G_malloc(len);
193 snprintf(ftype_tmp, len, "3D %s", ftype);
194 G_free(ftype);
196 }
197
198 return ftype;
199}
200
201/*!
202 \brief Get header info for non-native formats
203
204 \param Map pointer to Map_info structure
205
206 \return pointer to Format_info structure
207 \return NULL for native format
208 */
210{
211 /* do not check Map-format which is native (see
212 * GRASS_VECTOR_EXTERNAL_IMMEDIATE) */
213
214 if (Map->fInfo.ogr.driver_name || Map->fInfo.pg.conninfo)
215 return &(Map->fInfo);
216
217 return NULL;
218}
219
220/*!
221 \brief Get topology type (relevant only for non-native formats)
222
223 \param Map pointer to Map_info structure
224 \param[out] toposchema Topology schema name or NULL
225 \param[out] topogeom TopoGeometry column name or NULL
226 \param[out] topo_geo_only TRUE for Topo-Geo data model or NULL
227
228 \return GV_TOPO_NATIVE for native format
229 \return GV_TOPO_PSEUDO for pseudo-topology
230 \return GV_TOPO_POSTGIS for PostGIS Topology
231 */
233 char **topogeom, int *topo_geo_only)
234{
235 if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
236 return GV_TOPO_PSEUDO;
237 }
238
239 if (Map->format == GV_FORMAT_POSTGIS) {
240 const struct Format_info_pg *pg_info;
241
242 pg_info = &(Map->fInfo.pg);
243 if (pg_info->toposchema_name) {
244 if (toposchema)
245 *toposchema = G_store(pg_info->toposchema_name);
246 if (topogeom)
247 *topogeom = G_store(pg_info->topogeom_column);
248 if (topo_geo_only)
249 *topo_geo_only = pg_info->topo_geo_only;
250
251 return GV_TOPO_POSTGIS;
252 }
253 else {
254 return GV_TOPO_PSEUDO;
255 }
256 }
257
258 return GV_TOPO_NATIVE;
259}
#define NULL
Definition ccmath.h:32
#define DB_SQL_MAX
Definition dbmi.h:142
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:139
int G_asprintf(char **, const char *,...) __attribute__((format(printf
void G_str_to_lower(char *)
Convert string to lower case.
Definition strings.c:383
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
char * G_str_replace(const char *, const char *, const char *)
Replace all occurrences of old_str in buffer with new_str.
Definition strings.c:189
int G_debug(int, const char *,...) __attribute__((format(printf
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
#define GV_FORMAT_POSTGIS
PostGIS format.
Definition dig_defines.h:89
#define GV_TOPO_POSTGIS
PostGIS topology - external PostGIS format.
Definition dig_defines.h:96
#define GV_TOPO_NATIVE
GRASS topology - native format.
Definition dig_defines.h:92
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
Definition dig_defines.h:87
#define GV_FORMAT_OGR
OGR format.
Definition dig_defines.h:85
#define GV_TOPO_PSEUDO
Pseudo-topology - external simple features (OGR/PostGIS) format.
Definition dig_defines.h:94
#define _(str)
Definition glocale.h:10
const char * Vect_get_finfo_geometry_type(struct Map_info *Map)
Get geometry type as string (relevant only for non-native formats)
int Vect_get_finfo_topology_info(struct Map_info *Map, char **toposchema, char **topogeom, int *topo_geo_only)
Get topology type (relevant only for non-native formats)
const struct Format_info * Vect_get_finfo(struct Map_info *Map)
Get header info for non-native formats.
const char * Vect_get_finfo_dsn_name(struct Map_info *Map)
Get datasource name (relevant only for non-native formats)
const char * Vect_get_finfo_format_info(struct Map_info *Map)
Get format info as string (relevant only for non-native formats)
char * Vect_get_finfo_layer_name(struct Map_info *Map)
Get layer name (relevant only for non-native formats)
const char * name
Definition named_colr.c:6
Non-native format info (PostGIS)
PGresult * res
int topo_geo_only
Topology format.
Non-native format info (currently only OGR is implemented)
Vector map info.