All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ROISet.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/classification/ROISet.cpp
22 
23  \brief A ROISet is a set of ROI's.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../dataaccess/dataset/DataSet.h"
29 #include "../dataaccess/dataset/DataSetAdapter.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../dataaccess/dataset/DataSetTypeConverter.h"
32 #include "../dataaccess/datasource/DataSource.h"
33 #include "../dataaccess/datasource/DataSourceFactory.h"
34 #include "../dataaccess/utils/Utils.h"
35 #include "../datatype/StringProperty.h"
36 #include "../geometry/GeometryProperty.h"
37 #include "../geometry/MultiPolygon.h"
38 #include "../geometry/Polygon.h"
39 #include "../memory/DataSet.h"
40 #include "../memory/DataSetItem.h"
41 #include "ROISet.h"
42 
43 // Boost
44 #include <boost/filesystem.hpp>
45 
47 {
48 }
49 
51 {
52  te::common::FreeContents(m_roiMap);
53  m_roiMap.clear();
54 }
55 
57 {
58  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.find(label);
59 
60  if(it != m_roiMap.end())
61  return it->second;
62 
63  return 0;
64 }
65 
67 {
68  m_roiMap[roi->getLabel()] = roi;
69 }
70 
71 void te::cl::ROISet::removeROI(std::string label)
72 {
73  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.find(label);
74 
75  if(it != m_roiMap.end())
76  m_roiMap.erase(it);
77 }
78 
79 std::map<std::string, te::cl::ROI*>& te::cl::ROISet::getROISet()
80 {
81  return m_roiMap;
82 }
83 
84 void te::cl::ROISet::exportToFile(std::string fileName, int srid)
85 {
86  //get dsType
87  std::auto_ptr<te::da::DataSetType> dsType = getDataSetType(srid);
88 
89  //create data source
90  std::map<std::string, std::string> connInfo;
91  connInfo["URI"] = fileName;
92 
93  std::auto_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR");
94  dsOGR->setConnectionInfo(connInfo);
95  dsOGR->open();
96 
97  //create converter
98  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(dsType.get(), dsOGR->getCapabilities());
99 
100  te::da::DataSetType* dsTypeResult = converter->getResult();
101 
102  boost::filesystem::path uri(fileName);
103 
104  std::string val = uri.stem().string();
105 
106  dsTypeResult->setName(val);
107 
108  //get dataset
109  std::auto_ptr<te::da::DataSet> dataset = getDataSet(srid);
110 
111  //exchange
112  std::map<std::string,std::string> nopt;
113 
114  dsOGR->createDataSet(dsTypeResult, nopt);
115 
116  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), converter));
117 
118  dsAdapter->setSRID(srid);
119 
120  if(dataset->moveBeforeFirst())
121  dsOGR->add(dsTypeResult->getName(), dsAdapter.get(), dsOGR->getConnectionInfo());
122 
123  dsOGR->close();
124 }
125 
126 te::cl::ROISet* te::cl::ROISet::createROISet(std::auto_ptr<te::da::DataSet> ds)
127 {
128  //check the input dataset
129  assert(ds.get());
130  assert(ds->getNumProperties() == 5);
131  assert(ds->getPropertyName(1) == TE_CL_ROI_GEOM_ID_NAME);
132  assert(ds->getPropertyName(2) == TE_CL_ROI_LABEL_NAME);
133  assert(ds->getPropertyName(3) == TE_CL_ROI_COLOR_NAME);
134  //assert(ds->getPropertyName(4) == TE_CL_ROI_GEOM_NAME); //OGR_GEOMETRY
135 
136  //move the data set to begin
137  ds->moveBeforeFirst();
138 
139  te::cl::ROISet* rs = new te::cl::ROISet();
140 
141  while(ds->moveNext())
142  {
143  std::string label = ds->getString(TE_CL_ROI_LABEL_NAME);
144 
145  te::cl::ROI* roi = rs->getROI(label);
146 
147  if(roi)
148  {
149  std::string pId = ds->getString(TE_CL_ROI_GEOM_ID_NAME);
150 
151  te::gm::MultiPolygon* mp = (te::gm::MultiPolygon*)ds->getGeometry(4).release();
153 
154  roi->addPolygon(p, pId);
155  }
156  else
157  {
158  std::string color = ds->getString(TE_CL_ROI_COLOR_NAME);
159  std::string pId = ds->getString(TE_CL_ROI_GEOM_ID_NAME);
160 
161  te::gm::MultiPolygon* mp = (te::gm::MultiPolygon*)ds->getGeometry(4).release();
163 
164  te::cl::ROI* r = new te::cl::ROI(label);
165  r->setColor(color);
166  r->addPolygon(p, pId);
167 
168  rs->addROI(r);
169  }
170  }
171 
172  return rs;
173 }
174 
175 std::auto_ptr<te::da::DataSetType> te::cl::ROISet::getDataSetType(int srid)
176 {
177  std::auto_ptr<te::da::DataSetType> dsType;
178 
183 
185  pk->add(geomIdProp);
186 
187  te::da::DataSetType* dst = new te::da::DataSetType("ROISet");
188  dst->add(geomIdProp);
189  dst->add(labelProp);
190  dst->add(colorProp);
191  dst->add(geomProp);
192  dst->add(pk);
193 
194  dsType.reset(dst);
195 
196  return dsType;
197 }
198 
199 std::auto_ptr<te::da::DataSet> te::cl::ROISet::getDataSet(int srid)
200 {
201  std::auto_ptr<te::da::DataSet> ds;
202 
203  std::auto_ptr<te::da::DataSetType> dsType = getDataSetType(srid);
204 
205  te::da::DataSet* dsMem = new te::mem::DataSet(dsType.get());
206 
207  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.begin();
208 
209  while(it != m_roiMap.end())
210  {
211  te::cl::ROI* roi = it->second;
212 
213  std::map<std::string, te::gm::Polygon*> roiMap = roi->getPolygons();
214 
215  std::map<std::string, te::gm::Polygon*>::iterator itPols = roiMap.begin();
216 
217  while(itPols != roiMap.end())
218  {
219  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(dsMem);
220 
221  dsItem->setString(TE_CL_ROI_GEOM_ID_NAME, itPols->first);
222  dsItem->setString(TE_CL_ROI_LABEL_NAME, roi->getLabel());
223  dsItem->setString(TE_CL_ROI_COLOR_NAME, roi->getColor());
224 
225  te::gm::Polygon* poly = (te::gm::Polygon*)itPols->second->clone();
226 
228 
229  ((te::mem::DataSet*)dsMem)->add(dsItem);
230 
231  ++itPols;
232  }
233 
234  ++it;
235  }
236 
237  ds.reset(dsMem);
238 
239  return ds;
240 }
#define TE_CL_ROI_GEOM_NAME
This mark defines the geom attribute name.
Definition: Config.h:66
te::cl::ROI * getROI(std::string label)
Gets a ROI from this set.
Definition: ROISet.cpp:56
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
Geometric property.
void add(te::dt::Property *p)
It adds a property to the list of properties of the primary key.
Definition: PrimaryKey.h:123
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
#define TE_CL_ROI_LABEL_NAME
This mark defines the label attribute name.
Definition: Config.h:52
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual ~ROISet()
Virtual destructor.
Definition: ROISet.cpp:50
static te::cl::ROISet * createROISet(std::auto_ptr< te::da::DataSet > ds)
Imports the ROISet from a dataset.
Definition: ROISet.cpp:126
void addPolygon(te::gm::Polygon *p, std::string id)
Add a new region into this ROI.
Definition: ROI.cpp:67
A ROISet is a set of ROI's.
void addROI(te::cl::ROI *roi)
Add a new ROI to this set.
Definition: ROISet.cpp:66
void exportToFile(std::string fileName, int srid)
Export the ROISet to a shapefile.
Definition: ROISet.cpp:84
A ROISet is a set of ROI's.
Definition: ROISet.h:53
virtual te::dt::AbstractData * clone() const
It clones the linestring.
Definition: Polygon.cpp:52
An converter for DataSetType.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
std::auto_ptr< te::da::DataSet > getDataSet(int srid)
Creates a dataset that with the roi set information.
Definition: ROISet.cpp:199
static std::auto_ptr< DataSource > make(const std::string &dsType)
void removeROI(std::string label)
Removes a ROI from this set.
Definition: ROISet.cpp:71
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
void setColor(std::string color)
Set the ROI color using the hexadecimal color name.
Definition: ROI.cpp:52
A region of interest (often abbreviated ROI), is a selected subset of samples within a dataset identi...
Definition: ROI.h:60
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
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
#define TE_CL_ROI_GEOM_ID_NAME
This mark defines the geom id attribute name.
Definition: Config.h:45
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
const std::vector< Geometry * > & getGeometries() const
It returns a reference to the internal list of geometries.
std::string getLabel()
Get the ROI label.
Definition: ROI.cpp:47
ROISet()
Default constructor.
Definition: ROISet.cpp:46
#define TE_CL_ROI_PK_NAME
This mark defines the primary key attribute name.
Definition: Config.h:73
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
std::auto_ptr< te::da::DataSetType > getDataSetType(int srid)
Creates a dataset type that defines a roi.
Definition: ROISet.cpp:175
#define TE_CL_ROI_COLOR_NAME
This mark defines the color attribute name.
Definition: Config.h:59
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
std::map< std::string, te::cl::ROI * > & getROISet()
Get the roi set map.
Definition: ROISet.cpp:79
std::map< std::string, te::gm::Polygon * > & getPolygons()
Get all polygons belongs to this roi.
Definition: ROI.cpp:62
std::string getColor()
Get the ROI color defined by a hexadecimal color name.
Definition: ROI.cpp:57