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 
46 te::cl::ROISet::ROISet() = default;
47 
49 {
51  m_roiMap.clear();
52 }
53 
55 {
56  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.find(label);
57 
58  if(it != m_roiMap.end())
59  return it->second;
60 
61  return nullptr;
62 }
63 
65 {
66  m_roiMap[roi->getLabel()] = roi;
67 }
68 
69 void te::cl::ROISet::removeROI(std::string label)
70 {
71  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.find(label);
72 
73  if(it != m_roiMap.end())
74  m_roiMap.erase(it);
75 }
76 
77 std::map<std::string, te::cl::ROI*>& te::cl::ROISet::getROISet()
78 {
79  return m_roiMap;
80 }
81 
82 void te::cl::ROISet::exportToFile(std::string fileName, int srid)
83 {
84  //get dsType
85  std::unique_ptr<te::da::DataSetType> dsType = getDataSetType(srid);
86 
87  //create data source
88  std::string connInfo("file://" + fileName);
89  std::unique_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR", connInfo);
90 
91  dsOGR->open();
92 
93  boost::filesystem::path uri(fileName);
94 
95  std::string val = uri.stem().string();
96 
97  dsType->setName(val);
98 
99  //get dataset
100  std::unique_ptr<te::da::DataSet> dataset = getDataSet(srid);
101 
102  //exchange
103  std::map<std::string,std::string> nopt;
104 
105  dsOGR->createDataSet(dsType.get(), nopt);
106 
107  if(dataset->moveBeforeFirst())
108  dsOGR->add(dsType->getName(), dataset.get(), nopt);
109 
110  dsOGR->close();
111 }
112 
113 te::cl::ROISet* te::cl::ROISet::createROISet(std::unique_ptr<te::da::DataSet> ds)
114 {
115  //check the input dataset
116  assert(ds.get());
117  assert(ds->getNumProperties() == 5);
118  assert(ds->getPropertyName(1) == TE_CL_ROI_GEOM_ID_NAME);
119  assert(ds->getPropertyName(2) == TE_CL_ROI_LABEL_NAME);
120  assert(ds->getPropertyName(3) == TE_CL_ROI_COLOR_NAME);
121  //assert(ds->getPropertyName(4) == TE_CL_ROI_GEOM_NAME); //OGR_GEOMETRY
122 
123  //move the data set to begin
124  ds->moveBeforeFirst();
125 
126  te::cl::ROISet* rs = new te::cl::ROISet();
127 
128  while(ds->moveNext())
129  {
130  std::string label = ds->getString(TE_CL_ROI_LABEL_NAME);
131 
132  te::cl::ROI* roi = rs->getROI(label);
133 
134  if(roi)
135  {
136  std::string pId = ds->getString(TE_CL_ROI_GEOM_ID_NAME);
137 
138  te::gm::MultiPolygon* mp = (te::gm::MultiPolygon*)ds->getGeometry(4).release();
140 
141  roi->addPolygon(p, pId);
142  }
143  else
144  {
145  std::string color = ds->getString(TE_CL_ROI_COLOR_NAME);
146  std::string pId = ds->getString(TE_CL_ROI_GEOM_ID_NAME);
147 
148  te::gm::MultiPolygon* mp = (te::gm::MultiPolygon*)ds->getGeometry(4).release();
150 
151  te::cl::ROI* r = new te::cl::ROI(label);
152  r->setColor(color);
153  r->addPolygon(p, pId);
154 
155  rs->addROI(r);
156  }
157  }
158 
159  return rs;
160 }
161 
162 std::unique_ptr<te::da::DataSetType> te::cl::ROISet::getDataSetType(int srid)
163 {
164  std::unique_ptr<te::da::DataSetType> dsType;
165 
170 
172  pk->add(geomIdProp);
173 
174  te::da::DataSetType* dst = new te::da::DataSetType("ROISet");
175  dst->add(geomIdProp);
176  dst->add(labelProp);
177  dst->add(colorProp);
178  dst->add(geomProp);
179  dst->add(pk);
180 
181  dsType.reset(dst);
182 
183  return dsType;
184 }
185 
186 std::unique_ptr<te::da::DataSet> te::cl::ROISet::getDataSet(int srid)
187 {
188  std::unique_ptr<te::da::DataSet> ds;
189 
190  std::unique_ptr<te::da::DataSetType> dsType = getDataSetType(srid);
191 
192  te::da::DataSet* dsMem = new te::mem::DataSet(dsType.get());
193 
194  std::map<std::string, te::cl::ROI*>::iterator it = m_roiMap.begin();
195 
196  while(it != m_roiMap.end())
197  {
198  te::cl::ROI* roi = it->second;
199 
200  std::map<std::string, te::gm::Polygon*> roiMap = roi->getPolygons();
201 
202  std::map<std::string, te::gm::Polygon*>::iterator itPols = roiMap.begin();
203 
204  while(itPols != roiMap.end())
205  {
206  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(dsMem);
207 
208  dsItem->setString(TE_CL_ROI_GEOM_ID_NAME, itPols->first);
209  dsItem->setString(TE_CL_ROI_LABEL_NAME, roi->getLabel());
210  dsItem->setString(TE_CL_ROI_COLOR_NAME, roi->getColor());
211 
212  te::gm::Polygon* poly = (te::gm::Polygon*)itPols->second->clone();
213 
215 
216  ((te::mem::DataSet*)dsMem)->add(dsItem);
217 
218  ++itPols;
219  }
220 
221  ++it;
222  }
223 
224  ds.reset(dsMem);
225 
226  return ds;
227 }
te::cl::ROI * getROI(std::string label)
Gets a ROI from this set.
Definition: ROISet.cpp:54
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
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.
std::unique_ptr< te::da::DataSet > getDataSet(int srid)
Creates a dataset that with the roi set information.
Definition: ROISet.cpp:186
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual ~ROISet()
Virtual destructor.
Definition: ROISet.cpp:48
void addPolygon(te::gm::Polygon *p, std::string id)
Add a new region into this ROI.
Definition: ROI.cpp:67
static te::dt::Date ds(2010, 01, 01)
A ROISet is a set of ROI&#39;s.
#define TE_CL_ROI_GEOM_ID_NAME
This mark defines the geom id attribute name.
void addROI(te::cl::ROI *roi)
Add a new ROI to this set.
Definition: ROISet.cpp:64
void exportToFile(std::string fileName, int srid)
Export the ROISet to a shapefile.
Definition: ROISet.cpp:82
std::unique_ptr< te::da::DataSetType > getDataSetType(int srid)
Creates a dataset type that defines a roi.
Definition: ROISet.cpp:162
A ROISet is a set of ROI&#39;s.
Definition: ROISet.h:53
virtual te::dt::AbstractData * clone() const
It clones the linestring.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
static te::cl::ROISet * createROISet(std::unique_ptr< te::da::DataSet > ds)
Imports the ROISet from a dataset.
Definition: ROISet.cpp:113
#define TE_CL_ROI_COLOR_NAME
This mark defines the color attribute name.
#define TE_CL_ROI_LABEL_NAME
This mark defines the label attribute name.
void DataSet()
std::map< std::string, te::cl::ROI * > m_roiMap
The map of ROI&#39;s.
Definition: ROISet.h:118
void removeROI(std::string label)
Removes a ROI from this set.
Definition: ROISet.cpp:69
te::gm::Polygon * p
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.
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
#define TE_CL_ROI_GEOM_NAME
This mark defines the geom attribute name.
void add(Constraint *c)
It adds a new constraint.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
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.
#define TE_CL_ROI_PK_NAME
This mark defines the primary key attribute name.
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
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:77
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