src/terralib/vp/Intersection.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Intersection.cpp
22  */
23 
24 #include "../core/translator/Translator.h"
25 #include "../core/logger/Logger.h"
26 
27 #include "../dataaccess/dataset/DataSet.h"
28 #include "../dataaccess/dataset/DataSetAdapter.h"
29 #include "../dataaccess/dataset/DataSetType.h"
30 #include "../dataaccess/dataset/DataSetTypeConverter.h"
31 #include "../dataaccess/dataset/ObjectIdSet.h"
32 
33 #include "../dataaccess/datasource/DataSource.h"
34 #include "../dataaccess/datasource/DataSourceInfo.h"
35 #include "../dataaccess/datasource/DataSourceInfoManager.h"
36 #include "../dataaccess/datasource/DataSourceCapabilities.h"
37 #include "../dataaccess/datasource/DataSourceTransactor.h"
38 
39 #include "../dataaccess/query/And.h"
40 #include "../dataaccess/query/DataSetName.h"
41 #include "../dataaccess/query/Expression.h"
42 #include "../dataaccess/query/Field.h"
43 #include "../dataaccess/query/Fields.h"
44 #include "../dataaccess/query/From.h"
45 #include "../dataaccess/query/FromItem.h"
46 #include "../dataaccess/query/Insert.h"
47 #include "../dataaccess/query/Join.h"
48 #include "../dataaccess/query/JoinConditionOn.h"
49 #include "../dataaccess/query/PropertyName.h"
50 #include "../dataaccess/query/Select.h"
51 #include "../dataaccess/query/SubSelect.h"
52 #include "../dataaccess/query/ST_Intersection.h"
53 #include "../dataaccess/query/ST_Intersects.h"
54 #include "../dataaccess/query/ST_Multi.h"
55 #include "../dataaccess/query/Where.h"
56 
57 #include "../dataaccess/utils/Utils.h"
58 
59 #include "../datatype/Property.h"
60 #include "../datatype/SimpleData.h"
61 #include "../datatype/StringProperty.h"
62 
63 #include "../geometry/Geometry.h"
64 #include "../geometry/GeometryCollection.h"
65 #include "../geometry/GeometryProperty.h"
66 #include "../geometry/MultiLineString.h"
67 #include "../geometry/MultiPoint.h"
68 #include "../geometry/MultiPolygon.h"
69 #include "../geometry/Utils.h"
70 
71 #include "../sam.h"
72 
73 #include "../memory/DataSet.h"
74 #include "../memory/DataSetItem.h"
75 
76 #include "../statistics/core/Utils.h"
77 
78 #include "AlgorithmParams.h"
79 #include "ComplexData.h"
80 #include "Intersection.h"
81 #include "Utils.h"
82 
83 // BOOST
84 #include <boost/lexical_cast.hpp>
85 
87 
89 {
91 
92  te::vp::InputParams firstInputParams = mainParams->getInputParams()[0];
93  te::vp::InputParams secondInputParams = mainParams->getInputParams()[1];
94 
95  te::da::DataSourcePtr outputDataSource = mainParams->getOutputDataSource();
96 
97  const std::map<std::string, te::dt::AbstractData*> specificParams = mainParams->getSpecificParams();
98 
99  bool isCollection = this->isCollection(specificParams);
100 
101  te::da::DataSetType* firstDataSetType = firstInputParams.m_inputDataSetType;
102  te::da::DataSet* firstDataSet = firstInputParams.m_inputDataSet;
103  te::gm::GeometryProperty* firstGeometryProperty = te::da::GetFirstGeomProperty(firstDataSetType);
104 
105  te::da::DataSetType* secondDataSetType = secondInputParams.m_inputDataSetType;
106  te::da::DataSet* secondDataSet = secondInputParams.m_inputDataSet;
107  te::gm::GeometryProperty* secondGeometryProperty = te::da::GetFirstGeomProperty(secondDataSetType);
108 
109  std::unique_ptr<te::da::DataSetType> outputDataSetType(getOutputDataSetType(mainParams));
110  std::unique_ptr<te::mem::DataSet> outputDataSet(new te::mem::DataSet(outputDataSetType.get()));
111  te::gm::GeometryProperty* outputGeometryProperty = te::da::GetFirstGeomProperty(outputDataSetType.get());
112 
113  te::sam::rtree::Index<size_t, 8>* rtree = te::vp::GetRtree(secondDataSet);
114 
115  firstDataSet->moveBeforeFirst();
116  while (firstDataSet->moveNext())
117  {
118  std::unique_ptr<te::mem::DataSetItem> item(new te::mem::DataSetItem(outputDataSet.get()));
119 
120  if (firstDataSet->isNull(firstGeometryProperty->getName()))
121  continue;
122 
123  std::unique_ptr<te::gm::Geometry> currentGeometry = firstDataSet->getGeometry(firstGeometryProperty->getName());
124 
125  std::vector<std::size_t> rtreeReport;
126  rtree->search(*currentGeometry->getMBR(), rtreeReport);
127 
128  for (std::size_t i = 0; i < rtreeReport.size(); ++i)
129  {
130  secondDataSet->move(rtreeReport[i]);
131 
132  std::unique_ptr<te::gm::Geometry> candidateGeometry = secondDataSet->getGeometry(secondGeometryProperty->getName());
133 
134  if (!currentGeometry->intersects(candidateGeometry.get()))
135  continue;
136 
137  te::gm::Geometry* resultingGeometry = currentGeometry->intersection(candidateGeometry.get());
138 
139  if (!resultingGeometry || !resultingGeometry->isValid())
140  {
141 #ifdef TERRALIB_LOGGER_ENABLED
142  TE_CORE_LOG_DEBUG("vp", "Intersection - Invalid geometry found");
143 #endif //TERRALIB_LOGGER_ENABLED
144  continue;
145  }
146 
147  for (std::map<std::string, std::string>::iterator it = m_firstAttrNameMap.begin(); it != m_firstAttrNameMap.end(); ++it)
148  {
149  if(!firstDataSet->isNull(it->second))
150  item->setValue(it->first, firstDataSet->getValue(it->second).release());
151  }
152 
153  for (std::map<std::string, std::string>::iterator it = m_secondAttrNameMap.begin(); it != m_secondAttrNameMap.end(); ++it)
154  {
155  if (!secondDataSet->isNull(it->second))
156  item->setValue(it->first, secondDataSet->getValue(it->second).release());
157  }
158 
159  if (!resultingGeometry->isEmpty())
160  {
161  std::vector<te::gm::Geometry*> geomVec;
162 
163  if (isCollection)
164  geomVec.push_back(resultingGeometry);
165  else
166  te::gm::Multi2Single(resultingGeometry, geomVec);
167 
168  for (std::size_t g = 0; g < geomVec.size(); ++g)
169  {
170  if (!te::gm::IsMultiType(geomVec[g]->getGeomTypeId()) && isCollection)
171  geomVec[g] = te::vp::SetGeomAsMulti(*geomVec[g]);
172 
173  std::unique_ptr<te::mem::DataSetItem> currentItem = item->clone();
174  currentItem->setGeometry(outputGeometryProperty->getName(), geomVec[g]);
175 
176  outputDataSet->add(currentItem.release());
177  }
178  }
179 
180  outputDataSet->moveNext();
181 
182  std::size_t outputDsGeomePropPosition = te::da::GetFirstSpatialPropertyPos(outputDataSet.get());
183 
184  if (!item->isNull(outputDsGeomePropPosition))
185  outputDataSet->add(item.release());
186  }
187  }
188 
189  outputDataSet->moveBeforeFirst();
190 
191  std::unique_ptr<te::da::DataSet> dataSetPrepared = PrepareAdd(outputDataSet.release(), outputDataSetType.get());
192 
193  if (!dataSetPrepared.get())
194  throw te::common::Exception(TE_TR("Output DataSet was not prepared to save."));
195 
196  if (dataSetPrepared->size() == 0)
197  throw te::common::Exception("The resultant layer is empty!");
198 
199  te::vp::Save(outputDataSource.get(), dataSetPrepared.get(), outputDataSetType.get());
200 
201  return true;
202 }
203 
204 std::vector<te::dt::Property*> te::vp::Intersection::getFirstSelectedProperties(const te::da::DataSetType* dataSetType, const std::map<std::string, te::dt::AbstractData*>& specificParams)
205 {
206  std::vector<te::dt::Property*> result;
207 
208  std::vector<std::pair<std::string, std::string> > selectedProperties = getSelectedProperties(specificParams);
209 
210  for (std::size_t i = 0; i < selectedProperties.size(); ++i)
211  {
212  if (selectedProperties[i].first == "FIRST")
213  {
214  result.push_back(dataSetType->getProperty(selectedProperties[i].second));
215  }
216  }
217 
218  return result;
219 }
220 
221 std::vector<te::dt::Property*> te::vp::Intersection::getSecondSelectedProperties(const te::da::DataSetType* dataSetType, const std::map<std::string, te::dt::AbstractData*>& specificParams)
222 {
223  std::vector<te::dt::Property*> result;
224 
225  std::vector<std::pair<std::string, std::string> > selectedProperties = getSelectedProperties(specificParams);
226 
227  for (std::size_t i = 0; i < selectedProperties.size(); ++i)
228  {
229  if (selectedProperties[i].first == "SECOND")
230  {
231  result.push_back(dataSetType->getProperty(selectedProperties[i].second));
232  }
233  }
234 
235  return result;
236 }
237 
238 std::vector<std::pair<std::string, std::string> > te::vp::Intersection::getSelectedProperties(const std::map<std::string, te::dt::AbstractData*>& specificParams)
239 {
240  std::vector<std::pair<std::string, std::string> > result;
241 
242  for (std::map<std::string, te::dt::AbstractData*>::const_iterator it = specificParams.begin(); it != specificParams.end(); ++it)
243  {
244  if (it->first == "ATTRIBUTES")
245  {
248 
249  if (cd)
250  result = cd->getValue();
251  }
252  }
253 
254  return result;
255 }
256 
257 bool te::vp::Intersection::isCollection(const std::map<std::string, te::dt::AbstractData*>& specificParams)
258 {
259  bool isCollection = false;
260 
261  for (std::map<std::string, te::dt::AbstractData*>::const_iterator it = specificParams.begin(); it != specificParams.end(); ++it)
262  {
263  if (it->first != "IS_COLLECTION")
264  {
265  continue;
266  }
267 
269  dynamic_cast<te::dt::SimpleData<bool, te::dt::BOOLEAN_TYPE >* >(it->second);
270 
271  if (sd)
272  isCollection = sd->getValue();
273  }
274 
275  return isCollection;
276 }
277 
279 {
280 
281  bool isCollection;
282 
283  //*TESTE SPECIFC PARAM*//
284  //std::map<std::string, te::dt::AbstractData*> specificParams = mainParams->getSpecificParams();
285  //std::map<std::string, te::dt::AbstractData*>::iterator it = specificParams.begin();
286  //while (it != specificParams.end())
287  //{
288  // std::string key = it->first;
289  // int type = it->second->getTypeCode();
290 
291  // te::dt::SimpleData<double, te::dt::DOUBLE_TYPE>* sd = dynamic_cast< te::dt::SimpleData<double, te::dt::DOUBLE_TYPE>* >(it->second);
292  // double value = sd->getValue();
293  // ++it;
294  //}
295  //*TESTE SPECIFC PARAM*//
296 
297  std::vector<te::vp::InputParams> inputParams = mainParams->getInputParams();
298 
299  if (inputParams.size() < 2)
300  {
301  //Return a message: It is necessary more than one item for performing the operation
302  return false;
303  }
304 
305 // Verify if input parameters has two entry, if so, verify if both are the same and set as true the self intersection check.
306 
307  if (inputParams.size() == 2)
308  {
309  std::string table1 = inputParams[0].m_inputDataSetName;
310  std::string table2 = inputParams[1].m_inputDataSetName;
311  }
312 
313 
314 // Build Query to Intersect data.
315  te::da::Fields* fields = new te::da::Fields;
316 
317  std::unique_ptr<te::da::DataSetType> dsType_first;
318  std::unique_ptr<te::da::DataSetType> dsType_second;
319 
320  te::gm::GeometryProperty* geom_first;
321  te::gm::GeometryProperty* geom_second;
322 
323  dsType_first = inputParams[0].m_inputDataSource->getDataSetType(inputParams[0].m_inputDataSetName);
324  geom_first = te::da::GetFirstGeomProperty(dsType_first.get());
325 
326  dsType_second = inputParams[1].m_inputDataSource->getDataSetType(inputParams[1].m_inputDataSetName);
327  geom_second = te::da::GetFirstGeomProperty(dsType_second.get());
328 
329  te::da::Expression* e_intersection = new te::da::ST_Intersection(
330  new te::da::PropertyName(dsType_first->getName() + "." + geom_first->getName()),
331  new te::da::PropertyName(dsType_second->getName() + "." + geom_second->getName()));
332 
333  te::da::Expression* e_multi = new te::da::ST_Multi(*e_intersection);
334 
335  te::da::Field* f_intersection = new te::da::Field(*e_multi, "geom");
336 
337  fields->push_back(f_intersection);
338 
339  te::da::FromItem* firstFromItem = new te::da::DataSetName(dsType_first->getName());
340  te::da::FromItem* secondFromItem = new te::da::DataSetName(dsType_second->getName());
341 
342  te::da::Expression* e_intersects = new te::da::ST_Intersects(
343  new te::da::PropertyName(dsType_first->getName() + "." + geom_first->getName()),
344  new te::da::PropertyName(dsType_second->getName() + "." + geom_second->getName()));
345 
346  te::da::JoinConditionOn* on = new te::da::JoinConditionOn(e_intersects);
347  te::da::Join* join = new te::da::Join(*firstFromItem, *secondFromItem, te::da::INNER_JOIN, *on);
348 
349  te::da::From* from = new te::da::From;
350  from->push_back(join);
351 
352  te::da::Select* mainSelect = new te::da::Select(fields, from);
353 
354 /*If the input parameters size is greater than two, add the previous
355  select query into a subselect and builds a new external select to query.*/
356  for (std::size_t i = 2; i < inputParams.size(); ++i)
357  {
358  te::da::SubSelect* subSelect = new te::da::SubSelect(mainSelect, "Intersection" + boost::lexical_cast<std::string>(i));
359 
360  te::da::Fields* external_fields = new te::da::Fields;
361  std::unique_ptr<te::da::DataSetType> external_dsType = inputParams[i].m_inputDataSource->getDataSetType(inputParams[i].m_inputDataSetName);
362  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(external_dsType.get());
363 
364  te::da::Expression* e_externalIntersection = new te::da::ST_Intersection(
365  new te::da::PropertyName(external_dsType->getName() + "." + geomProp->getName()),
366  new te::da::PropertyName(subSelect->getAlias() + ".geom"));
367 
368  te::da::Expression* e_externalMulti = new te::da::ST_Multi(*e_externalIntersection);
369 
370  te::da::Field* f_externalIntersection = new te::da::Field(*e_externalMulti, "geom");
371  external_fields->push_back(f_externalIntersection);
372 
373  te::da::FromItem* externalFromItem = new te::da::DataSetName(external_dsType->getName());
374  te::da::FromItem* internalFromItem = new te::da::SubSelect(*subSelect);
375 
376  te::da::Expression* e_externalIntersects = new te::da::ST_Intersects(
377  new te::da::PropertyName(external_dsType->getName() + "." + geomProp->getName()),
378  new te::da::PropertyName(subSelect->getAlias() + ".geom"));
379 
380  te::da::JoinConditionOn* externalOn = new te::da::JoinConditionOn(e_externalIntersects);
381  te::da::Join* externalJoin = new te::da::Join(*externalFromItem, *internalFromItem, te::da::INNER_JOIN, *externalOn);
382 
383  te::da::From* externalFrom = new te::da::From;
384  externalFrom->push_back(externalJoin);
385 
386  mainSelect = new te::da::Select(external_fields, externalFrom);
387  }
388 
389 /*Check if the input and output dataSource are the same, if so,
390  persists the result of select query into database with insert command.*/
391  te::da::DataSourcePtr outputDataSource = mainParams->getOutputDataSource();
392 
393  te::da::DataSourceInfoPtr inDataSourceInfoPtr = te::da::DataSourceInfoManager::getInstance().get(inputParams[0].m_inputDataSource->getId());
394  te::da::DataSourceInfoPtr outDataSourceInfoPtr = te::da::DataSourceInfoManager::getInstance().get(outputDataSource->getId());
395 
396 // Create output dataset
397  std::unique_ptr<te::da::DataSourceTransactor> t = outputDataSource->getTransactor();
398  std::map<std::string, std::string> options;
399  t->begin();
400  t->createDataSet(getOutputDataSetType(mainParams), options);
401  t->commit();
402 
403  if (!inDataSourceInfoPtr)
404  {
405  t->rollBack();
406  return false;
407  }
408 
409  if (!outDataSourceInfoPtr)
410  {
411  t->rollBack();
412  return false;
413  }
414 
415  std::string inputConnection = inDataSourceInfoPtr->getConnInfoAsString();
416  std::string outputConnection = outDataSourceInfoPtr->getConnInfoAsString();
417 
418 // Execute Query
419  if (inputConnection == outputConnection)
420  {
421  te::da::Fields* insertFields = new te::da::Fields;
422  te::da::Field* f_insert = new te::da::Field("geom");
423  insertFields->push_back(f_insert);
424 
425  te::da::Insert* insert = new te::da::Insert(new te::da::DataSetName(mainParams->getOutputDataSetName()), insertFields, mainSelect);
426  inputParams[0].m_inputDataSource->execute(*insert);
427  }
428  else
429  {
430  std::unique_ptr<te::da::DataSet> dsQuery = inputParams[0].m_inputDataSource->query(mainSelect);
431  dsQuery->moveBeforeFirst();
432 
433  if (dsQuery->size() == 0)
434  throw te::common::Exception("The Layers do not intersect!");
435 
436  std::string outputDsName = mainParams->getOutputDataSetName();
437 
438  std::unique_ptr<te::da::DataSetType> outDataSetType(new te::da::DataSetType(outputDsName));
439 
440  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(outputDsName + "_id", te::dt::INT32_TYPE);
441  pkProperty->setAutoNumber(true);
442  outDataSetType->add(pkProperty);
443 
444  te::da::PrimaryKey* pk = new te::da::PrimaryKey(outputDsName + "_pk", outDataSetType.get());
445  pk->add(pkProperty);
446  outDataSetType->setPrimaryKey(pk);
447 
448  te::gm::GeomType newType = getGeomResultType(te::da::GetFirstGeomProperty(dsType_first.get())->getGeometryType(),
449  te::da::GetFirstGeomProperty(dsType_second.get())->getGeometryType(), isCollection);
450 
451  te::gm::GeometryProperty* newGeomProp = new te::gm::GeometryProperty("geom");
452  newGeomProp->setGeometryType(newType);
453 
454  outDataSetType->add(newGeomProp);
455 
456  std::unique_ptr<te::da::DataSet> outDset(updateGeomType(outDataSetType.get(), dsQuery.release()));
457 
458  outDset->moveBeforeFirst();
459 
460  t->add(outputDsName, outDset.get(), options);
461  t->commit();
462  }
463 
464 
465  return true;
466 }
467 
469 {
470  te::mem::DataSet* dsMem = new te::mem::DataSet(dsType);
471 
472  std::size_t type = 0;
473  std::vector<te::dt::Property*> props = dsType->getProperties();
474 
475  int pk = 0;
476  while (ds->moveNext())
477  {
478  std::string propName;
479  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(dsMem);
480 
481  dsItem->setInt32(0, pk);
482 
483  for (std::size_t i = 1; i < props.size(); ++i)
484  {
485  type = static_cast<size_t>(props[i]->getType());
486  propName = props[i]->getName();
487 
488  if (type != te::dt::GEOMETRY_TYPE)
489  {
490  if (!ds->isNull(propName))
491  {
492  std::unique_ptr<te::dt::AbstractData> value = ds->getValue(propName);
493  dsItem->setValue(i, value.release());
494  }
495  }
496  else
497  {
498  std::unique_ptr<te::gm::Geometry> geom = ds->getGeometry(propName);
499  std::unique_ptr<te::gm::GeometryProperty> geomProp(static_cast<te::gm::GeometryProperty*>(props[i]->clone()));
500 
501  if (geomProp->getGeometryType() == te::gm::MultiPolygonType)
502  {
503  if (geom->getGeomTypeId() == te::gm::MultiPolygonType)
504  {
505  dsItem->setGeometry(i, geom.release());
506  }
507  else if (geom->getGeomTypeId() == te::gm::PolygonType)
508  {
509  std::unique_ptr<te::gm::GeometryCollection> newGeom(new te::gm::GeometryCollection(0, te::gm::MultiPolygonType, geom->getSRID()));
510  newGeom->add(geom.release());
511  dsItem->setGeometry(i, newGeom.release());
512  }
513  }
514  else if (geomProp->getGeometryType() == te::gm::MultiLineStringType)
515  {
516  if (geom->getGeomTypeId() == te::gm::MultiLineStringType)
517  {
518  dsItem->setGeometry(i, geom.release());
519  }
520  else if (geom->getGeomTypeId() == te::gm::LineStringType)
521  {
522  std::unique_ptr<te::gm::GeometryCollection> newGeom(new te::gm::GeometryCollection(0, te::gm::MultiLineStringType, geom->getSRID()));
523  newGeom->add(geom.release());
524  dsItem->setGeometry(i, newGeom.release());
525  }
526  }
527  else if (geomProp->getGeometryType() == te::gm::MultiPointType)
528  {
529  if (geom->getGeomTypeId() == te::gm::MultiPointType)
530  {
531  dsItem->setGeometry(i, geom.release());
532  }
533  else if (geom->getGeomTypeId() == te::gm::PointType)
534  {
535  std::unique_ptr<te::gm::GeometryCollection> newGeom(new te::gm::GeometryCollection(0, te::gm::MultiPointType, geom->getSRID()));
536  newGeom->add(geom.release());
537  dsItem->setGeometry(i, newGeom.release());
538  }
539  }
540  }
541  }
542  ++pk;
543  dsMem->add(dsItem);
544  }
545  return dsMem;
546 }
547 
549 {
550  te::vp::InputParams firstInputParams = mainParams->getInputParams()[0];
551  te::vp::InputParams secondInputParams = mainParams->getInputParams()[1];
552 
553  std::string outputDataSetName = mainParams->getOutputDataSetName();
554 
555  const std::map<std::string, te::dt::AbstractData*> specificParams = mainParams->getSpecificParams();
556 
557  bool isCollection = this->isCollection(specificParams);
558 
559  te::da::DataSetType* firstDataSetType = firstInputParams.m_inputDataSetType;
560  te::gm::GeometryProperty* firstGeometryProperty = te::da::GetFirstGeomProperty(firstDataSetType);
561 
562  te::da::DataSetType* secondDataSetType = secondInputParams.m_inputDataSetType;
563  te::gm::GeometryProperty* secondGeometryProperty = te::da::GetFirstGeomProperty(secondDataSetType);
564 
565  std::vector<te::dt::Property*> firstSelectedProperties = getFirstSelectedProperties(firstDataSetType, specificParams);
566  std::vector<te::dt::Property*> secondSelectedProperties = getSecondSelectedProperties(secondDataSetType, specificParams);
567 
568  te::da::DataSetType* outputDataSetType = new te::da::DataSetType(outputDataSetName);
569 
570  std::vector<std::string> attrNamesAux;
571 
572  if (mainParams->getOutputDataSource()->getType() != "OGR")
573  {
574  te::dt::SimpleProperty* pkProperty = new te::dt::SimpleProperty(outputDataSetName + "_id", te::dt::INT32_TYPE);
575  pkProperty->setAutoNumber(true);
576  outputDataSetType->add(pkProperty);
577 
578  te::da::PrimaryKey* pk = new te::da::PrimaryKey(outputDataSetName + "_pk", outputDataSetType);
579  pk->add(pkProperty);
580  outputDataSetType->setPrimaryKey(pk);
581  }
582 
583  for (std::size_t i = 0; i < firstSelectedProperties.size(); ++i)
584  {
585  te::dt::Property* clonedProperty = firstSelectedProperties[i]->clone();
586 
587  std::string pName = clonedProperty->getName();
588 
589  if (mainParams->getOutputDataSource()->getType() == "OGR")
590  pName = GetDistinctName(pName, attrNamesAux, 10);
591  else
592  pName = GetDistinctName(pName, attrNamesAux);
593 
594  m_firstAttrNameMap[pName] = clonedProperty->getName();
595 
596  clonedProperty->setName(pName);
597 
598  outputDataSetType->add(clonedProperty);
599 
600  attrNamesAux.push_back(clonedProperty->getName());
601  }
602 
603  for (std::size_t i = 0; i < secondSelectedProperties.size(); ++i)
604  {
605  te::dt::Property* clonedProperty = secondSelectedProperties[i]->clone();
606 
607  std::string pName = clonedProperty->getName();
608 
609  if (mainParams->getOutputDataSource()->getType() == "OGR")
610  pName = GetDistinctName(pName, attrNamesAux, 10);
611  else
612  pName = GetDistinctName(pName, attrNamesAux);
613 
614  m_secondAttrNameMap[pName] = clonedProperty->getName();
615 
616  clonedProperty->setName(pName);
617 
618  outputDataSetType->add(clonedProperty);
619 
620  attrNamesAux.push_back(clonedProperty->getName());
621  }
622 
623  te::gm::GeometryProperty* newGeometryProperty = new te::gm::GeometryProperty("geom");
624 
625  te::gm::GeomType type = getGeomResultType(firstGeometryProperty->getGeometryType(), secondGeometryProperty->getGeometryType(), isCollection);
626  newGeometryProperty->setGeometryType(type);
627 
628  newGeometryProperty->setSRID(firstGeometryProperty->getSRID());
629 
630  outputDataSetType->add(newGeometryProperty);
631 
632  return outputDataSetType;
633 }
634 
636 {
637  te::gm::GeomType geomType;
638 
639  if ((firstGeom == te::gm::PointType) ||
640  (firstGeom == te::gm::PointZType) ||
641  (firstGeom == te::gm::PointMType) ||
642  (firstGeom == te::gm::PointZMType) ||
643  (firstGeom == te::gm::PointKdType) ||
644 
645  (secondGeom == te::gm::PointType) ||
646  (secondGeom == te::gm::PointZType) ||
647  (secondGeom == te::gm::PointMType) ||
648  (secondGeom == te::gm::PointZMType) ||
649  (secondGeom == te::gm::PointKdType) ||
650 
651  (firstGeom == te::gm::MultiPointType) ||
652  (firstGeom == te::gm::MultiPointZType) ||
653  (firstGeom == te::gm::MultiPointMType) ||
654  (firstGeom == te::gm::MultiPointZMType) ||
655 
656  (secondGeom == te::gm::MultiPointType) ||
657  (secondGeom == te::gm::MultiPointZType) ||
658  (secondGeom == te::gm::MultiPointMType) ||
659  (secondGeom == te::gm::MultiPointZMType))
660  {
661  geomType = te::gm::MultiPointType;
662  }
663  else if ((firstGeom == te::gm::LineStringType) ||
664  (firstGeom == te::gm::LineStringZType) ||
665  (firstGeom == te::gm::LineStringMType) ||
666  (firstGeom == te::gm::LineStringZMType) ||
667 
668  (secondGeom == te::gm::LineStringType) ||
669  (secondGeom == te::gm::LineStringZType) ||
670  (secondGeom == te::gm::LineStringMType) ||
671  (secondGeom == te::gm::LineStringZMType) ||
672 
673  (firstGeom == te::gm::MultiLineStringType) ||
674  (firstGeom == te::gm::MultiLineStringZType) ||
675  (firstGeom == te::gm::MultiLineStringMType) ||
676  (firstGeom == te::gm::MultiLineStringZMType) ||
677 
678  (secondGeom == te::gm::MultiLineStringType) ||
679  (secondGeom == te::gm::MultiLineStringZType) ||
680  (secondGeom == te::gm::MultiLineStringMType) ||
681  (secondGeom == te::gm::MultiLineStringZMType))
682  {
683  geomType = te::gm::MultiLineStringType;
684  }
685  else
686  geomType = te::gm::MultiPolygonType;
687 
688  if (isCollection)
689  {
690  if (te::gm::IsMultiType(geomType))
691  return geomType;
692  else
693  return te::gm::GetMultiType(geomType);
694  }
695  else
696  {
697  if (te::gm::IsMultiType(geomType))
698  return te::gm::GetSimpleType(geomType);
699  else
700  return geomType;
701  }
702 }
bool executeQuery(te::vp::AlgorithmParams *mainParams)
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
Property * getProperty(std::size_t i) const
It returns the i-th property.
Geometric property.
TEVPEXPORT std::unique_ptr< te::da::DataSet > PrepareAdd(te::da::DataSet *ds, te::da::DataSetType *dt)
A structure to hold the input parameters of vector processing.
Definition: InputParams.h:50
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
void setGeometryType(GeomType t)
It sets the geometry subtype.
bool isCollection(const std::map< std::string, te::dt::AbstractData * > &specificParams)
An atomic property like an integer or double.
Spatial intersects operator.
Definition: ST_Intersects.h:46
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
boost::shared_ptr< DataSource > DataSourcePtr
A class that represents an R-tree.
A class that models the name of any property of an object.
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEGEOMEXPORT te::gm::GeomType GetMultiType(te::gm::GeomType geomType)
Get the collection type of GeomType.
#define TE_CORE_LOG_DEBUG(channel, message)
Use this tag in order to log a message to a specified logger with the DEBUG level.
Definition: Logger.h:225
Spatial multi operator.
Definition: ST_Multi.h:50
virtual Geometry * intersection(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns a geometric object that represents the point set intersection with another geometry...
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
TEGEOMEXPORT bool IsMultiType(te::gm::GeomType geomType)
Verifies if the geomType is a collection type.
std::vector< te::vp::InputParams > getInputParams()
T getValue() const
It returns the associated value.
Definition: SimpleData.h:139
std::map< std::string, std::string > m_firstAttrNameMap
Definition: Intersection.h:87
TEVPEXPORT te::sam::rtree::Index< size_t, 8 > * GetRtree(te::da::DataSet *data)
te::da::DataSetType * getOutputDataSetType(te::vp::AlgorithmParams *mainParams)
TEGEOMEXPORT te::gm::GeomType GetSimpleType(te::gm::GeomType geomType)
Get the simple type of GeomType.
static te::dt::Date ds(2010, 01, 01)
std::vector< te::dt::Property * > getFirstSelectedProperties(const te::da::DataSetType *dataSetType, const std::map< std::string, te::dt::AbstractData * > &specificParams)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
Algorithm Parameters.
virtual Property * clone() const =0
It returns a clone of the object.
The Insert object can add the return of a select object.
Definition: Insert.h:50
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
std::vector< std::pair< std::string, std::string > > getSelectedProperties(const std::map< std::string, te::dt::AbstractData * > &specificParams)
It models a property definition.
Definition: Property.h:59
T getValue() const
It returns the associated value.
Definition: ComplexData.h:129
This is an abstract class that models a query expression.
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
virtual bool move(std::size_t i)=0
It moves the dataset internal pointer to a given position.
te::da::DataSet * updateGeomType(te::da::DataSetType *dsType, te::da::DataSet *ds)
bool executeMemory(te::vp::AlgorithmParams *mainParams)
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
TEVPEXPORT void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType, const bool &enableProgress=true)
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
A template for complex data types.
Definition: ComplexData.h:52
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
const std::string & getOutputDataSetName()
virtual bool isValid() const _NOEXCEPT_OP(false)
It tells if the geometry is well formed.
const std::string & getAlias() const
It returns the alias associated to the source item.
Definition: FromItem.cpp:47
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
void DataSet()
virtual bool isEmpty() const _NOEXCEPT_OP(false)
It returns true if this geometric object is the empty Geometry.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
virtual AbstractData * clone() const =0
It returns a clone of this object.
te::da::DataSourcePtr getOutputDataSource()
te::gm::GeomType getGeomResultType(const te::gm::GeomType &firstGeom, const te::gm::GeomType &secondGeom, const bool &isCollection)
std::map< std::string, std::string > m_secondAttrNameMap
Definition: Intersection.h:89
A Join clause combines two FromItems.
Definition: Join.h:50
te::da::DataSetType * m_inputDataSetType
Is required.
Definition: InputParams.h:82
const std::map< std::string, te::dt::AbstractData * > & getSpecificParams()
Utility functions for the data access module.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
std::vector< te::dt::Property * > getSecondSelectedProperties(const te::da::DataSetType *dataSetType, const std::map< std::string, te::dt::AbstractData * > &specificParams)
void add(Constraint *c)
It adds a new constraint.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
te::da::DataSet * m_inputDataSet
Is required for operations in memory.
Definition: InputParams.h:84
virtual std::unique_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Spatial intersection operator.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TEVPEXPORT void ValidateAlgorithmParams(AlgorithmParams *mainParams, Strategy st)
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
JoinConditionOn is a boolean expression and it specifies which items in a join are considered to matc...
TEVPEXPORT std::string GetDistinctName(const std::string &name, std::vector< std::string > names, std::size_t maxSize=0)
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
A Select can be used as a source of information in another query.
Definition: SubSelect.h:49
It is a collection of other geometric objects.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
TEVPEXPORT te::gm::Geometry * SetGeomAsMulti(const te::gm::Geometry &geom)
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.
Definition: Property.h:127