GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 imlementation 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 */
75  dig_init_boxlist(&list, TRUE);
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, list.box[found].N, 0.0);
88  }
89  else {
90  G_warning(_("Unable to construct centroid for area %d. Skipped."),
91  topo->area);
92  }
93  }
94  else {
95  G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
96  }
97  }
98 
99  if (line_c != NULL) {
100  /* cat = fid and offset = fid for centroid */
101  Vect_reset_cats(line_c);
102  Vect_cat_set(line_c, 1, (int) Line->offset);
103  }
104 
105  return GV_CENTROID;
106  }
107 
108  if (!line_p && !line_c)
109  return Line->type;
110 
111  if (Map->format == GV_FORMAT_POSTGIS)
112  type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
113  else
114  type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
115 
116  if (type != Line->type) {
117  G_warning(_("Unexpected feature type (%d) - should be (%d)"),
118  type, Line->type);
119  return -1;
120  }
121 
122  return type;
123 #else
124  G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
125  return -1;
126 #endif
127 }
int Vect_get_area_box(const struct Map_info *, int, struct bound_box *)
Get bounding box of area.
#define TRUE
Definition: gis.h:59
plus_t n_areas
Current number of areas.
Definition: dig_structs.h:951
Bounding box.
Definition: dig_structs.h:65
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
plus_t area
Area number, negative for duplicate centroid.
Definition: dig_structs.h:1537
int * id
Array of ids.
Definition: dig_structs.h:1755
off_t offset
Offset in coor file for line.
Definition: dig_structs.h:1593
Vector geometry.
Definition: dig_structs.h:1574
struct P_line ** Line
Array of vector geometries.
Definition: dig_structs.h:887
#define GV_CENTROID
Definition: dig_defines.h:185
double E
East.
Definition: dig_structs.h:78
#define NULL
Definition: ccmath.h:32
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 n_values
Number of items in the list.
Definition: dig_structs.h:1767
struct bound_box * box
Array of bounding boxes.
Definition: dig_structs.h:1759
plus_t n_lines
Current number of lines.
Definition: dig_structs.h:947
Feature category info.
Definition: dig_structs.h:1702
double N
North.
Definition: dig_structs.h:70
char type
Line type.
Definition: dig_structs.h:1586
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:34
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
Centroid topology.
Definition: dig_structs.h:1532
void * topo
Topology info.
Definition: dig_structs.h:1599
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
Definition: line.c:149
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1286
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:237
int Vect_reset_cats(struct line_cats *)
Reset category structure to make sure cats structure is clean to be re-used.
int dig_init_boxlist(struct boxlist *, int)
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:1259
#define GV_FORMAT_POSTGIS
PostGIS format.
Definition: dig_defines.h:89
List of bounding boxes with id.
Definition: dig_structs.h:1750
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1271
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_reset_line(struct line_pnts *)
Reset line.
Definition: line.c:130
int Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn&#39;t exist yet.