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  /*!
225  \brief It get the value of required operation type from numeric statistical summary
226 
227  \param ss The numeric statistical summary.
228  \param type Required operation type.
229 
230  \return The double value.
231  */
233 
234  /*!
235  \brief It get the value of required operation type from string statistical summary
236 
237  \param ss The string statistical summary.
238  \param type Required operation type.
239 
240  \return The string value.
241  */
243 
244  /*!
245  \brief It get a string with the mode operation format.
246 
247  \param ss The string statistical summary.
248 
249  \return The string value.
250  */
251  std::string getModeValue(te::stat::NumericStatisticalSummary ss);
252 
253  /*!
254  \brief It get the class with highest occurrence from "From" data set in "To" data set.
255 
256  \param fromDs The "From" data set.
257  \param dsPos Vector of "To" data set positions that intersect "From".
258  \param propertyName Required property name.
259  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
260 
261  \return The class value.
262  */
263  te::dt::AbstractData* getClassWithHighestOccurrence(te::da::DataSet* fromDs,
264  std::vector<std::size_t> dsPos,
265  const std::string& propertyName,
266  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
267 
268  /*!
269  \brief It get the class with highest intersection area from "From" data set in "To" data set.
270 
271  \param toDs The "To" data set.
272  \param toSrid The "To" data set srid.
273  \param fromDs The "From" data set.
274  \param fromSrid The "From" data set srid.
275  \param dsPos Vector of "To" data set positions that intersect "From".
276  \param propertyName Required property name.
277  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
278 
279  \return The class value.
280  */
281  te::dt::AbstractData* getClassWithHighestIntersectionArea(te::da::DataSet* toDs,
282  std::size_t toSrid,
283  te::da::DataSet* fromDs,
284  std::size_t fromSrid,
285  std::vector<std::size_t> dsPos,
286  const std::string& propertyName,
287  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
288 
289  /*!
290  \brief It get a abstract data with the value based on the type.
291 
292  \param strValue The string value.
293  \param type The value type.
294 
295  \return The value.
296  */
297  te::dt::AbstractData* getDataBasedOnType(const std::string& strValue, const int type);
298 
299  /*!
300  \brief It get the percent per class from "From" data set in "To" data set.
301 
302  \param fromDs The "From" data set.
303  \param dsPos Vector of "To" data set positions that intersect "From".
304  \param propertyName Required property name.
305  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
306 
307  \return Map with class value and percentage.
308  */
309  std::map<std::string, double> getPercentagePerClass(te::da::DataSet* fromDs,
310  std::vector<std::size_t> dsPos,
311  const std::string& propertyName,
312  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
313 
314  /*!
315  \brief It get the percentage of intersection area in the total area.
316 
317  \param toDs The "To" data set.
318  \param toSrid The "To" data set srid.
319  \param fromDs The "From" data set.
320  \param fromSrid The "From" data set srid.
321  \param dsPos Vector of "To" data set positions that intersect "From".
322  \param propertyName Required property name.
323  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
324 
325  \return The percentage.
326  */
327  double getPercentageOfTotalArea(te::da::DataSet* toDs,
328  std::size_t toSrid,
329  te::da::DataSet* fromDs,
330  std::size_t fromSrid,
331  std::vector<std::size_t> dsPos,
332  const std::string& propertyName,
333  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
334 
335  /*!
336  \brief It get the percentage of each class intersection area in the total area.
337 
338  \param toDs The "To" data set.
339  \param toSrid The "To" data set srid.
340  \param fromDs The "From" data set.
341  \param fromSrid The "From" data set srid.
342  \param dsPos Vector of "To" data set positions that intersect "From".
343  \param propertyName Required property name.
344  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
345 
346  \return Map of class and percentage.
347  */
348  std::map<std::string, double> getPercentageOfEachClassByArea(te::da::DataSet* toDs,
349  std::size_t toSrid,
350  te::da::DataSet* fromDs,
351  std::size_t fromSrid,
352  std::vector<std::size_t> dsPos,
353  const std::string& propertyName,
354  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
355 
356  /*!
357  \brief It get the weighted average if intersections values.
358 
359  \param toDs The "To" data set.
360  \param toSrid The "To" data set srid.
361  \param fromDs The "From" data set.
362  \param fromSrid The "From" data set srid.
363  \param dsPos Vector of "To" data set positions that intersect "From".
364  \param propertyName Required property name.
365  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
366 
367  \return The weighted average.
368  */
369  double getWeightedByArea(te::da::DataSet* toDs,
370  std::size_t toSrid,
371  te::da::DataSet* fromDs,
372  std::size_t fromSrid,
373  std::vector<std::size_t> dsPos,
374  const std::string& propertyName,
375  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
376 
377  /*!
378  \brief It get the sum of weighted average if intersections values.
379 
380  \param toDs The "To" data set.
381  \param toSrid The "To" data set srid.
382  \param fromDs The "From" data set.
383  \param fromSrid The "From" data set srid.
384  \param dsPos Vector of "To" data set positions that intersect "From".
385  \param propertyName Required property name.
386  \param dataValues Vector that contains all values of the item that intersects the "To" data set. (line and columns).
387 
388  \return The sum of weighted average.
389  */
390  double getWeightedSumByArea(te::da::DataSet* toDs,
391  std::size_t toSrid,
392  te::da::DataSet* fromDs,
393  std::size_t fromSrid,
394  std::vector<std::size_t> dsPos,
395  const std::string& propertyName,
396  std::vector< std::vector<te::dt::AbstractData*> >& dataValues);
397 
398  /*!
399  \brief It get the minimum distance of objects that not intersect.
400 
401  \param toDs The "To" data set.
402  \param toSrid The "To" data set srid.
403  \param fromDs The "From" data set.
404  \param fromSrid The "From" data set srid.
405  \param kdtree A generated KDTree based on the "From" data set.
406 
407  \return The minimum distance.
408  */
409  double getMinimumDistance(te::da::DataSet* toDs,
410  std::size_t toSrid,
411  te::da::DataSet* fromDs,
412  std::size_t fromSrid,
413  KD_ADAPTATIVE_TREE* kdtree);
414 
415  /*!
416  \brief It verify if the geometry is a polygon.
417 
418  \param type The geometry type.
419 
420  \return True if were polygon.
421  */
422  bool isPolygon(te::gm::GeomType type);
423 
424  /*!
425  \brief It verify if the geometry is a line.
426 
427  \param type The geometry type.
428 
429  \return True if were line.
430  */
431  bool isLine(te::gm::GeomType type);
432 
433  /*!
434  \brief It verify if the geometry is a point.
435 
436  \param type The geometry type.
437 
438  \return True if were potin.
439  */
440  bool isPoint(te::gm::GeomType type);
441 
442  /*!
443  \brief It verify if the geometry is a multi polygon.
444 
445  \param type The geometry type.
446 
447  \return True if were multi polygon.
448  */
449  bool isMultiPolygon(te::gm::GeomType type);
450 
451  /*!
452  \brief It verify if the geometry is a multi line.
453 
454  \param type The geometry type.
455 
456  \return True if were multi line.
457  */
458  bool isMultiLine(te::gm::GeomType type);
459 
460  /*!
461  \brief It verify if the geometry is a multi point.
462 
463  \param type The geometry type.
464 
465  \return True if were multi point.
466  */
467  bool isMultiPoint(te::gm::GeomType type);
468 
469  /*!
470  \brief It get the area of a geometry.
471 
472  \param geom The geometry.
473 
474  \return The area.
475  */
476  double getArea(te::gm::Geometry* geom);
477 
478  /*!
479  \brief It get all points of a geometry.
480 
481  \param geom The geometry.
482 
483  \return Vector of points.
484  */
485  std::vector<te::gm::Point*> getAllPointsOfGeometry(te::gm::Geometry* geom);
486 
487  /*!
488  \brief Verify if has operations that don't need the intersections.
489 
490  \return True if has.
491  */
492  bool hasNoIntersectionOperations();
493 
494  void normalizeClassName(std::string& name);
495 
496  bool checkGeometries(te::gm::Geometry* fromGeom, std::size_t fromPos, te::gm::Geometry* toGeom);
497 
498  bool isToLayerOGR();
499 
500  private:
501 
502  std::map<std::size_t, te::gm::Geometry*> m_mapGeom; // All geometries of "From" Layer.
503  };
504  }
505 }
506 #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:1435
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:352
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:73
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