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  boost::filesystem::path uri(fileName);
98 
99  std::string val = uri.stem().string();
100 
101  dsType->setName(val);
102 
103  //get dataset
104  std::auto_ptr<te::da::DataSet> dataset = getDataSet(srid);
105 
106  //exchange
107  std::map<std::string,std::string> nopt;
108 
109  dsOGR->createDataSet(dsType.get(), nopt);
110 
111  if(dataset->moveBeforeFirst())
112  dsOGR->add(dsType->getName(), dataset.get(), dsOGR->getConnectionInfo());
113 
114  dsOGR->close();
115 }
116 
117 te::cl::ROISet* te::cl::ROISet::createROISet(std::auto_ptr<te::da::DataSet> ds)
118 {
119  //check the input dataset
120  assert(ds.get());
121  assert(ds->getNumProperties() == 5);
122  assert(ds->getPropertyName(1) == TE_CL_ROI_GEOM_ID_NAME);
123  assert(ds->getPropertyName(2) == TE_CL_ROI_LABEL_NAME);
124  assert(ds->getPropertyName(3) == TE_CL_ROI_COLOR_NAME);
125  //assert(ds->getPropertyName(4) == TE_CL_ROI_GEOM_NAME); //OGR_GEOMETRY
126 
127  //move the data set to begin
128  ds->moveBeforeFirst();
129 
130  te::cl::ROISet* rs = new te::cl::ROISet();
131 
132  while(ds->moveNext())
133  {
134  std::string label = ds->getString(TE_CL_ROI_LABEL_NAME);
135 
136  te::cl::ROI* roi = rs->getROI(label);
137 
138  if(roi)
139  {
140  std::string pId = ds->getString(TE_CL_ROI_GEOM_ID_NAME);
141 
142  te::gm::MultiPolygon* mp = (te::gm::MultiPolygon*)ds->getGeometry(4).release();
144 
145  roi->addPolygon(p, pId);
146  }
147  else
148  {
149  std::string color = ds->getString(TE_CL_ROI_COLOR_NAME);
150  std::string pId = ds->getString(TE_CL_ROI_GEOM_ID_NAME);
151 
152  te::gm::MultiPolygon* mp = (te::gm::MultiPolygon*)ds->getGeometry(4).release();
154 
155  te::cl::ROI* r = new te::cl::ROI(label);
156  r->setColor(color);
157  r->addPolygon(p, pId);
158 
159  rs->addROI(r);
160  }
161  }
162 
163  return rs;
164 }
165 
166 std::auto_ptr<te::da::DataSetType> te::cl::ROISet::getDataSetType(int srid)
167 {
168  std::auto_ptr<te::da::DataSetType> dsType;
169 
174 
176  pk->add(geomIdProp);
177 
178  te::da::DataSetType* dst = new te::da::DataSetType("ROISet");
179  dst->add(geomIdProp);
180  dst->add(labelProp);
181  dst->add(colorProp);
182  dst->add(geomProp);
183  dst->add(pk);
184 
185  dsType.reset(dst);
186 
187  return dsType;
188 }
189 
190 std::auto_ptr<te::da::DataSet> te::cl::ROISet::getDataSet(int srid)
191 {
192  std::auto_ptr<te::da::DataSet> ds;
193 
194  std::auto_ptr<te::da::DataSetType> dsType = getDataSetType(srid);
195 
196  te::da::DataSet* dsMem = new te::mem::DataSet(dsType.get());
197 
198  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.begin();
199 
200  while(it != m_roiMap.end())
201  {
202  te::cl::ROI* roi = it->second;
203 
204  std::map<std::string, te::gm::Polygon*> roiMap = roi->getPolygons();
205 
206  std::map<std::string, te::gm::Polygon*>::iterator itPols = roiMap.begin();
207 
208  while(itPols != roiMap.end())
209  {
210  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(dsMem);
211 
212  dsItem->setString(TE_CL_ROI_GEOM_ID_NAME, itPols->first);
213  dsItem->setString(TE_CL_ROI_LABEL_NAME, roi->getLabel());
214  dsItem->setString(TE_CL_ROI_COLOR_NAME, roi->getColor());
215 
216  te::gm::Polygon* poly = (te::gm::Polygon*)itPols->second->clone();
217 
219 
220  ((te::mem::DataSet*)dsMem)->add(dsItem);
221 
222  ++itPols;
223  }
224 
225  ++it;
226  }
227 
228  ds.reset(dsMem);
229 
230  return ds;
231 }
#define TE_CL_ROI_GEOM_NAME
This mark defines the geom attribute name.
Definition: Config.h:65
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:51
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:117
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
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:190
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:44
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:72
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:166
#define TE_CL_ROI_COLOR_NAME
This mark defines the color attribute name.
Definition: Config.h:58
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