VectorToVectorMemory.h
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 terralib/attributefill/VectorToVectorMemory.h
22 
23  \brief A class that execute vector to vector operations in the memory.
24 */
25 
26 #ifndef __TERRALIB_ATTRIBUTEFILL_INTERNAL_VECTORTOVECTORMEMORY_H
27 #define __TERRALIB_ATTRIBUTEFILL_INTERNAL_VECTORTOVECTORMEMORY_H
28 
29 //Terralib
30 #include "../dataaccess/dataset/DataSet.h"
31 #include "../dataaccess/dataset/DataSetType.h"
32 #include "../dataaccess/datasource/DataSource.h"
33 #include "../datatype/Property.h"
34 #include "../maptools/AbstractLayer.h"
35 #include "../memory/DataSet.h"
36 #include "../sam/kdtree.h"
37 #include "Config.h"
38 #include "VectorToVectorOp.h"
39 
40 // STL
41 #include <map>
42 #include <memory>
43 #include <string>
44 #include <vector>
45 
48 
49 namespace te
50 {
51  namespace dt
52  {
53  class AbstractData;
54  }
55 
56  namespace gm
57  {
58  class Geometry;
59  }
60 
61  namespace stat
62  {
65  }
66 
67  namespace attributefill
68  {
69  /*!
70  \class VectorToVector
71 
72  \brief A class that execute vector to vector operations in the memory.
73 
74  \sa DataSourceManager, DataSourceFactory, DataSet, DataSetType
75  */
77  {
78  public:
79 
80  /*!
81  \brief Constructor.
82  */
84 
86 
87  /*!
88  \brief It set the input informations
89 
90  \param fromLayer Te "From" Layer.
91  \param toLayer The "To" Layer.
92  */
93  void setInput(te::map::AbstractLayerPtr fromLayer,
95 
96  /*!
97  \brief It set the params used to execute.
98 
99  \param options Property and string vector map with informations about which property was selected
100  and which operations were selected to this property.
101  */
102  void setParams(const std::map<te::dt::Property*, std::vector<std::string> >& options);
103 
104  /*!
105  \brief It set output.
106 
107  \param outDsrc The Data Source that will be the result.
108  \param dsName The name of output data set.
109  */
110  void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName);
111 
112  /*!
113  \brief It execute the operations.
114 
115  \return True if execute.
116  */
117  bool run();
118 
119  protected:
120 
121  /*!
122  \brief It return a RTree with data set iterator position information.
123 
124  \param data The Data Set.
125 
126  \return The Rtree.
127  */
129 
130  /*!
131  \brief It return a KDTree.
132 
133  \param data The Data Set.
134  \param toSrid Data Set srid.
135 
136  \return The KDtree.
137  */
138  KD_ADAPTATIVE_TREE* getKDtree(te::da::DataSet* data, std::size_t toSrid);
139 
140  /*!
141  \brief It create a data set type based on selecteds properties and operations.
142 
143  \return A Data Set Type.
144  */
145  te::da::DataSetType* getOutputDataSetType();
146 
147  /*!
148  \brief It return distincts values of a property from the "From" data set.
149 
150  \param fromDs The "From" data set.
151  \param propertyName The property name with the information.
152 
153  \return A vector of string values.
154  */
155  std::vector<std::string> getDistinctClasses(te::da::DataSet* fromDs, const std::string& propertyName);
156 
157  /*!
158  \brief It return a name based on original property name and the selected operation.
159 
160  \param prop The original property.
161  \param func The selected operation.
162 
163  \return A name based on the property and operation.
164  */
165  std::string getPropertyName(te::dt::Property* prop, te::attributefill::OperationType func);
166 
167  /*!
168  \brief It verify if the operation is a statistical operation.
169 
170  \param type The operation type.
171 
172  \return True if the operation were statistical.
173  */
174  bool isStatistical(te::attributefill::OperationType type);
175 
176  /*!
177  \brief It verify all intersection between the "From" and "To" data sets.
178 
179  \param toDs The "To" data set.
180  \param fromDs The "From" data set.
181  \param rtree A generated RTree based on the "From" data set.
182  \param hasInvalid Set true if there are any invalid geometry as intersection candidate.
183 
184  \return A vector of positions that "To" data set has intersection with "From".
185  */
186  std::vector<std::size_t> getIntersections(te::da::DataSet* toDs,
187  te::da::DataSet* fromDs,
189  bool& hasInvalid);
190 
191  /*!
192  \brief It get the value of all positions in "To" data set that has intersection with
193  a position of "From" data set.
194 
195  \param fromDs The "From" data set.
196  \param dsPos Vector of intersections positions.
197  \param propertyName The property name.
198 
199  \note The fromDs iterator must be in the correct position.
200 
201  \return A vector of values.
202  */
203  std::vector<std::vector<te::dt::AbstractData*> > getDataValues(te::da::DataSet* fromDs,
204  std::vector<std::size_t> dsPos);
205 
206  /*!
207  \brief It get the numeric values of a vector of abstract data.
208 
209  \param data Vector of abstract data.
210 
211  \return A vector of double values.
212  */
213  std::vector<double> getNumValues(std::vector< std::vector<te::dt::AbstractData*> > dataValues, std::size_t pos);
214 
215  /*!
216  \brief It get the string values of a vector of abstract data.
217 
218  \param data Vector of abstract data.
219 
220  \return A vector of string values.
221  */
222  std::vector<std::string> getStrValues(std::vector< std::vector<te::dt::AbstractData*> > dataValues, std::size_t pos);
223 
224  int getTotalNumberOfDistinctValues(te::da::DataSet* fromDs,
225  const std::string& propertyName,
226  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
227 
228  /*!
229  \brief It get the value of required operation type from numeric statistical summary
230 
231  \param ss The numeric statistical summary.
232  \param type Required operation type.
233 
234  \return The double value.
235  */
237 
238  /*!
239  \brief It get the value of required operation type from string statistical summary
240 
241  \param ss The string statistical summary.
242  \param type Required operation type.
243 
244  \return The string value.
245  */
247 
248  /*!
249  \brief It get a string with the mode operation format.
250 
251  \param ss The string statistical summary.
252 
253  \return The string value.
254  */
255  std::string getModeValue(te::stat::NumericStatisticalSummary ss);
256 
257  /*!
258  \brief It get the class with highest occurrence from "From" data set in "To" data set.
259 
260  \param fromDs The "From" data set.
261  \param dsPos Vector of "To" data set positions that intersect "From".
262  \param propertyName Required property name.
263  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
264 
265  \return The class value.
266  */
267  te::dt::AbstractData* getClassWithHighestOccurrence(te::da::DataSet* fromDs,
268  std::vector<std::size_t> dsPos,
269  const std::string& propertyName,
270  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
271 
272  /*!
273  \brief It get the class with highest intersection area from "From" data set in "To" data set.
274 
275  \param toDs The "To" data set.
276  \param toSrid The "To" data set srid.
277  \param fromDs The "From" data set.
278  \param fromSrid The "From" data set srid.
279  \param dsPos Vector of "To" data set positions that intersect "From".
280  \param propertyName Required property name.
281  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
282 
283  \return The class value.
284  */
285  te::dt::AbstractData* getClassWithHighestIntersectionArea(te::da::DataSet* toDs,
286  std::size_t toSrid,
287  te::da::DataSet* fromDs,
288  std::size_t fromSrid,
289  std::vector<std::size_t> dsPos,
290  const std::string& propertyName,
291  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
292 
293  /*!
294  \brief It get a abstract data with the value based on the type.
295 
296  \param strValue The string value.
297  \param type The value type.
298 
299  \return The value.
300  */
301  te::dt::AbstractData* getDataBasedOnType(const std::string& strValue, const int type);
302 
303  /*!
304  \brief It get the percent per class from "From" data set in "To" data set.
305 
306  \param fromDs The "From" data set.
307  \param dsPos Vector of "To" data set positions that intersect "From".
308  \param propertyName Required property name.
309  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
310 
311  \return Map with class value and percentage.
312  */
313  std::map<std::string, double> getPercentagePerClass(te::da::DataSet* fromDs,
314  std::vector<std::size_t> dsPos,
315  const std::string& propertyName,
316  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
317 
318  /*!
319  \brief It get the percentage of intersection area in the total area.
320 
321  \param toDs The "To" data set.
322  \param toSrid The "To" data set srid.
323  \param fromDs The "From" data set.
324  \param fromSrid The "From" data set srid.
325  \param dsPos Vector of "To" data set positions that intersect "From".
326  \param propertyName Required property name.
327  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
328 
329  \return The percentage.
330  */
331  double getPercentageOfTotalArea(te::da::DataSet* toDs,
332  std::size_t toSrid,
333  te::da::DataSet* fromDs,
334  std::size_t fromSrid,
335  std::vector<std::size_t> dsPos,
336  const std::string& propertyName,
337  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
338 
339  /*!
340  \brief It get the percentage of each class intersection area in the total area.
341 
342  \param toDs The "To" data set.
343  \param toSrid The "To" data set srid.
344  \param fromDs The "From" data set.
345  \param fromSrid The "From" data set srid.
346  \param dsPos Vector of "To" data set positions that intersect "From".
347  \param propertyName Required property name.
348  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
349 
350  \return Map of class and percentage.
351  */
352  std::map<std::string, double> getPercentageOfEachClassByArea(te::da::DataSet* toDs,
353  std::size_t toSrid,
354  te::da::DataSet* fromDs,
355  std::size_t fromSrid,
356  std::vector<std::size_t> dsPos,
357  const std::string& propertyName,
358  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
359 
360  /*!
361  \brief It get the weighted average if intersections values.
362 
363  \param toDs The "To" data set.
364  \param toSrid The "To" data set srid.
365  \param fromDs The "From" data set.
366  \param fromSrid The "From" data set srid.
367  \param dsPos Vector of "To" data set positions that intersect "From".
368  \param propertyName Required property name.
369  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
370 
371  \return The weighted average.
372  */
373  double getWeightedByArea(te::da::DataSet* toDs,
374  std::size_t toSrid,
375  te::da::DataSet* fromDs,
376  std::size_t fromSrid,
377  std::vector<std::size_t> dsPos,
378  const std::string& propertyName,
379  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
380 
381  /*!
382  \brief It get the sum of weighted average if intersections values.
383 
384  \param toDs The "To" data set.
385  \param toSrid The "To" data set srid.
386  \param fromDs The "From" data set.
387  \param fromSrid The "From" data set srid.
388  \param dsPos Vector of "To" data set positions that intersect "From".
389  \param propertyName Required property name.
390  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
391 
392  \return The sum of weighted average.
393  */
394  double getWeightedSumByArea(te::da::DataSet* toDs,
395  std::size_t toSrid,
396  te::da::DataSet* fromDs,
397  std::size_t fromSrid,
398  std::vector<std::size_t> dsPos,
399  const std::string& propertyName,
400  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
401 
402  /*!
403  \brief It get the minimum distance of objects that not intersect.
404 
405  \param toDs The "To" data set.
406  \param toSrid The "To" data set srid.
407  \param fromDs The "From" data set.
408  \param fromSrid The "From" data set srid.
409  \param kdtree A generated KDTree based on the "From" data set.
410 
411  \return The minimum distance.
412  */
413  double getMinimumDistance(te::da::DataSet* toDs,
414  std::size_t toSrid,
415  te::da::DataSet* fromDs,
416  std::size_t fromSrid,
417  KD_ADAPTATIVE_TREE* kdtree);
418 
419  /*!
420  \brief It get the minimum distance of objects centroid that not intersect.
421 
422  \param toDs The "To" data set.
423  \param toSrid The "To" data set srid.
424  \param fromDs The "From" data set.
425  \param fromSrid The "From" data set srid.
426  \param kdtree A generated KDTree based on the "From" data set.
427 
428  \return The minimum distance.
429  */
430  double getMinimumDistanceFromCentroid(te::da::DataSet* toDs,
431  std::size_t toSrid,
432  te::da::DataSet* fromDs,
433  std::size_t fromSrid,
434  KD_ADAPTATIVE_TREE* kdtree,
436 
437  /*!
438  \brief It verify if the geometry is a polygon.
439 
440  \param type The geometry type.
441 
442  \return True if were polygon.
443  */
444  bool isPolygon(te::gm::GeomType type);
445 
446  /*!
447  \brief It verify if the geometry is a line.
448 
449  \param type The geometry type.
450 
451  \return True if were line.
452  */
453  bool isLine(te::gm::GeomType type);
454 
455  /*!
456  \brief It verify if the geometry is a point.
457 
458  \param type The geometry type.
459 
460  \return True if were potin.
461  */
462  bool isPoint(te::gm::GeomType type);
463 
464  /*!
465  \brief It verify if the geometry is a multi polygon.
466 
467  \param type The geometry type.
468 
469  \return True if were multi polygon.
470  */
471  bool isMultiPolygon(te::gm::GeomType type);
472 
473  /*!
474  \brief It verify if the geometry is a multi line.
475 
476  \param type The geometry type.
477 
478  \return True if were multi line.
479  */
480  bool isMultiLine(te::gm::GeomType type);
481 
482  /*!
483  \brief It verify if the geometry is a multi point.
484 
485  \param type The geometry type.
486 
487  \return True if were multi point.
488  */
489  bool isMultiPoint(te::gm::GeomType type);
490 
491  /*!
492  \brief It get the area of a geometry.
493 
494  \param geom The geometry.
495 
496  \return The area.
497  */
498  double getArea(te::gm::Geometry* geom);
499 
500  /*!
501  \brief It get all points of a geometry.
502 
503  \param geom The geometry.
504 
505  \return Vector of points.
506  */
507  std::vector<te::gm::Point*> getAllPointsOfGeometry(te::gm::Geometry* geom);
508 
509  /*!
510  \brief Verify if has operations that don't need the intersections.
511 
512  \return True if has.
513  */
514  bool hasNoIntersectionOperations();
515 
516  void normalizeClassName(std::string& name);
517 
518  bool checkGeometries(te::gm::Geometry* fromGeom, std::size_t fromPos, te::gm::Geometry* toGeom);
519 
520  bool isToLayerOGR();
521 
522  private:
523 
524  std::map<std::size_t, te::gm::Geometry*> m_mapGeom; // All geometries of "From" Layer.
525  };
526  }
527 }
528 #endif // __TERRALIB_ATTRIBUTEFILL_INTERNAL_VECTORTOVECTORMEMORY_H
A structure to hold the set of statistics from a set of numerical values.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
Configuration flags for the Attribute Fill module of TerraLib.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1440
A class that represents an R-tree.
Definition: Index.h:56
A class that models the description of a dataset.
Definition: DataSetType.h:72
A class that represents a two dimensional K-d Tree (2-d Tree) that store data-elements into the leafs...
Definition: Index.h:353
te::sam::kdtree::AdaptativeNode< te::gm::Coord2D, std::vector< te::gm::Point >, te::gm::Point > KD_ADAPTATIVE_NODE
It models a property definition.
Definition: Property.h:59
#define TEATTRIBUTEFILLEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:61
A point with x and y coordinate values.
Definition: Point.h:50
URI C++ Library.
te::sam::kdtree::AdaptativeIndex< KD_ADAPTATIVE_NODE > KD_ADAPTATIVE_TREE
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:75
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
A structure to hold the set of statistics from a set of categorical (sample) values.
Vector To Vector operation.
std::map< std::size_t, te::gm::Geometry * > m_mapGeom
A class that represents an Kd-tree node.
Definition: Node.h:206
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
OperationType
Define grouping operations type.
Definition: Enums.h:39