23
20
#include <sys/types.h>
24
21
#include <sys/stat.h>
26
#include <grass/Vect.h>
27
#include <grass/gis.h>
23
#include <grass/vector.h>
24
#include <grass/dbmi.h>
28
25
#include <grass/glocale.h>
31
28
#include <ogr_api.h>
34
\brief Open existing vector map
36
Map->name and Map->mapset must be set before.
38
\param Map pointer to vector map
39
\param update non-zero for write mode, otherwise read-only
40
(write mode is currently not supported)
32
\brief Open existing OGR layer on non-topological level
34
Note: Map->name, Map->mapset, Map->fInfo.ogr.dsn and
35
Map->fInfo.ogr.layer_name must be set before.
37
\param[in,out] Map pointer to Map_info structure
38
\param update TRUE for write mode, otherwise read-only
45
43
int V1_open_old_ogr(struct Map_info *Map, int update)
47
46
int i, layer, nLayers;
48
struct Format_info_ogr *ogr_info;
48
50
OGRDataSourceH Ogr_ds;
49
OGRLayerH Ogr_layer = NULL;
50
52
OGRFeatureDefnH Ogr_featuredefn;
51
53
OGRwkbGeometryType Ogr_geom_type;
54
G_fatal_error(_("OGR format cannot be updated"));
56
Ogr_geom_type = wkbUnknown;
58
G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
59
Map->fInfo.ogr.layer_name);
58
ogr_info = &(Map->fInfo.ogr);
60
G_fatal_error(_("OGR datasource not defined"));
64
if (!ogr_info->layer_name) {
65
G_fatal_error(_("OGR layer not defined"));
69
G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", ogr_info->dsn,
70
ogr_info->layer_name);
63
/*Data source handle */
64
Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
74
/* open data source handle */
75
Ogr_ds = OGROpen(ogr_info->dsn, FALSE, NULL);
65
76
if (Ogr_ds == NULL)
66
77
G_fatal_error(_("Unable to open OGR data source '%s'"),
68
Map->fInfo.ogr.ds = Ogr_ds;
79
ogr_info->ds = Ogr_ds;
81
/* get layer number */
72
83
nLayers = OGR_DS_GetLayerCount(Ogr_ds);
73
84
G_debug(2, "%d layers found in data source", nLayers);
75
86
for (i = 0; i < nLayers; i++) {
76
Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
87
Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
77
88
Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
78
if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
79
Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
89
if (strcmp(OGR_FD_GetName(Ogr_featuredefn), ogr_info->layer_name) == 0) {
90
Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
85
96
OGR_DS_Destroy(Ogr_ds);
86
G_fatal_error(_("Unable to open layer <%s>"),
87
Map->fInfo.ogr.layer_name);
97
G_fatal_error(_("OGR layer <%s> not found"),
98
ogr_info->layer_name);
89
100
G_debug(2, "OGR layer %d opened", layer);
91
Map->fInfo.ogr.layer = Ogr_layer;
93
Map->fInfo.ogr.lines = NULL;
94
Map->fInfo.ogr.lines_types = NULL;
95
Map->fInfo.ogr.lines_alloc = 0;
96
Map->fInfo.ogr.lines_num = 0;
97
Map->fInfo.ogr.lines_next = 0;
102
ogr_info->layer = Ogr_layer;
103
if (update && OGR_L_TestCapability(ogr_info->layer, OLCTransactions))
104
OGR_L_StartTransaction(ogr_info->layer);
99
106
switch(Ogr_geom_type) {
100
107
case wkbPoint25D: case wkbLineString25D: case wkbPolygon25D:
101
108
case wkbMultiPoint25D: case wkbMultiLineString25D: case wkbMultiPolygon25D:
102
109
case wkbGeometryCollection25D:
103
Map->head.with_z = WITH_Z;
110
Map->head.with_z = WITH_Z;
106
Map->head.with_z = WITHOUT_Z;
113
Map->head.with_z = WITHOUT_Z;
110
Map->fInfo.ogr.feature_cache = NULL;
111
Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
117
ogr_info->cache.fid = -1; /* FID >= 0 */
121
G_fatal_error(_("GRASS is not compiled with OGR support"));
117
\brief Open OGR specific level 2 files (feature index)
119
\param Map pointer to vector map
127
\brief Open existing OGR layer on topological level
129
This functions reads feature index (fidx) file required for
132
\param[in,out] Map pointer to Map_info structure
121
134
\return 0 success
124
137
int V2_open_old_ogr(struct Map_info *Map)
141
G_debug(3, "V2_open_old_ogr(): name = %s mapset = %s", Map->name,
144
if (Vect_open_fidx(Map, &(Map->fInfo.ogr.offset)) != 0) {
145
G_warning(_("Unable to open feature index file for vector map <%s>"),
146
Vect_get_full_name(Map));
147
G_zero(&(Map->fInfo.ogr.offset), sizeof(struct Format_info_offset));
150
Map->fInfo.ogr.next_line = 1; /* reset feature cache */
154
G_fatal_error(_("GRASS is not compiled with OGR support"));
160
\brief Prepare OGR datasource for creating new OGR layer (level 1)
162
New OGR layer is created when writing features by
165
\param[out] Map pointer to Map_info structure
166
\param name name of OGR layer to create
167
\param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
172
int V1_open_new_ogr(struct Map_info *Map, const char *name, int with_z)
177
struct Format_info_ogr *ogr_info;
179
OGRSFDriverH Ogr_driver;
180
OGRDataSourceH Ogr_ds;
182
OGRFeatureDefnH Ogr_featuredefn;
186
ogr_info = &(Map->fInfo.ogr);
188
G_debug(1, "V1_open_new_ogr(): name = %s with_z = %d", name, with_z);
189
Ogr_driver = OGRGetDriverByName(ogr_info->driver_name);
191
G_warning(_("Unable to get OGR driver <%s>"), ogr_info->driver_name);
194
ogr_info->driver = Ogr_driver;
196
/* TODO: creation options */
197
Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, ogr_info->dsn, NULL);
199
G_warning(_("Unable to create OGR data source '%s'"),
203
ogr_info->ds = Ogr_ds;
205
nlayers = OGR_DS_GetLayerCount(Ogr_ds);
206
for (i = 0; i < nlayers; i++) {
207
Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
208
Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
209
if (strcmp(OGR_FD_GetName(Ogr_featuredefn), name) == 0) {
210
if (G_get_overwrite()) {
211
G_warning(_("OGR layer <%s> already exists and will be overwritten"),
212
ogr_info->layer_name);
214
if (OGR_DS_DeleteLayer(Ogr_ds, i) != OGRERR_NONE) {
215
G_warning(_("Unable to delete OGR layer <%s>"),
216
ogr_info->layer_name);
221
G_fatal_error(_("OGR layer <%s> already exists in datasource '%s'"),
222
ogr_info->layer_name, ogr_info->dsn);
224
ogr_info->layer = NULL;
231
G_fatal_error(_("GRASS is not compiled with OGR support"));
237
\brief Open feature index file
239
\param[in,out] Map pointer to Map_info struct
240
\param[out] offset pointer to Format_info_offset (OGR or PG)
245
int Vect_open_fidx(struct Map_info *Map, struct Format_info_offset *offset)
126
247
char elem[GPATH_MAX];
127
248
char buf[5]; /* used for format version */
250
int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
130
253
struct Port_info port;
131
int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
133
G_debug(3, "V2_open_old_ogr()");
135
sprintf(elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
255
G_debug(1, "Vect_open_fidx(): name = %s mapset = %s format = %d",
256
Map->name, Map->mapset, Map->format);
258
sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
136
259
dig_file_init(&fp);
137
fp.file = G_fopen_old(elem, "fidx", Map->mapset);
260
fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
138
261
if (fp.file == NULL) {
139
G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
140
Map->name, Map->mapset);
262
G_debug(1, "unable to open fidx file for vector map <%s>",
263
Vect_get_full_name(Map));
145
268
if (0 >= dig__fread_port_C(buf, 5, &fp))
147
270
Version_Major = buf[0];
148
271
Version_Minor = buf[1];
154
276
/* check version numbers */
155
277
if (Version_Major > 5 || Version_Minor > 0) {
156
278
if (Back_Major > 5 || Back_Minor > 0) {
157
279
G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
158
280
" Try to rebuild topology or upgrade GRASS."),
159
281
Version_Major, Version_Minor);
162
284
G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
163
285
" Consider to rebuild topology or upgrade GRASS."),