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 
52 
54  const std::string& name,
55  const double& resX,
56  const double& resY,
57  const te::gm::Envelope& env,
58  const int srid,
59  const CellSpaceType type,
60  te::map::AbstractLayerPtr layerBase)
61 {
62  int maxcols = (int)ceil((env.m_urx-env.m_llx)/resX),
63  maxrows = (int)ceil((env.m_ury-env.m_lly)/resY);
64 
65  bool useMask = false;
66  std::auto_ptr<te::da::DataSet> refDs;
67  if(layerBase.get())
68  {
69  refDs = layerBase->getData();
70  useMask=true;
71  }
72 
73  std::auto_ptr<te::da::DataSetType> outputDataSetType(createCellularDataSetType(name, srid, type));
74 
75  std::auto_ptr<te::sam::rtree::Index<size_t, 8> > rtree;
76  if(useMask)
77  rtree.reset(getRtree(layerBase));
78 
79  te::common::TaskProgress task("Processing Cellular Spaces...");
80  task.setTotalSteps(maxrows);
81  task.useTimer(true);
82 
83  std::auto_ptr<te::da::DataSet> outputDataSet(new te::mem::DataSet(outputDataSetType.get()));
84 
85  std::auto_ptr<te::mem::DataSet> ds(dynamic_cast<te::mem::DataSet*>(outputDataSet.release()));
86 
87  double x, y;
88  for(int lin = 0; lin < maxrows; ++lin)
89  {
90  y = env.m_lly+(lin*resY);
91  for(int col = 0; col < maxcols; ++col)
92  {
93  x = env.m_llx+(col*resX);
94 
95  te::gm::Envelope* env = new te::gm::Envelope(x, y, x+resX, y+resY);
96 
97  std::auto_ptr<te::gm::Geometry> geom;
98  if(type == CELLSPACE_POLYGONS)
99  {
100  geom.reset(te::gm::GetGeomFromEnvelope(env, srid));
101  }
102  else if(type == CELLSPACE_POINTS)
103  {
104  double pX = env->m_llx +( (env->m_urx - env->m_llx) / 2);
105  double pY = env->m_lly +( (env->m_ury - env->m_lly) / 2);
106  geom.reset(new te::gm::Point(pX, pY, srid));
107  }
108 
109  if(useMask)
110  {
111  std::vector<size_t> report;
112  rtree->search(*geom->getMBR(), report);
113 
114  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(refDs.get());
115 
116  if(!report.empty())
117  {
118  for(std::size_t i = 0; i < report.size(); ++i)
119  {
120  refDs->move(report[i]);
121 
122  std::auto_ptr<te::gm::Geometry> g = refDs->getGeometry(geomPos);
123  g->setSRID(srid);
124 
125  if(geom->intersects(g.get()))
126  {
127  addCell(ds.get(), col, lin, geom.release());
128  break;
129  }
130  }
131  }
132  }
133  else
134  {
135  addCell(ds.get(), col, lin, geom.release());
136  }
137  }
138 
139  task.pulse();
140  if (!task.isActive())
141  {
142  throw te::common::Exception(TE_TR("Operation canceled!"));
143  }
144  }
145 
146  // Output
147  std::auto_ptr<te::da::DataSource> source = te::da::DataSourceFactory::make(outputSource->getAccessDriver());
148  source->setConnectionInfo(outputSource->getConnInfo());
149  source->open();
150 
151  std::map<std::string, std::string> options;
152  // create the dataset
153  source->createDataSet(outputDataSetType.get(), options);
154 
155  // copy from memory to output datasource
156  ds->moveBeforeFirst();
157  source->add(outputDataSetType->getName(),ds.get(), options);
158 }
159 
161 {
162  char celId[32];
163  sprintf(celId,"C%02dL%02d",col,row);
164 
166  item->setString(0, celId);
167  item->setInt32(1, col);
168  item->setInt32(2, row);
169  item->setGeometry(3, geom);
170  ds->add(item);
171 }
172 
174 {
176 
177  std::auto_ptr<te::da::DataSet> ds = layerBase->getData();
178 
179  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(ds.get());
180 
181  ds->moveBeforeFirst();
182 
183  int count = 0;
184 
185  while(ds->moveNext())
186  {
187  std::auto_ptr<te::gm::Geometry> geom = ds->getGeometry(geomPos);
188 
189  rtree->insert(*geom->getMBR(), count);
190 
191  ++count;
192  }
193 
194  return rtree;
195 }
196 
198 {
199  te::da::DataSetType* dst = new te::da::DataSetType(name);
200 
202  idProp->setSize(255);
205  te::dt::Property* geomProp = 0;
206 
207  if(type == CELLSPACE_POLYGONS)
208  geomProp = new te::gm::GeometryProperty("geom", srid, te::gm::PolygonType);
209  else if(type == CELLSPACE_POINTS)
210  geomProp = new te::gm::GeometryProperty("geom", srid, te::gm::PointType);
211 
212  dst->add(idProp);
213  dst->add(colProp);
214  dst->add(rowProp);
215  dst->add(geomProp);
216 
217  std::string pkName = name + "_pk_id";
218  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dst);
219  std::vector<te::dt::Property*> pkProp;
220  pkProp.push_back(idProp);
221  pk->setProperties(pkProp);
222 
223  return dst;
224 }
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.
void setSize(std::size_t s)
It sets the maximum number of characters for a varying string, or the number of characters for a fixe...
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
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
It models a property definition.
Definition: Property.h:59
bool isActive() const
Verify if the task is active.
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.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
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
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void createCellSpace(te::da::DataSourceInfoPtr outputSource, const std::string &name, const double &resX, const double &resY, const te::gm::Envelope &env, const int srid, CellSpaceType type, te::map::AbstractLayerPtr layerBase)
It creates a Cellular Space.
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