Buffer.cpp
Go to the documentation of this file.
1 
2 #include "../Config.h"
3 #include <terralib/common.h>
4 #include <terralib/dataaccess.h>
8 #include <terralib/vp/BufferOp.h>
10 
11 // STL
12 #include <iostream>
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 //OGR to OGR
20 {
21  std::string data_dir = TERRALIB_DATA_DIR;
22 
23  std::string filename = data_dir + "/shape/Buffer/SP_meso.shp";
24 
25  std::string srcInfo("file://" + filename);
26 
28  srcDs->open();
29 
30  std::string inDsetName = "SP_meso";
31  if (!srcDs->dataSetExists(inDsetName))
32  {
33  std::cout << "Input dataset not found: " << inDsetName << std::endl;
34  return false;
35  }
36 
37  std::unique_ptr<te::da::DataSetType> inDsetType = srcDs->getDataSetType(inDsetName);
39 
40  std::unique_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(inDsetType.get(), srcDs->getCapabilities(), srcDs->getEncoding()));
41 
42  double distance = 0.1;
43 
44  //options for Polygon Rule.
45  int bufferPolygonRule = te::vp::INSIDE_OUTSIDE;
46  //int bufferPolygonRule = te::vp::ONLY_INSIDE;
47  //int bufferPolygonRule = te::vp::ONLY_OUTSIDE;
48 
49  //options for Boundaries Rule.
50  //int bufferBoundariesRule = te::vp::DISSOLVE;
51  int bufferBoundariesRule = te::vp::NOT_DISSOLVE;
52 
53  bool copyInputColumns = false;
54  int levels = 1;
55 
56  std::string file_result = data_dir + "/shape/Buffer/file_result.shp";
57  std::string tgrInfo("file://" + file_result);
58 
60  trgDs->open();
61 
62  std::string outDS = "file_result";
63 
64  if (trgDs->dataSetExists(outDS))
65  {
66  std::cout << "A dataset with the same requested output dataset name already exists: " << outDS << std::endl;
67  return false;
68  }
69 
70  // sera feito por algum tipo de factory
71  te::vp::BufferOp* bufferOp = new te::vp::BufferMemory();
72 
73  bufferOp->setInput(srcDs, inDsetName, std::move(converter));
74  bufferOp->setOutput(trgDs, outDS);
75  bufferOp->setParams(distance,
76  gp->getSRID(),
77  bufferPolygonRule,
78  bufferBoundariesRule,
79  copyInputColumns,
80  levels);
81 
82  bool result;
83 
84  if (!bufferOp->paramsAreValid())
85  result = false;
86  else
87  result = bufferOp->run();
88 
89  delete bufferOp;
90 
91  return result;
92 }
93 
94 //OGR to Postgis
96 {
97  std::string data_dir = TERRALIB_DATA_DIR;
98 
99  std::string filename(data_dir + "/shape/Buffer/SP_meso.shp");
100  std::string srcInfo("file://" + filename);
101 
103  srcDs->open();
104 
105  std::string inDsetName = "SP_meso";
106  if (!srcDs->dataSetExists(inDsetName))
107  {
108  std::cout << "Input dataset not found: " << inDsetName << std::endl;
109  return false;
110  }
111 
112  std::unique_ptr<te::da::DataSet> inDset = srcDs->getDataSet(inDsetName);
113  std::unique_ptr<te::da::DataSetType> inDsetType = srcDs->getDataSetType(inDsetName);
115 
116  std::unique_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(inDsetType.get(), srcDs->getCapabilities(), srcDs->getEncoding()));
117 
118  double distance = 0.1;
119 
120  //options for Polygon Rule.
121  int bufferPolygonRule = te::vp::INSIDE_OUTSIDE;
122  //int bufferPolygonRule = te::vp::ONLY_INSIDE;
123  //int bufferPolygonRule = te::vp::ONLY_OUTSIDE;
124 
125  //options for Boundaries Rule.
126  int bufferBoundariesRule = te::vp::DISSOLVE;
127  //int bufferBoundariesRule = te::vp::NOT_DISSOLVE;
128 
129  bool copyInputColumns = false;
130  int levels = 1;
131 
132  std::string connInfo("ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS?PG_CONNECT_TIMEOUT=4&PG_CLIENT_ENCODING=UTF-8");
133 
134  te::da::DataSourcePtr trgDs(te::da::DataSourceFactory::make("POSTGIS", connInfo));
135  trgDs->open();
136 
137  std::string outDS = "result";
138 
139  if (trgDs->dataSetExists(outDS))
140  {
141  std::cout << "A dataset with the same requested output dataset name already exists: " << outDS << std::endl;
142  return false;
143  }
144 
145  // sera feito por algum tipo de factory
146  te::vp::BufferOp* bufferOp = new te::vp::BufferMemory();
147 
148  bufferOp->setInput(srcDs, inDsetName, std::move(converter));
149  bufferOp->setOutput(trgDs, outDS);
150  bufferOp->setParams(distance,
151  gp->getSRID(),
152  bufferPolygonRule,
153  bufferBoundariesRule,
154  copyInputColumns,
155  levels);
156 
157  bool result = false;
158 
159  if (!bufferOp->paramsAreValid())
160  result = false;
161  else
162  result = bufferOp->run();
163 
164  delete bufferOp;
165 
166  return result;
167 }
168 
169 //Postgis to Postgis
171 {
172  std::string connInfo("ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS?PG_CONNECT_TIMEOUT=4&PG_CLIENT_ENCODING=UTF-8");
173 
174  te::da::DataSourcePtr srcDs(te::da::DataSourceFactory::make("POSTGIS", connInfo));
175 
176  std::string inDsetName = "sp_meso";
177  if (!srcDs->dataSetExists(inDsetName))
178  {
179  std::cout << "Input dataset not found: " << inDsetName << std::endl;
180  return false;
181  }
182 
183  std::unique_ptr<te::da::DataSet> inDset = srcDs->getDataSet(inDsetName);
184  std::unique_ptr<te::da::DataSetType> inDsetType = srcDs->getDataSetType(inDsetName);
186 
187  std::unique_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(inDsetType.get(), srcDs->getCapabilities(), srcDs->getEncoding()));
188 
189  std::string outDSet = "bufferPGISToPGIS";
190 
191  double distance = 0.1;
192 
193  //options for Polygon Rule.
194  int bufferPolygonRule = te::vp::INSIDE_OUTSIDE;
195 
196  //options for Boundaries Rule.
197  int bufferBoundariesRule = te::vp::DISSOLVE;
198 
199  bool copyInputColumns = false;
200  int levels = 2;
201 
202  te::da::DataSourcePtr outDsource(te::da::DataSourceFactory::make("POSTGIS", connInfo));
203  outDsource->open();
204 
205  if (outDsource->dataSetExists(outDSet))
206  {
207  std::cout << "A dataset with the same requested output dataset name already exists: " << outDSet << std::endl;
208  return false;
209  }
210 
211  // sera feito por algum tipo de factory
212  te::vp::BufferOp* bufferOp = new te::vp::BufferMemory();
213 
214  bufferOp->setInput(srcDs, inDsetName, std::move(converter));
215  bufferOp->setOutput(outDsource, outDSet);
216  bufferOp->setParams(distance,
217  gp->getSRID(),
218  bufferPolygonRule,
219  bufferBoundariesRule,
220  copyInputColumns,
221  levels);
222 
223  bool result = false;
224 
225  if (!bufferOp->paramsAreValid())
226  result = false;
227  else
228  result = bufferOp->run();
229 
230  delete bufferOp;
231 
232  return result;
233 }
234 
235 //Postgis to OGR
237 {
238  std::string connInfo("ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS?PG_CONNECT_TIMEOUT=4&PG_CLIENT_ENCODING=UTF-8");
239 
240  te::da::DataSourcePtr srcDs(te::da::DataSourceFactory::make("POSTGIS", connInfo));
241  srcDs->open();
242 
243  std::string inDsetName = "distritos";
244  if (!srcDs->dataSetExists(inDsetName))
245  {
246  std::cout << "Input dataset not found: " << inDsetName << std::endl;
247  return false;
248  }
249 
250  std::unique_ptr<te::da::DataSet> inDset = srcDs->getDataSet(inDsetName);
251  std::unique_ptr<te::da::DataSetType> inDsetType = srcDs->getDataSetType(inDsetName);
253 
254  std::unique_ptr<te::da::DataSetTypeConverter> converter(new te::da::DataSetTypeConverter(inDsetType.get(), srcDs->getCapabilities(), srcDs->getEncoding()));
255 
256  double distance = 0.1;
257 
258  //options for Polygon Rule.
259  int bufferPolygonRule = te::vp::INSIDE_OUTSIDE;
260 
261  //options for Boundaries Rule.
262  int bufferBoundariesRule = te::vp::DISSOLVE;
263 
264  bool copyInputColumns = false;
265  int levels = 1;
266 
267  std::string data_dir = TERRALIB_DATA_DIR;
268 
269  std::string uriResult(data_dir + "/shape/Buffer/bufferPGISToOGR_distritos.shp");
270 
271  std::string tgrInfo("file://" + uriResult);
272 
274  trgDs->open();
275 
276  std::string outDSet = "bufferPGISToOGR_distritos";
277 
278  if (trgDs->dataSetExists(outDSet))
279  {
280  std::cout << "A dataset with the same requested output dataset name already exists and will be removed: " << outDSet << std::endl;
281  trgDs->dropDataSet(outDSet);
282  //return true;
283  }
284 
285  // sera feito por algum tipo de factory
286  te::vp::BufferOp* bufferOp = new te::vp::BufferQuery();
287 
288  bufferOp->setInput(srcDs, inDsetName, std::move(converter));
289  bufferOp->setOutput(trgDs, outDSet);
290  bufferOp->setParams(distance,
291  gp->getSRID(),
292  bufferPolygonRule,
293  bufferBoundariesRule,
294  copyInputColumns,
295  levels);
296 
297  bool result = false;
298 
299  if (!bufferOp->paramsAreValid())
300  result = false;
301  else
302  result = bufferOp->run();
303 
304  delete bufferOp;
305 
306  return result;
307 }
Buffer Vector Processing functions.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
virtual bool run()=0
It executes the operation.
boost::shared_ptr< DataSource > DataSourcePtr
Buffer operation.
The boundaries between buffers will not be dissolved.
An abstract class to compute the buffer memory and buffer query operation.
Definition: BufferOp.h:61
void setParams(const double &distance, const int &newSrid, const int &bufferPolygonRule, const int &bufferBoundariesRule, const bool &copyInputColumns, const int &levels, const int &attributePosition=-1)
It sets the input parameters that will be consumed to execute the operation.
Definition: BufferOp.cpp:62
bool BufferOGRToPGIS()
Definition: Buffer.cpp:95
bool BufferPGISToPGIS()
Definition: Buffer.cpp:170
Buffer Vector Processing functions.
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, std::unique_ptr< te::da::DataSetTypeConverter > converter, const te::da::ObjectIdSet *oidSet=0)
It sets the input data will be calculated.
Definition: BufferOp.cpp:51
An converter for DataSetType.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
A factory for data sources.
Geometric property.
virtual bool paramsAreValid()
It verifies if the parameters are valid.
Definition: BufferOp.cpp:85
A concrete class to compute the buffer operation.
Definition: BufferQuery.h:61
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
It sets the output parameters that will be used to persist operation result.
Definition: BufferOp.cpp:79
The buffer is generated Inside and outside of the polygons.
bool BufferOGRToOGR()
Definition: Buffer.cpp:19
This file contains include headers for the TerraLib Common Runtime module.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
This file contains include headers for the Data Access module of TerraLib.
bool BufferPGISToOGR()
Definition: Buffer.cpp:236
A concrete class to compute the buffer operation.
Definition: BufferMemory.h:62
The boundaries between buffers will be dissolved.