GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
build_ogr.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/build_ogr.c
3
4 \brief Vector library - Building topology for OGR
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 Category: FID, not all layer have FID, OGRNullFID is defined
9 (5/2004) as -1, so FID should be only >= 0
10
11 (C) 2001-2010, 2012 by the GRASS Development Team
12
13 This program is free software under the GNU General Public License
14 (>=v2). Read the file COPYING that comes with GRASS for details.
15
16 \author Radim Blazek, Piero Cavalieri
17 \author Various updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
18 */
19
20#include <string.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <grass/vector.h>
24#include <grass/glocale.h>
25
26#include <ogr_api.h>
27#include <cpl_error.h>
28
29#include "local_proto.h"
30
31/*!
32 \brief Build pseudo-topology (simple features) for OGR layer
33
34 Build levels:
35 - GV_BUILD_NONE
36 - GV_BUILD_BASE
37 - GV_BUILD_ATTACH_ISLES
38 - GV_BUILD_CENTROIDS
39 - GV_BUILD_ALL
40
41 \param Map pointer to Map_info structure
42 \param build build level
43
44 \return 1 on success
45 \return 0 on error
46 */
48{
49 struct Plus_head *plus;
51
52 plus = &(Map->plus);
53 ogr_info = &(Map->fInfo.ogr);
54
55 G_debug(1, "Vect_build_ogr(): dsn='%s' layer='%s', build=%d", ogr_info->dsn,
56 ogr_info->layer_name, build);
57
58 if (build == plus->built)
59 return 1; /* do nothing */
60
61 /* TODO move this init to better place (Vect_open_ ?), because in
62 theory build may be reused on level2 */
63 if (build >= plus->built && build > GV_BUILD_BASE) {
64 G_free((void *)ogr_info->offset.array);
65 G_zero(&(ogr_info->offset), sizeof(struct Format_info_offset));
66 }
67
68 if (!ogr_info->layer) {
69 G_warning(_("Empty OGR layer, nothing to build"));
70 return 0;
71 }
72
76 G_debug(1, "Unable to commit transaction");
78 }
79
80 /* test layer capabilities */
82 if (strcmp(OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds)),
83 "PostgreSQL") == 0)
84 G_warning(_("Feature table <%s> has no primary key defined"),
85 ogr_info->layer_name);
86 G_warning(_("Random read is not supported by OGR for this layer. "
87 "Unable to build topology."));
88 return 0;
89 }
90
91 if (build > GV_BUILD_NONE)
92 G_message(_("Using external data format '%s' (feature type '%s')"),
95
96 return Vect__build_sfa(Map, build);
97}
98
99/*!
100 \brief Save feature index file for vector map
101
102 \param Map pointer to Map_info structure
103 \param offset pointer to Format_info_offset struct
104 (see Format_info_ogr and Format_info_pg struct for implementation issues)
105
106 \return 1 on success
107 \return 0 on error
108 */
110{
111 char fname[GPATH_MAX], elem[GPATH_MAX];
112 char buf[5];
113 long length;
114 struct gvfile fp;
115 struct Port_info port;
116
117 if (strcmp(Map->mapset, G_mapset()) != 0 || Map->support_updated == FALSE ||
118 Map->plus.built != GV_BUILD_ALL)
119 return 1;
120
121 length = 9;
122
123 snprintf(elem, sizeof(elem), "%s/%s", GV_DIRECTORY, Map->name);
125 G_debug(4, "Open fidx: %s", fname);
126 dig_file_init(&fp);
127 fp.file = fopen(fname, "w");
128 if (fp.file == NULL) {
129 G_warning(_("Unable to open fidx file for write <%s>"), fname);
130 return 0;
131 }
132
134 dig_set_cur_port(&port);
135
136 /* Header */
137 /* bytes 1 - 5 */
138 buf[0] = 5;
139 buf[1] = 0;
140 buf[2] = 5;
141 buf[3] = 0;
142 buf[4] = (char)dig__byte_order_out();
143 if (0 >= dig__fwrite_port_C(buf, 5, &fp))
144 return 0;
145
146 /* bytes 6 - 9 : header size */
147 if (0 >= dig__fwrite_port_L(&length, 1, &fp))
148 return 0;
149
150 /* Body */
151 /* number of records */
152 if (0 >= dig__fwrite_port_I(&(offset->array_num), 1, &fp))
153 return 0;
154
155 /* offsets */
156 if (0 >= dig__fwrite_port_I(offset->array, offset->array_num, &fp))
157 return 0;
158
159 G_debug(3, "Vect_save_fidx(): offset_num = %d", offset->array_num);
160
161 fclose(fp.file);
162
163 return 1;
164}
int Vect_build_ogr(struct Map_info *Map, int build)
Build pseudo-topology (simple features) for OGR layer.
Definition build_ogr.c:47
int Vect_save_fidx(struct Map_info *Map, struct Format_info_offset *offset)
Save feature index file for vector map.
Definition build_ogr.c:109
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:23
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void G_warning(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:33
int Vect__build_sfa(struct Map_info *, int)
Build pseudo-topology (for simple features) - internal use only.
Definition build_sfa.c:696
const char * Vect_get_finfo_format_info(struct Map_info *)
Get format info as string (relevant only for non-native formats)
const char * Vect_get_finfo_geometry_type(struct Map_info *)
Get geometry type as string (relevant only for non-native formats)
#define GV_BUILD_NONE
Topology levels - nothing to build.
#define GV_DIRECTORY
Name of vector directory.
Definition dig_defines.h:8
#define GV_BUILD_BASE
Topology levels - basic level (without areas and isles)
#define GV_BUILD_ALL
Topology levels - build everything (currently same as GV_BUILD_CENTROIDS)
#define GV_FIDX_ELEMENT
External format (OGR), feature index.
Definition dig_defines.h:26
int dig__byte_order_out(void)
Get byte order.
Definition portable.c:1008
int dig__fwrite_port_C(const char *, size_t, struct gvfile *)
Write chars to the Portable Vector Format.
Definition portable.c:886
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition portable.c:900
int dig__fwrite_port_L(const long *, size_t, struct gvfile *)
Write longs to the Portable Vector Format.
Definition portable.c:703
int dig__fwrite_port_I(const int *, size_t, struct gvfile *)
Write integers to the Portable Vector Format.
Definition portable.c:758
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
Definition portable.c:996
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
Definition file.c:171
#define GPATH_MAX
Definition gis.h:199
#define FALSE
Definition gis.h:82
#define _(str)
Definition glocale.h:10
Data structure used for building pseudo-topology.
int * array
Offset list.
int array_num
Number of items in offset list.
Non-native format info (OGR)
struct Format_info_offset offset
Offset list used for building pseudo-topology.
Vector map info.
Basic topology-related info.
int built
Highest level of topology currently available.
Portability info.
File definition.
Definition dig_structs.h:92
FILE * file
File descriptor.
Definition dig_structs.h:96
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)