GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
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 SPDX-FileCopyrightText: 2001-2010, 2012 GRASS Development Team
12 SPDX-License-Identifier: GPL-2.0-or-later
13
14 \author Radim Blazek, Piero Cavalieri
15 \author Various updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
16 */
17
18#include <string.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <grass/vector.h>
22#include <grass/glocale.h>
23
24#include <ogr_api.h>
25#include <cpl_error.h>
26
27#include "local_proto.h"
28
29/*!
30 \brief Build pseudo-topology (simple features) for OGR layer
31
32 Build levels:
33 - GV_BUILD_NONE
34 - GV_BUILD_BASE
35 - GV_BUILD_ATTACH_ISLES
36 - GV_BUILD_CENTROIDS
37 - GV_BUILD_ALL
38
39 \param Map pointer to Map_info structure
40 \param build build level
41
42 \return 1 on success
43 \return 0 on error
44 */
46{
47 struct Plus_head *plus;
49
50 plus = &(Map->plus);
51 ogr_info = &(Map->fInfo.ogr);
52
53 G_debug(1, "Vect_build_ogr(): dsn='%s' layer='%s', build=%d", ogr_info->dsn,
54 ogr_info->layer_name, build);
55
56 if (build == plus->built)
57 return 1; /* do nothing */
58
59 /* TODO move this init to better place (Vect_open_ ?), because in
60 theory build may be reused on level2 */
61 if (build >= plus->built && build > GV_BUILD_BASE) {
62 G_free((void *)ogr_info->offset.array);
63 G_zero(&(ogr_info->offset), sizeof(struct Format_info_offset));
64 }
65
66 if (!ogr_info->layer) {
67 G_warning(_("Empty OGR layer, nothing to build"));
68 return 0;
69 }
70
74 G_debug(1, "Unable to commit transaction");
76 }
77
78 /* test layer capabilities */
80 if (strcmp(OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds)),
81 "PostgreSQL") == 0)
82 G_warning(_("Feature table <%s> has no primary key defined"),
83 ogr_info->layer_name);
84 G_warning(_("Random read is not supported by OGR for this layer. "
85 "Unable to build topology."));
86 return 0;
87 }
88
89 if (build > GV_BUILD_NONE)
90 G_message(_("Using external data format '%s' (feature type '%s')"),
93
94 return Vect__build_sfa(Map, build);
95}
96
97/*!
98 \brief Save feature index file for vector map
99
100 \param Map pointer to Map_info structure
101 \param offset pointer to Format_info_offset struct
102 (see Format_info_ogr and Format_info_pg struct for implementation issues)
103
104 \return 1 on success
105 \return 0 on error
106 */
108{
109 char fname[GPATH_MAX], elem[GPATH_MAX];
110 char buf[5];
111 long length;
112 struct gvfile fp;
113 struct Port_info port;
114
115 if (strcmp(Map->mapset, G_mapset()) != 0 || Map->support_updated == FALSE ||
116 Map->plus.built != GV_BUILD_ALL)
117 return 1;
118
119 length = 9;
120
121 snprintf(elem, sizeof(elem), "%s/%s", GV_DIRECTORY, Map->name);
123 G_debug(4, "Open fidx: %s", fname);
124 dig_file_init(&fp);
125 fp.file = fopen(fname, "w");
126 if (fp.file == NULL) {
127 G_warning(_("Unable to open fidx file for write <%s>"), fname);
128 return 0;
129 }
130
132 dig_set_cur_port(&port);
133
134 /* Header */
135 /* bytes 1 - 5 */
136 buf[0] = 5;
137 buf[1] = 0;
138 buf[2] = 5;
139 buf[3] = 0;
140 buf[4] = (char)dig__byte_order_out();
141 if (0 >= dig__fwrite_port_C(buf, 5, &fp))
142 return 0;
143
144 /* bytes 6 - 9 : header size */
145 if (0 >= dig__fwrite_port_L(&length, 1, &fp))
146 return 0;
147
148 /* Body */
149 /* number of records */
150 if (0 >= dig__fwrite_port_I(&(offset->array_num), 1, &fp))
151 return 0;
152
153 /* offsets */
154 if (0 >= dig__fwrite_port_I(offset->array, offset->array_num, &fp))
155 return 0;
156
157 G_debug(3, "Vect_save_fidx(): offset_num = %d", offset->array_num);
158
159 fclose(fp.file);
160
161 return 1;
162}
int Vect_build_ogr(struct Map_info *Map, int build)
Build pseudo-topology (simple features) for OGR layer.
Definition build_ogr.c:45
int Vect_save_fidx(struct Map_info *Map, struct Format_info_offset *offset)
Save feature index file for vector map.
Definition build_ogr.c:107
#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:21
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
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:31
int Vect__build_sfa(struct Map_info *, int)
Build pseudo-topology (for simple features) - internal use only.
Definition build_sfa.c:694
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:1006
int dig__fwrite_port_C(const char *, size_t, struct gvfile *)
Write chars to the Portable Vector Format.
Definition portable.c:884
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition portable.c:898
int dig__fwrite_port_L(const long *, size_t, struct gvfile *)
Write longs to the Portable Vector Format.
Definition portable.c:701
int dig__fwrite_port_I(const int *, size_t, struct gvfile *)
Write integers to the Portable Vector Format.
Definition portable.c:756
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
Definition portable.c:994
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
Definition file.c:169
#define GPATH_MAX
Definition gis.h:196
#define FALSE
Definition gis.h:79
#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)