GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
read_sfa.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/read_sfa.c
3
4 \brief Vector library - reading features - simple feature access
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 See read_ogr.c (OGR interface) and read_pg.c (PostGIS interface)
9 for implementation issues.
10
11 SPDX-FileCopyrightText: 2011-2012 GRASS Development Team
12 SPDX-License-Identifier: GPL-2.0-or-later
13
14 \author Martin Landa <landa.martin gmail.com>
15 */
16
17#include <grass/vector.h>
18#include <grass/glocale.h>
19
20/*!
21 \brief Reads feature from OGR/PostGIS layer on topological level.
22
23 This function implements random access on level 2.
24
25 Note: Topology must be built at level >= GV_BUILD_BASE
26
27 \param Map pointer to Map_info structure
28 \param[out] line_p container used to store line points within
29 (pointer to line_pnts struct)
30 \param[out] line_c container used to store line categories within
31 (pointer to line_cats struct)
32 \param line feature id (starts at 1)
33
34 \return feature type
35 \return -2 no more features
36 \return -1 on failure
37 */
39 struct line_cats *line_c, int line)
40{
41 int type;
42 struct P_line *Line;
43
44 G_debug(4, "V2_read_line_sfa() line = %d", line);
45
46 if (line < 1 || line > Map->plus.n_lines) {
47 G_warning(_("Attempt to access feature with invalid id (%d)"), line);
48 return -1;
49 }
50
51 Line = Map->plus.Line[line];
52 if (Line == NULL) {
53 G_warning(_("Attempt to access dead feature %d"), line);
54 return -1;
55 }
56
57 if (Line->type == GV_CENTROID) {
58 /* read centroid for topo */
59 if (line_p != NULL) {
60 int i, found;
61 struct bound_box box;
62 struct boxlist list;
63 struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
64
65 G_debug(4, "Centroid: area = %d", topo->area);
67
68 if (topo->area > 0 && topo->area <= Map->plus.n_areas) {
69 /* get area bbox */
70 Vect_get_area_box(Map, topo->area, &box);
71 /* search in spatial index for centroid with area bbox */
73 Vect_select_lines_by_box(Map, &box, Line->type, &list);
74
75 found = -1;
76 for (i = 0; i < list.n_values; i++) {
77 if (list.id[i] == line) {
78 found = i;
79 break;
80 }
81 }
82
83 if (found > -1) {
85 list.box[found].N, 0.0);
86 }
87 else {
89 _("Unable to construct centroid for area %d. Skipped."),
90 topo->area);
91 }
92 }
93 else {
94 G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
95 }
96 }
97
98 if (line_c != NULL) {
99 /* cat = fid and offset = fid for centroid */
101 Vect_cat_set(line_c, 1, (int)Line->offset);
102 }
103
104 return GV_CENTROID;
105 }
106
107 if (!line_p && !line_c)
108 return Line->type;
109
110 if (Map->format == GV_FORMAT_POSTGIS)
111 type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
112 else
113 type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
114
115 if (type != Line->type) {
116 G_warning(_("Unexpected feature type (%d) - should be (%d)"), type,
117 Line->type);
118 return -1;
119 }
120
121 return type;
122}
#define NULL
Definition ccmath.h:32
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_reset_cats(struct line_cats *)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn't exist yet.
int V1_read_line_ogr(struct Map_info *, struct line_pnts *, struct line_cats *, off_t)
Read feature from OGR layer at given offset (level 1 without topology)
Definition read_ogr.c:165
int Vect_get_area_box(struct Map_info *, int, struct bound_box *)
Get bounding box of area.
int Vect_select_lines_by_box(struct Map_info *, const struct bound_box *, int, struct boxlist *)
Select lines with bounding boxes by box.
Definition sindex.c:30
void Vect_reset_line(struct line_pnts *)
Reset line.
Definition line.c:127
int V1_read_line_pg(struct Map_info *, struct line_pnts *, struct line_cats *, off_t)
Read feature from PostGIS layer at given offset (level 1 without topology)
Definition read_pg.c:243
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
Definition line.c:146
#define GV_CENTROID
#define GV_FORMAT_POSTGIS
PostGIS format.
Definition dig_defines.h:89
int dig_init_boxlist(struct boxlist *, int)
#define TRUE
Definition gis.h:75
#define _(str)
Definition glocale.h:10
int V2_read_line_sfa(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Reads feature from OGR/PostGIS layer on topological level.
Definition read_sfa.c:38
Vector map info.
Vector geometry.
char type
Line type.
off_t offset
Offset in coor file for line.
void * topo
Topology info.
Centroid topology.
plus_t area
Area number, negative for duplicate centroid.
Bounding box.
Definition dig_structs.h:62
List of bounding boxes with id.
Feature category info.
Feature geometry info - coordinates.
Definition manage.h:4