RetrieveUsingSpatialFilter.cpp
Go to the documentation of this file.
1 // Examples
2 #include "DataAccessExamples.h"
3 #include <iostream>
4 
6 {
7 // Let's get a DataSet called 'br_munic_2001' in the 'public' schema
8 // and retrieve all its data
9 
10  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
11  {
12  std::unique_ptr<te::da::DataSet> dataset = ds->getDataSet("public.br_munic_2001");
13 
14  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
15 
16  int row = 0;
17 
18  std::cout << std::endl << "=== DataSet: public.br_munic_2001: 5 first rows" << std::endl;
19 
20  while(dataset->moveNext() && row<5)
21  {
22  std::string municName = dataset->getString(2);
23 
24  std::cout << std::endl << "City Name: " << municName;
25 
26  std::cout << std::endl << "ROW: " << row++ << "\t";
27 
28  te::gm::Geometry* g = (dataset->getGeometry(pos)).release();
29 
30  std::cout << dataset->getPropertyName(pos) << ": " << g->asText() << std::endl;
31 
32  delete g;
33  }
34 
35  std::cout << std::endl;
36  }
37 
38  // Defining 2 variables used in other examples...
39  te::gm::Geometry* geomMunic;
40  int srid;
41 
42 // now we will retrieve all cities that contains a given point
43  {
44  te::gm::Point pt(-43.6107606714293, -20.3913548070123, 4291);
45 
46  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("public.br_munic_2001", "geom", &pt, te::gm::INTERSECTS);
47 
48  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
49 
50  int row = 0;
51 
52  std::cout << std::endl << "=== DataSet: public.br_munic_2001: objects that intercepts location (-43.6107606714293, -20.3913548070123)" << std::endl;
53 
54  while(dataset->moveNext())
55  {
56  std::string municName = dataset->getString(2);
57  std::cout << std::endl << "City Name: " << municName;
58 
59  std::cout << std::endl << "ROW: " << row++ << "\t";
60 
61  te::gm::Geometry* g = static_cast<te::gm::Geometry*>(dataset->getGeometry(pos)->clone());
62  //te::gm::Geometry* g = (dataset->getGeometry(pos)).get();
63  srid = g->getSRID();
64 
65  geomMunic = static_cast<te::gm::Geometry*>(dataset->getGeometry(pos)->clone());
66 
67  std::cout << dataset->getPropertyName(pos) << ": " << geomMunic->asText() << std::endl;
68 
69  }
70  std::cout << std::endl;
71  }
72 
73  geomMunic->computeMBR(true);
74 
75 // now we will retrieve all cities that intercepts a given geometry (OuroPreto Geometry recovered above)
76  {
77  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("public.br_munic_2001", "geom", geomMunic, te::gm::INTERSECTS);
78 
79  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
80 
81  int row = 0;
82 
83  std::cout << std::endl << "=== DataSet: public.br_munic_2001: objects that intercepts the last object found in the previous query" << std::endl;
84 
85  while(dataset->moveNext())
86  {
87  std::string municName = dataset->getString(2);
88  std::cout << std::endl << "City Name: " << municName;
89 
90  std::cout << std::endl << "ROW: " << row++ << "\t";
91 
92  te::gm::Geometry* g = static_cast<te::gm::Geometry*>(dataset->getGeometry(pos)->clone());
93 
94  std::cout << dataset->getPropertyName(pos) << ": " << g->asText() << std::endl;
95 
96  delete g;
97  }
98  std::cout << std::endl;
99  }
100 
101  delete geomMunic;
102 
103 // now we will retrieve all cities that intercepts a given Polygon formed by OuroPreto_box with one inserted point
104  {
106  s->setPoint(0, -43.932979522347395, -20.632799968306863);
107  s->setPoint(1, -43.932979522347395, -20.161208868097958);
108  s->setPoint(2, -43.600000000000000, -19.500000000000000); //inserted point
109  s->setPoint(3, -43.403643659752738, -20.161208868097958);
110  s->setPoint(4, -43.403643659752738, -20.632799968306863);
111  s->setPoint(5, -43.932979522347395, -20.632799968306863); // closing
112 
114  pol->push_back(s);
115  pol->setSRID(srid); //srid = 4291
116 
117  std::cout << std::endl << "Created a polygon with: " << pol->getNPoints() << "points. " << std::endl;
118 
119  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("public.br_munic_2001", "geom", pol, te::gm::INTERSECTS);
120 
121  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
122 
123  int row = 0;
124 
125  std::cout << std::endl << "=== DataSet: public.br_munic_2001: objects that intercept the polygon" << std::endl;
126 
127  while(dataset->moveNext())
128  {
129  std::string municName = dataset->getString(2);
130  std::cout << std::endl << "City Name: " << municName;
131 
132  std::cout << std::endl << "ROW: " << row++ << "\t";
133 
134  te::gm::Geometry* g = static_cast<te::gm::Geometry*>(dataset->getGeometry(pos)->clone());
135 
136  std::cout << dataset->getPropertyName(pos) << ": " << g->asText() << std::endl;
137 
138  delete g;
139  }
140  std::cout << std::endl;
141  }
142 
143 // now we will retrieve all cities that intersects a given box
144  {
145  te::gm::Envelope box(-43.9329795837402, -20.6328010559082, -43.4036407470703, -20.1612071990967);
146 
147  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("public.br_munic_2001", "geom", &box, te::gm::INTERSECTS);
148 
149  std::size_t pos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
150 
151  int row = 0;
152 
153  std::cout << std::endl << "=== DataSet: public.br_munic_2001: objects that intercept a box" << std::endl;
154 
155  while(dataset->moveNext())
156  {
157  std::string municName = dataset->getString(2);
158  std::cout << std::endl << "City Name: " << municName;
159 
160  std::cout << std::endl << "ROW: " << row++ << "\t";
161 
162  te::gm::Geometry* g = static_cast<te::gm::Geometry*>(dataset->getGeometry(pos)->clone());
163 
164  std::cout << dataset->getPropertyName(pos) << ": " << g->asText() << std::endl;
165 
166  delete g;
167  }
168  std::cout << std::endl;
169  }
170 }
void push_back(Curve *ring)
It adds the curve to the curve polygon.
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
static te::dt::Date ds(2010, 01, 01)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
virtual void computeMBR(bool cascade) const _NOEXCEPT_OP(true)=0
It computes the minimum bounding rectangle for the geometry.
std::size_t getNPoints() const
it returns the number of points (vertexes) in the geometry.
A point with x and y coordinate values.
Definition: Point.h:50
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
std::string asText() const _NOEXCEPT_OP(true)
It returns an string with the Well-Known Text Representation for the geometry.
Examples on how to access/manipulate DataSources in TerraLib.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
virtual std::unique_ptr< DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. This method always returns a disconnected dataset...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
void RetrieveUsingSpatialFilter(te::da::DataSource *ds)
An example showing how to retrieve data using a spatial filter.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)