TsCloudDetection.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/unittest/rp/cloud_detection/TsCloudDetection.cpp
22 
23  \brief A test suit for the Cloud Detection interface.
24 */
25 
26 // TerraLib
27 #include "../Config.h"
35 #include <terralib/rp.h>
36 #include <terralib/raster.h>
37 
38 // Boost
39 #define BOOST_TEST_NO_MAIN
40 #include <boost/test/unit_test.hpp>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/uuid/random_generator.hpp>
43 #include <boost/uuid/uuid_io.hpp>
44 
45 BOOST_AUTO_TEST_CASE(cloudDetection_test)
46 {
47  /* Openning input raster */
48 
49  std::map<std::string, std::string> rasterInfo;
50 
51  rasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/Landsat8_22162_07102015_8bits_rec.tif";
52  boost::shared_ptr< te::rst::Raster > cloudRasterPtrPointer ( te::rst::RasterFactory::open(
53  rasterInfo));
54  BOOST_CHECK(cloudRasterPtrPointer.get());
55 
56  boost::shared_ptr< te::rst::Raster > shadowRasterPtrPointer(te::rst::RasterFactory::open(
57  rasterInfo));
58  BOOST_CHECK(shadowRasterPtrPointer.get());
59 
60  /* Creating the algorithm parameters */
61 
62  int cloudBand = 2;
63  int shadowBand = 1;
64  double minCloud = 130;
65  double maxCloud = 255;
66  double minShadow = 0;
67  double maxShadow = 60;
68 
69  std::vector<te::rp::Filter::InputParameters::FilterType> vecFilter;
70  std::vector<int> vecIt;
71 
72  std::unique_ptr<te::rp::CloudDetection> cloudDetection(new te::rp::CloudDetection());
73 
74  //run cloud detection operation
75  te::rst::Raster* raster = cloudDetection->executeCloudDetection(cloudRasterPtrPointer.get(), shadowRasterPtrPointer.get(), cloudBand, shadowBand,
76  minCloud, maxCloud, minShadow, maxShadow, vecFilter, vecIt);
77 
78  BOOST_CHECK( raster );
79 
80  //check output parameters
81  std::string outputdataset = "terralib_unittest_rp_clouddetection.shp";
82 
83  BOOST_CHECK(!outputdataset.empty());
84 
85  std::string uriStr = TERRALIB_BUILD_PATH "/terralib_unittest_rp/" + outputdataset;
86 
87  //check output datasource parameters
88  te::da::DataSourceInfoPtr outDSInfo;
89 
90  boost::filesystem::path uri(uriStr);
91 
92  std::size_t idx = outputdataset.find(".");
93  if (idx != std::string::npos)
94  outputdataset = outputdataset.substr(0, idx);
95 
96  std::string dsinfo("file://" + uri.string());
97 
98  // let's include the new datasource in the managers
99  boost::uuids::basic_random_generator<boost::mt19937> gen;
100  boost::uuids::uuid u = gen();
101  std::string id = boost::uuids::to_string(u);
102 
104  ds->setConnInfo(dsinfo);
105  ds->setTitle(uri.stem().string());
106  ds->setAccessDriver("OGR");
107  ds->setType("OGR");
108  ds->setDescription("Test Cloud Detection");
109  ds->setId(id);
110 
112  outDSInfo = ds;
113 
114  //output parameters
115  std::vector<te::gm::Geometry*> geomVec;
116  std::vector< double > geomsValues;
117 
118  //vectorize
119  raster->vectorize(geomVec, 0, 0, &geomsValues);
120 
121  BOOST_CHECK(!geomVec.empty());
122 
123  std::unique_ptr<te::da::DataSetType> dsType(new te::da::DataSetType(outputdataset));
124 
125  //create id property
127  dsType->add(idProperty);
128 
129  //create geometry property
130  te::gm::GeometryProperty* geomProperty = new te::gm::GeometryProperty("geom", raster->getSRID(), te::gm::PolygonType);
131  dsType->add(geomProperty);
132 
133  //create primary key
134  std::string pkName = "pk_id";
135  pkName += "_" + outputdataset;
136  te::da::PrimaryKey* pk = new te::da::PrimaryKey(pkName, dsType.get());
137  pk->add(idProperty);
138 
139  std::unique_ptr<te::mem::DataSet> dataSet(new te::mem::DataSet(dsType.get()));
140 
141  for (std::size_t t = 0; t < geomVec.size(); ++t)
142  {
143  //create dataset item
144  te::mem::DataSetItem* item = new te::mem::DataSetItem(dataSet.get());
145 
146  //set id
147  item->setInt32(0, (int)t);
148 
149  //set geometry
150  item->setGeometry(1, geomVec[t]);
151 
152  dataSet->add(item);
153  }
154 
155  te::da::DataSourcePtr dataSource = te::da::DataSourceManager::getInstance().get(outDSInfo->getId(), outDSInfo->getType(), outDSInfo->getConnInfo());
156 
157  //save dataset
158  dataSet->moveBeforeFirst();
159 
160  std::map<std::string, std::string> options;
161 
162  dataSource->createDataSet(dsType.get(), options);
163 
164  dataSource->add(outputdataset, dataSet.get(), options);
165 }
virtual void vectorize(std::vector< te::gm::Geometry * > &g, std::size_t b, unsigned int mp=0, std::vector< double > *const polygonsValues=0)
Vectorizes a given raster band, using GDALPolygonize function.
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.
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
This is a singleton for managing all data source instances available in the system.
static te::dt::Date ds(2010, 01, 01)
A singleton to keep all the registered data sources.
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...
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
An abstract class for raster data strucutures.
This class provide cloud detection.
A factory for data sources.
This file contains include headers for the TerraLib Raster Processing module.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
A conteiner class for keeping information about a data source.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
int getSRID() const
Returns the raster spatial reference system identifier.
BOOST_AUTO_TEST_CASE(cloudDetection_test)
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
A class that represents a data source component.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.