GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72895
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
#define TRUE
Definition: gis.h:49
plus_t n_areas
Current number of areas.
Definition: dig_structs.h:951
int Vect_select_lines_by_box(struct Map_info *Map, const struct bound_box *Box, int type, struct boxlist *list)
Select lines with bounding boxes by box.
Definition: sindex.c:34
Bounding box.
Definition: dig_structs.h:65
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
int Vect_get_area_box(const struct Map_info *Map, int area, struct bound_box *Box)
Get bounding box of area.
#define GV_CENTROID
Definition: dig_defines.h:185
double E
East.
Definition: dig_structs.h:78
int Vect_reset_cats(struct line_cats *Cats)
Reset category structure to make sure cats structure is clean to be re-used.
#define NULL
Definition: ccmath.h:32
int V1_read_line_ogr(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, off_t offset)
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
int Vect_append_point(struct line_pnts *Points, double x, double y, double z)
Appends one point to the end of a line.
Definition: line.c:149
plus_t n_lines
Current number of lines.
Definition: dig_structs.h:947
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:160
Feature category info.
Definition: dig_structs.h:1702
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
Add new field/cat to category structure if doesn&#39;t exist yet.
double N
North.
Definition: dig_structs.h:70
char type
Line type.
Definition: dig_structs.h:1586
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 G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1286
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
#define _(str)
Definition: glocale.h:13
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1271
void Vect_reset_line(struct line_pnts *Points)
Reset line.
Definition: line.c:130
int V1_read_line_pg(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, off_t offset)
Read feature from PostGIS layer at given offset (level 1 without topology)
Definition: read_pg.c:237
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204