IntersectionOp.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 IntersectionOp.cpp
22  */
23 #include "../common/StringUtils.h"
24 #include "../dataaccess/dataset/DataSetType.h"
25 #include "../dataaccess/dataset/DataSetTypeConverter.h"
26 #include "../dataaccess/datasource/DataSource.h"
27 #include "../dataaccess/datasource/DataSourceCapabilities.h"
28 #include "../dataaccess/utils/Utils.h"
29 
30 #include "../datatype/Property.h"
31 #include "../datatype/StringProperty.h"
32 
33 #include "../geometry/GeometryProperty.h"
34 
35 #include "../statistics/core/Utils.h"
36 
37 #include "IntersectionOp.h"
38 #include "Utils.h"
39 
41  : m_firstOidSet(nullptr),
42  m_secondOidSet(nullptr),
43  m_isFistQuery(false),
44  m_isSecondQuery(false)
45 {
46 }
47 
49  std::string inFirstDsetName,
50  std::unique_ptr<te::da::DataSetTypeConverter> firstConverter,
51  te::da::DataSourcePtr inSecondDsrc,
52  std::string inSecondDsetName,
53  std::unique_ptr<te::da::DataSetTypeConverter> secondConverter,
54  const te::da::ObjectIdSet* firstOidSet,
55  const te::da::ObjectIdSet* secondOidSet)
56 {
57  m_inFirstDsrc = inFirstDsrc;
58  m_inFirstDsetName = inFirstDsetName;
59  m_firstConverter = std::move(firstConverter);
60  m_inSecondDsrc = inSecondDsrc;
61  m_inSecondDsetName = inSecondDsetName;
62  m_secondConverter = std::move(secondConverter);
63 
64  m_firstOidSet = firstOidSet;
65  m_secondOidSet = secondOidSet;
66 }
67 
69  std::string inFirstDsetName,
70  std::unique_ptr<te::da::DataSetType> firstDsType,
71  std::unique_ptr<te::da::DataSet> firstDs,
72  std::unique_ptr<te::da::DataSetTypeConverter> firstConverter,
73  te::da::DataSourcePtr inSecondDsrc,
74  std::string inSecondDsetName,
75  std::unique_ptr<te::da::DataSetType> secondDsType,
76  std::unique_ptr<te::da::DataSet> secondDs,
77  std::unique_ptr<te::da::DataSetTypeConverter> secondConverter,
78  const te::da::ObjectIdSet* firstOidSet,
79  const te::da::ObjectIdSet* secondOidSet)
80 {
81  m_inFirstDsrc = inFirstDsrc;
82  m_inFirstDsetName = inFirstDsetName;
83  m_firstConverter = std::move(firstConverter);
84  m_inSecondDsrc = inSecondDsrc;
85  m_inSecondDsetName = inSecondDsetName;
86  m_secondConverter = std::move(secondConverter);
87 
88  m_firstDsType = std::move(firstDsType);
89  m_firstDs = std::move(firstDs);
90  m_secondDsType = std::move(secondDsType);
91  m_secondDs = std::move(secondDs);
92 
93  m_firstOidSet = firstOidSet;
94  m_secondOidSet = secondOidSet;
95 }
96 
97 void te::vp::IntersectionOp::setParams(const std::vector<std::pair<int, std::string> >& attributeVec)
98 {
99  m_attributeVec = attributeVec;
100 }
101 
103 {
104  m_outDsrc = outDsrc;
105  m_outDsetName = dsname;
106 }
107 
109 {
110  if ((IsPointType(firstGeom)) || (IsPointType(secondGeom)))
111  {
112  return te::gm::MultiPointType;
113  }
114  else if ((IsLineStringType(firstGeom)) || (IsLineStringType(secondGeom)))
115  {
116  // Temporary solution for MNT.
117  // Result can generate lines and points.
118  // Possible solution , create a layer for each geometric type.
119  if(((IsLineStringType(firstGeom)) && (IsLineStringType(secondGeom))))
120  {
121  return te::gm::MultiPointType;
122  }
124  }
125  else
126  {
128  }
129 }
130 
132 {
133  if (!m_firstConverter->getResult())
134  return false;
135 
136  if (!m_firstConverter->getResult()->hasGeom())
137  return false;
138 
139  if (!m_secondConverter->getResult())
140  return false;
141 
142  if (!m_secondConverter->getResult()->hasGeom())
143  return false;
144 
145  if (m_outDsetName.empty() || !m_outDsrc.get())
146  return false;
147 
148  return true;
149 }
150 
151 std::vector<te::dt::Property*> te::vp::IntersectionOp::getTabularProps(te::da::DataSetType* dsType)
152 {
153  std::vector<te::dt::Property*> props;
154  te::dt::Property* prop;
155 
156  for(std::size_t i = 0; i < dsType->getProperties().size(); ++i)
157  {
158  prop = dsType->getProperty(i);
159 
160  if(prop->getType() != te::dt::GEOMETRY_TYPE)
161  props.push_back(prop);
162  }
163 
164  return props;
165 }
166 
168 {
170 
172  pkProperty->setAutoNumber(true);
173  dsType->add(pkProperty);
174 
175  te::da::PrimaryKey* pk = new te::da::PrimaryKey(m_outDsetName + "_pk", dsType);
176  pk->add(pkProperty);
177  dsType->setPrimaryKey(pk);
178 
179  for (std::size_t i = 0; i < m_attributeVec.size(); ++i)
180  {
181  te::dt::Property* p = nullptr;
182  if (m_attributeVec[i].first == 1)
183  {
184  p = m_firstDsType->getProperty(m_attributeVec[i].second);
185  p->setName("A_" + p->getName());
186  }
187  else
188  {
189  p = m_secondDsType->getProperty(m_attributeVec[i].second);
190  p->setName("B_" + p->getName());
191  }
192 
193  dsType->add(p->clone());
194  }
195 
197 
198  te::gm::GeometryProperty* newGeomProp = new te::gm::GeometryProperty("geom");
199  newGeomProp->setGeometryType(newType);
200 
202  newGeomProp->setSRID(firstGeomProp->getSRID());
203 
204  dsType->add(newGeomProp);
205 
206  return dsType;
207 }
208 
210 {
211  m_isFistQuery = true;
212 }
213 
215 {
216  m_isSecondQuery = true;
217 }
218 
220 {
221  for (std::size_t i = 0; i < m_attributeVec.size(); ++i)
222  {
223  std::pair<int, std::string> p = m_attributeVec[i];
224 
225  if (p.first == id && p.second == prop.getName())
226  return true;
227  }
228 
229  return false;
230 }
std::unique_ptr< te::da::DataSet > m_firstDs
std::unique_ptr< te::da::DataSet > m_secondDs
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
std::vector< te::dt::Property * > getTabularProps(te::da::DataSetType *dsType)
Property * getProperty(std::size_t i) const
It returns the i-th property.
std::string m_inSecondDsetName
Geometric property.
te::da::DataSetType * getOutputDsType()
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 setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
An atomic property like an integer or double.
std::vector< std::pair< int, std::string > > m_attributeVec
boost::shared_ptr< DataSource > DataSourcePtr
TEVPEXPORT bool IsPointType(const te::gm::GeomType &geomType)
void setInput(te::da::DataSourcePtr inFirstDsrc, std::string inFirstDsetName, std::unique_ptr< te::da::DataSetTypeConverter > firstConverter, te::da::DataSourcePtr inSecondDsrc, std::string inSecondDsetName, std::unique_ptr< te::da::DataSetTypeConverter > secondConverter, const te::da::ObjectIdSet *firstOidSet=0, const te::da::ObjectIdSet *secondOidSet=0)
const te::da::ObjectIdSet * m_firstOidSet
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::unique_ptr< te::da::DataSetType > m_secondDsType
virtual bool paramsAreValid()
virtual Property * clone() const =0
It returns a clone of the object.
It models a property definition.
Definition: Property.h:59
te::da::DataSourcePtr m_inSecondDsrc
bool isSelectedProperty(const int &id, const dt::Property &prop)
void setName(const std::string &name)
It sets the property name.
Definition: Property.h:137
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
TEVPEXPORT bool IsLineStringType(const te::gm::GeomType &geomType)
void setParams(const std::vector< std::pair< int, std::string > > &attributeVec)
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
std::unique_ptr< te::da::DataSetType > m_firstDsType
te::gm::Polygon * p
std::unique_ptr< te::da::DataSetTypeConverter > m_firstConverter
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsname)
Utility functions for the data access module.
te::da::DataSourcePtr m_outDsrc
Intersection operation.
std::string m_inFirstDsetName
te::da::DataSourcePtr m_inFirstDsrc
std::unique_ptr< te::da::DataSetTypeConverter > m_secondConverter
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
te::gm::GeomType setGeomResultType(te::gm::GeomType firstGeom, te::gm::GeomType secondGeom)
void setPrimaryKey(PrimaryKey *pk)
It sets the primary key constraint.
const te::da::ObjectIdSet * m_secondOidSet
const std::string & getName() const
It returns the property name.
Definition: Property.h:127