All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CellSpaceOperations.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 terralib/cellspace/CellularSpacesOperations.cpp
22 
23  \brief Implementation of the Cellular Spaces operations.
24 */
25 
26 // Terralib
27 #include "../common/progress/TaskProgress.h"
28 #include "../dataaccess.h"
29 #include "../datatype/SimpleProperty.h"
30 #include "../datatype/StringProperty.h"
31 #include "../geometry/Envelope.h"
32 #include "../geometry/GeometryProperty.h"
33 #include "../geometry/Point.h"
34 #include "../geometry/Polygon.h"
35 #include "../maptools/DataSetLayer.h"
36 #include "../memory/DataSet.h"
37 #include "../memory/DataSetItem.h"
38 #include "../raster.h"
39 #include "../sam.h"
40 #include "CellSpaceOperations.h"
41 
42 #include <stdio.h>
43 
45 {
46 }
47 
49 {
50 }
51 
53  const std::string& name,
54  te::map::AbstractLayerPtr layerBase,
55  double resX,
56  double resY,
57  bool useMask,
58  CellSpaceType type)
59 {
60  te::gm::Envelope env = layerBase->getExtent();
61 
62  int srid = layerBase->getSRID();
63 
64  int maxcols, maxrows;
65 
66  maxcols = (int)ceil((env.m_urx-env.m_llx)/resX);
67  maxrows = (int)ceil((env.m_ury-env.m_lly)/resY);
68 
69  std::auto_ptr<te::da::DataSet> refDs;
70 
71  if(useMask)
72  {
73  refDs = layerBase->getData();
74  }
75 
76  std::auto_ptr<te::da::DataSetType> outputDataSetType(createCellularDataSetType(name, srid, type));
77 
78  std::auto_ptr<te::sam::rtree::Index<size_t, 8> > rtree;
79 
80  if(useMask)
81  {
82  rtree.reset(getRtree(layerBase));
83  }
84 
85  te::common::TaskProgress task("Processing Cellular Spaces...");
86  task.setTotalSteps(maxrows);
87  task.useTimer(true);
88 
89  std::auto_ptr<te::da::DataSet> outputDataSet(new te::mem::DataSet(outputDataSetType.get()));
90 
91  std::auto_ptr<te::mem::DataSet> ds(dynamic_cast<te::mem::DataSet*>(outputDataSet.release()));
92 
93  double x, y;
94  for(int lin = 0; lin < maxrows; ++lin)
95  {
96 
97  y = env.m_lly+(lin*resY);
98  for(int col = 0; col < maxcols; ++col)
99  {
100  x = env.m_llx+(col*resX);
101 
102  te::gm::Envelope* env = new te::gm::Envelope(x, y, x+resX, y+resY);
103 
104  std::auto_ptr<te::gm::Geometry> geom;
105 
106  if(type == CELLSPACE_POLYGONS)
107  {
108  geom.reset(te::gm::GetGeomFromEnvelope(env, srid));
109  }
110  else if(type == CELLSPACE_POINTS)
111  {
112  double pX = env->m_llx +( (env->m_urx - env->m_llx) / 2);
113  double pY = env->m_lly +( (env->m_ury - env->m_lly) / 2);
114  geom.reset(new te::gm::Point(pX, pY, srid));
115  }
116 
117  if(useMask)
118  {
119  std::vector<size_t> report;
120  rtree->search(*geom->getMBR(), report);
121 
122  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(refDs.get());
123 
124  if(!report.empty())
125  {
126  for(std::size_t i = 0; i < report.size(); ++i)
127  {
128  refDs->move(report[i]);
129 
130  std::auto_ptr<te::gm::Geometry> g = refDs->getGeometry(geomPos);
131  g->setSRID(srid);
132 
133  if(geom->intersects(g.get()))
134  {
135  addCell(ds.get(), col, lin, geom.release());
136  break;
137  }
138  }
139  }
140  }
141  else
142  {
143  addCell(ds.get(), col, lin, geom.release());
144  }
145  }
146 
147  task.pulse();
148  }
149 
150  // Output
151  std::auto_ptr<te::da::DataSource> source = te::da::DataSourceFactory::make(outputSource->getAccessDriver());
152  source->setConnectionInfo(outputSource->getConnInfo());
153  source->open();
154 
155  std::map<std::string, std::string> options;
156  // create the dataset
157  source->createDataSet(outputDataSetType.get(), options);
158 
159  // copy from memory to output datasource
160  ds->moveBeforeFirst();
161  source->add(outputDataSetType->getName(),ds.get(), options);
162 }
163 
165  const std::string& name,
166  const double resX,
167  const double resY,
168  const te::gm::Envelope& env,
169  const int srid,
170  const CellSpaceType type)
171 {
173 
174  auxLayer->setSRID(srid);
175  auxLayer->setExtent(env);
176 
177  createCellSpace(outputSource, name, auxLayer, resX, resY, false, type);
178 }
179 
181 {
182  char celId[32];
183  sprintf(celId,"C%02dL%02d",col,row);
184 
186  item->setString(0, celId);
187  item->setInt32(1, col);
188  item->setInt32(2, row);
189  item->setGeometry(3, geom);
190  ds->add(item);
191 }
192 
194 {
196 
197  std::auto_ptr<te::da::DataSet> ds = layerBase->getData();
198 
199  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(ds.get());
200 
201  ds->moveBeforeFirst();
202 
203  int count = 0;
204 
205  while(ds->moveNext())
206  {
207  std::auto_ptr<te::gm::Geometry> geom = ds->getGeometry(geomPos);
208 
209  rtree->insert(*geom->getMBR(), count);
210 
211  ++count;
212  }
213 
214  return rtree;
215 }
216 
218 {
219  te::da::DataSetType* dst = new te::da::DataSetType(name);
220 
221  te::dt::Property* idProp = new te::dt::StringProperty("id");
224  te::dt::Property* geomProp = 0;
225 
226  if(type == CELLSPACE_POLYGONS)
227  geomProp = new te::gm::GeometryProperty("geom", srid, te::gm::PolygonType);
228  else if(type == CELLSPACE_POINTS)
229  geomProp = new te::gm::GeometryProperty("geom", srid, te::gm::PointType);
230 
231  dst->add(idProp);
232  dst->add(colProp);
233  dst->add(rowProp);
234  dst->add(geomProp);
235 
236  std::string pkName = name + "_pk_id";
237  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dst);
238  std::vector<te::dt::Property*> pkProp;
239  pkProp.push_back(idProp);
240  pk->setProperties(pkProp);
241 
242  return dst;
243 }
void addCell(te::mem::DataSet *ds, int col, int row, te::gm::Geometry *geom)
Add a cell in the memory dataset.
Geometric property.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
An atomic property like an integer or double.
A class that represents an R-tree.
Definition: Index.h:56
A class that models the description of a dataset.
Definition: DataSetType.h:72
void useTimer(bool flag)
Used to define if task use progress timer information.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
It models a property definition.
Definition: Property.h:59
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
Definition: DataSet.cpp:172
void setTotalSteps(int value)
Set the task total stepes.
te::da::DataSetType * createCellularDataSetType(const std::string &name, int srid, CellSpaceType type)
Create the DataSetType of the cellular space.
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
static std::auto_ptr< DataSource > make(const std::string &dsType)
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the primary key.
Definition: PrimaryKey.h:116
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
te::sam::rtree::Index< size_t, 8 > * getRtree(te::map::AbstractLayerPtr layerBase)
Get a RTree with the geometries envelopes of layer.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
void add(Constraint *c)
It adds a new constraint.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
Definition: Index.h:313
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
void createCellSpace(te::da::DataSourceInfoPtr outputSource, const std::string &name, te::map::AbstractLayerPtr layerBase, double resX, double resY, bool useMask, CellSpaceType type=CELLSPACE_POLYGONS)
It create a Cellular Space.