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