GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71746
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
constraint.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/constraint.c
3 
4  \brief Vector library - constraints for reading features
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  These routines can affect the Vect_read_next_line() functions by
9  restricting what they return. They are applied on a per map basis.
10 
11  These do not affect the lower level direct read functions.
12 
13  Normally, all 'Alive' lines will be returned unless overridden by
14  this function. You can specified all the types you are interested
15  in (by oring their types together). You can use this to say exclude
16  'boundary' type features.
17 
18  By default all DEAD lines are ignored by the Vect_read_next_line()
19  functions. This too can be overridden by including their types.
20 
21  (C) 2001-2009, 2011-2012 by the GRASS Development Team
22 
23  This program is free software under the GNU General Public License
24  (>=v2). Read the file COPYING that comes with GRASS for details.
25 
26  \author Original author CERL, probably Dave Gerdes or Mike Higgins.
27  \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
28  */
29 
30 #include <grass/vector.h>
31 #include <grass/glocale.h>
32 
33 /*!
34  \brief Set constraint region
35 
36  Vect_read_next_line() will read only features inside of given
37  region or features with overlapping bounding box.
38 
39  \note Constraint is ignored for random access - Vect_read_line().
40 
41  \param Map pointer to Map_info struct
42  \param n,s,e,w,t,b bbox definition (north, south, east, west, top, and bottom coordinates)
43 
44  \return 0 on success
45  \return -1 on error (invalid region)
46  */
48  double n, double s, double e, double w,
49  double t, double b)
50 {
51  if (n <= s)
52  return -1;
53  if (e <= w)
54  return -1;
55 
57  Map->constraint.box.N = n;
58  Map->constraint.box.S = s;
59  Map->constraint.box.E = e;
60  Map->constraint.box.W = w;
61  Map->constraint.box.T = t;
62  Map->constraint.box.B = b;
63  Map->head.proj = G_projection();
64 
65  return 0;
66 }
67 
68 /*!
69  \brief Get constraint box
70 
71  Constraint box can be defined by Vect_set_constraint_region().
72 
73  \param Map vector map
74  \param[out] Box bounding box
75 
76  \return 0 on success
77  \return -1 no region constraint defined
78  */
79 int Vect_get_constraint_box(const struct Map_info *Map, struct bound_box * Box)
80 {
81  if (!Map->constraint.region_flag)
82  return -1;
83 
84  Box->N = Map->constraint.box.N;
85  Box->S = Map->constraint.box.S;
86  Box->E = Map->constraint.box.E;
87  Box->W = Map->constraint.box.W;
88  Box->T = Map->constraint.box.T;
89  Box->B = Map->constraint.box.B;
90 
91  return 0;
92 }
93 
94 /*!
95  \brief Set constraint type
96 
97  Vect_read_next_line() will read only features of given
98  type. Constraint is ignored for random access - Vect_read_line().
99 
100  \param Map pointer to Map_info struct
101  \param type constraint feature type (GV_POINT, GV_LINE, ...)
102 
103  \return 0 on success
104  \return -1 invalid feature type
105 */
106 int Vect_set_constraint_type(struct Map_info *Map, int type)
107 {
108  if (!(type & (GV_POINTS | GV_LINES | GV_FACE | GV_KERNEL)))
109  return -1;
110 
111  Map->constraint.type = type;
112  Map->constraint.type_flag = TRUE;
113 
114  return 0;
115 }
116 
117 /*!
118  \brief Remove all constraints
119 
120  \param Map pointer to Map_info struct
121  */
123 {
125  Map->constraint.type_flag = FALSE;
126  Map->constraint.field_flag = FALSE;
127 }
128 
129 /*!
130  \brief Set constraint field
131 
132  Vect_read_next_line() will read only features of given type. Note
133  that categories must be read otherwise this constraint is
134  ignored. Constraint is ignored for random access -
135  Vect_read_line().
136 
137  Ignored for non-native vector formats.
138 
139  Note: Field is called layer on user level.
140 
141  \param Map pointer to Map_info struct
142  \param field field number (-1 for all fields)
143 
144  \return 0 on success
145  \return -1 invalid field
146 */
147 int Vect_set_constraint_field(struct Map_info *Map, int field)
148 {
149  if (Map->format != GV_FORMAT_NATIVE) {
150  G_warning(_("Layer constraint ignored for non-native vector formats"));
151  return -1;
152  }
153 
154  if (field == -1) {
155  Map->constraint.field_flag = FALSE;
156  return 0;
157  }
158  if (field < 1) {
159  return -1;
160  }
161  Map->constraint.field = field;
162  Map->constraint.field_flag = TRUE;
163 
164  return 0;
165 }
#define TRUE
Definition: gis.h:49
Bounding box.
Definition: dig_structs.h:65
double W
West.
Definition: dig_structs.h:82
int Vect_set_constraint_type(struct Map_info *Map, int type)
Set constraint type.
Definition: constraint.c:106
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don&#39;t change GV_FORMAT_* values, this order is hardcoded in li...
Definition: dig_defines.h:83
int region_flag
Non-zero value to enable region constraint.
Definition: dig_structs.h:1362
int Vect_set_constraint_field(struct Map_info *Map, int field)
Set constraint field.
Definition: constraint.c:147
float Box[8][3]
Vertices for box.
Definition: gsd_objs.c:1421
#define GV_POINTS
Definition: dig_defines.h:191
double E
East.
Definition: dig_structs.h:78
int type
Feature type constraint.
Definition: dig_structs.h:1374
double N
North.
Definition: dig_structs.h:70
int field_flag
Non-zero value to enable field constraint.
Definition: dig_structs.h:1378
double t
Definition: r_raster.c:39
#define GV_FACE
Definition: dig_defines.h:186
double b
Definition: r_raster.c:39
struct Map_info::@11 constraint
Constraints for sequential feature access.
#define FALSE
Definition: gis.h:53
double B
Bottom.
Definition: dig_structs.h:90
int Vect_get_constraint_box(const struct Map_info *Map, struct bound_box *Box)
Get constraint box.
Definition: constraint.c:79
double T
Top.
Definition: dig_structs.h:86
struct bound_box box
Region (bbox) constraint.
Definition: dig_structs.h:1366
int type_flag
Non-zero value to enable feature type constraint.
Definition: dig_structs.h:1370
struct dig_head head
Header info.
Definition: dig_structs.h:1403
int Vect_set_constraint_region(struct Map_info *Map, double n, double s, double e, double w, double t, double b)
Set constraint region.
Definition: constraint.c:47
Vector map info.
Definition: dig_structs.h:1259
double S
South.
Definition: dig_structs.h:74
#define _(str)
Definition: glocale.h:13
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1271
#define GV_LINES
Definition: dig_defines.h:192
void Vect_remove_constraints(struct Map_info *Map)
Remove all constraints.
Definition: constraint.c:122
int field
Field number constraint (see line_cats structure)
Definition: dig_structs.h:1382
int G_projection(void)
Query cartographic projection.
Definition: proj1.c:32
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
#define GV_KERNEL
Definition: dig_defines.h:187