GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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  */
40 int V2_read_line_sfa(struct Map_info *Map, struct line_pnts *line_p,
41  struct line_cats *line_c, int line)
42 {
43 #if defined HAVE_OGR || defined HAVE_POSTGRES
44  int type;
45  struct P_line *Line;
46 
47  G_debug(4, "V2_read_line_sfa() line = %d", line);
48 
49  if (line < 1 || line > Map->plus.n_lines) {
50  G_warning(_("Attempt to access feature with invalid id (%d)"), line);
51  return -1;
52  }
53 
54  Line = Map->plus.Line[line];
55  if (Line == NULL) {
56  G_warning(_("Attempt to access dead feature %d"), line);
57  return -1;
58  }
59 
60  if (Line->type == GV_CENTROID) {
61  /* read centroid for topo */
62  if (line_p != NULL) {
63  int i, found;
64  struct bound_box box;
65  struct boxlist list;
66  struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
67 
68  G_debug(4, "Centroid: area = %d", topo->area);
69  Vect_reset_line(line_p);
70 
71  if (topo->area > 0 && topo->area <= Map->plus.n_areas) {
72  /* get area bbox */
73  Vect_get_area_box(Map, topo->area, &box);
74  /* search in spatial index for centroid with area bbox */
76  Vect_select_lines_by_box(Map, &box, Line->type, &list);
77 
78  found = -1;
79  for (i = 0; i < list.n_values; i++) {
80  if (list.id[i] == line) {
81  found = i;
82  break;
83  }
84  }
85 
86  if (found > -1) {
87  Vect_append_point(line_p, list.box[found].E,
88  list.box[found].N, 0.0);
89  }
90  else {
91  G_warning(
92  _("Unable to construct centroid for area %d. Skipped."),
93  topo->area);
94  }
95  }
96  else {
97  G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
98  }
99  }
100 
101  if (line_c != NULL) {
102  /* cat = fid and offset = fid for centroid */
103  Vect_reset_cats(line_c);
104  Vect_cat_set(line_c, 1, (int)Line->offset);
105  }
106 
107  return GV_CENTROID;
108  }
109 
110  if (!line_p && !line_c)
111  return Line->type;
112 
113  if (Map->format == GV_FORMAT_POSTGIS)
114  type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
115  else
116  type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
117 
118  if (type != Line->type) {
119  G_warning(_("Unexpected feature type (%d) - should be (%d)"), type,
120  Line->type);
121  return -1;
122  }
123 
124  return type;
125 #else
126  G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
127  return -1;
128 #endif
129 }
#define NULL
Definition: ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
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:179
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)
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
Definition: dig_defines.h:186
#define GV_FORMAT_POSTGIS
PostGIS format.
Definition: dig_defines.h:89
int dig_init_boxlist(struct boxlist *, int)
#define TRUE
Definition: gis.h:79
#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.
Definition: dig_structs.h:1243
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1255
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1270
Vector geometry.
Definition: dig_structs.h:1553
char type
Line type.
Definition: dig_structs.h:1564
off_t offset
Offset in coor file for line.
Definition: dig_structs.h:1571
void * topo
Topology info.
Definition: dig_structs.h:1577
Centroid topology.
Definition: dig_structs.h:1514
plus_t area
Area number, negative for duplicate centroid.
Definition: dig_structs.h:1518
struct P_line ** Line
Array of vector geometries.
Definition: dig_structs.h:871
plus_t n_lines
Current number of lines.
Definition: dig_structs.h:931
plus_t n_areas
Current number of areas.
Definition: dig_structs.h:935
Bounding box.
Definition: dig_structs.h:64
List of bounding boxes with id.
Definition: dig_structs.h:1723
Feature category info.
Definition: dig_structs.h:1677
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
Definition: manage.h:4