GRASS 8 Programmer's Manual 8.6.0dev(2026)-5f4f7ad06c
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 (C) 2011-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 Martin Landa <landa.martin gmail.com>
17 */
18
19#include <grass/vector.h>
20#include <grass/glocale.h>
21
22/*!
23 \brief Reads feature from OGR/PostGIS layer on topological level.
24
25 This function implements random access on level 2.
26
27 Note: Topology must be built at level >= GV_BUILD_BASE
28
29 \param Map pointer to Map_info structure
30 \param[out] line_p container used to store line points within
31 (pointer to line_pnts struct)
32 \param[out] line_c container used to store line categories within
33 (pointer to line_cats struct)
34 \param line feature id (starts at 1)
35
36 \return feature type
37 \return -2 no more features
38 \return -1 on failure
39 */
41 struct line_cats *line_c, int line)
42{
43 int type;
44 struct P_line *Line;
45
46 G_debug(4, "V2_read_line_sfa() line = %d", line);
47
48 if (line < 1 || line > Map->plus.n_lines) {
49 G_warning(_("Attempt to access feature with invalid id (%d)"), line);
50 return -1;
51 }
52
53 Line = Map->plus.Line[line];
54 if (Line == NULL) {
55 G_warning(_("Attempt to access dead feature %d"), line);
56 return -1;
57 }
58
59 if (Line->type == GV_CENTROID) {
60 /* read centroid for topo */
61 if (line_p != NULL) {
62 int i, found;
63 struct bound_box box;
64 struct boxlist list;
65 struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
66
67 G_debug(4, "Centroid: area = %d", topo->area);
69
70 if (topo->area > 0 && topo->area <= Map->plus.n_areas) {
71 /* get area bbox */
72 Vect_get_area_box(Map, topo->area, &box);
73 /* search in spatial index for centroid with area bbox */
75 Vect_select_lines_by_box(Map, &box, Line->type, &list);
76
77 found = -1;
78 for (i = 0; i < list.n_values; i++) {
79 if (list.id[i] == line) {
80 found = i;
81 break;
82 }
83 }
84
85 if (found > -1) {
87 list.box[found].N, 0.0);
88 }
89 else {
91 _("Unable to construct centroid for area %d. Skipped."),
92 topo->area);
93 }
94 }
95 else {
96 G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
97 }
98 }
99
100 if (line_c != NULL) {
101 /* cat = fid and offset = fid for centroid */
103 Vect_cat_set(line_c, 1, (int)Line->offset);
104 }
105
106 return GV_CENTROID;
107 }
108
109 if (!line_p && !line_c)
110 return Line->type;
111
112 if (Map->format == GV_FORMAT_POSTGIS)
113 type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
114 else
115 type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
116
117 if (type != Line->type) {
118 G_warning(_("Unexpected feature type (%d) - should be (%d)"), type,
119 Line->type);
120 return -1;
121 }
122
123 return type;
124}
#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:167
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:32
void Vect_reset_line(struct line_pnts *)
Reset line.
Definition line.c:129
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:245
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
Definition line.c:148
#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:78
#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:40
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