examples/attributefill/RasterToVector.cpp
Go to the documentation of this file.
1 
2 #include "../Config.h"
4 #include <terralib/common.h>
5 #include <terralib/dataaccess.h>
8 
9 // STL
10 #include <iostream>
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 // Boost
17 #include <boost/filesystem.hpp>
18 
19 // Adapt the source and target datasource information to your environment!
21 {
22 // Input Raster
23  std::string dataDirRaster(TERRALIB_DATA_DIR "/geotiff");
24  std::string fileNameRaster = "cbers2b_rgb342_crop.tif";
25 
26  std::string connInfoRaster("file://" + dataDirRaster);
27 
28  te::da::DataSourcePtr dsGDAL(te::da::DataSourceFactory::make("GDAL", connInfoRaster).release());
29  dsGDAL->open();
30 
31  std::unique_ptr<te::da::DataSet> dsRaster = dsGDAL->getDataSet(fileNameRaster);
32 
33  std::size_t rpos = te::da::GetFirstPropertyPos(dsRaster.get(), te::dt::RASTER_TYPE);
34 
35  std::unique_ptr<te::rst::Raster> inputRst = dsRaster->getRaster(rpos);
36 
37 
38 // Input Vector
39  std::string dataDirVector(TERRALIB_DATA_DIR "/shape/shapeTeste.shp");
40  std::string fileNameVector = "shapeTeste";
41 
42  std::string connInfoVector("file://" + dataDirVector);
43 
44  te::da::DataSourcePtr dsOGR(te::da::DataSourceFactory::make("OGR", connInfoVector).release());
45  dsOGR->open();
46 
47  std::unique_ptr<te::da::DataSetType>dsTypeVector = dsOGR->getDataSetType(fileNameVector);
48 
49  std::unique_ptr<te::da::DataSetTypeConverter> converterVector(new te::da::DataSetTypeConverter(dsTypeVector.get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
50 
51  // Params
52  std::vector<unsigned int> vecBands;
53  vecBands.push_back(0);
54  vecBands.push_back(1);
55 
56  std::vector<te::stat::StatisticalSummary> vecStat;
57  vecStat.push_back(te::stat::MIN_VALUE);
58  vecStat.push_back(te::stat::MAX_VALUE);
59  vecStat.push_back(te::stat::SUM);
60 
61 // Output Vector
62  std::string outputdataset = "raster2vector";
63  std::string outputdatadir = "/shp/" + outputdataset + ".shp";
64 
65  boost::filesystem::path uri(TERRALIB_DATA_DIR + outputdatadir);
66  if (te::core::FileSystem::exists(uri.string()))
67  {
68  std::cout << "Output file already exists. Remove it or select a new name and try again.\n";
69  return false;
70  }
71 
72  std::string dsinfo("file://" + uri.string());
73 
74  te::da::DataSourcePtr outDataSource(te::da::DataSourceFactory::make("OGR", dsinfo).release());
75  outDataSource->open();
76  if (outDataSource->dataSetExists(outputdataset))
77  {
78  std::cout << "There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again.\n";
79  return false;
80  }
81 
82 // Processing
84 
85  rst2vec->setInput(inputRst.get(),
86  dsOGR, fileNameVector, converterVector.get());
87  rst2vec->setParams(vecBands, vecStat, false, false);
88  rst2vec->setOutput(outDataSource, outputdataset);
89 
90  bool res;
91 
92  if (!rst2vec->paramsAreValid())
93  res = false;
94  else
95  res = rst2vec->run();
96 
97  if (!res)
98  {
99  dsOGR->close();
100  std::cout << "Error: could not generate the operation.";
101  }
102  dsOGR->close();
103 
104  delete rst2vec;
105 
106  return res;
107 }
108 
109 // Adapt the source and target datasource information to your environment!
111 {
112  // Input Raster
113  std::string dataDirRaster(TERRALIB_DATA_DIR "/geotiff");
114  std::string fileNameRaster = "cbers2b_rgb342_crop.tif";
115 
116  std::string connInfoRaster("file://" + dataDirRaster);
117  te::da::DataSourcePtr dsGDAL(te::da::DataSourceFactory::make("GDAL", connInfoRaster).release());
118  dsGDAL->open();
119 
120  std::unique_ptr<te::da::DataSet> dsRaster = dsGDAL->getDataSet(fileNameRaster);
121 
122  std::size_t rpos = te::da::GetFirstPropertyPos(dsRaster.get(), te::dt::RASTER_TYPE);
123 
124  std::unique_ptr<te::rst::Raster> inputRst = dsRaster->getRaster(rpos);
125 
126 
127  // Input Vector
128  std::string dataDirVector(TERRALIB_DATA_DIR "/shape/shapeTeste.shp");
129  std::string fileNameVector = "shapeTeste";
130 
131  std::string connInfoVector("file://" + dataDirVector);
132 
133  te::da::DataSourcePtr dsOGR(te::da::DataSourceFactory::make("OGR", connInfoVector).release());
134  dsOGR->open();
135 
136  std::unique_ptr<te::da::DataSetType>dsTypeVector = dsOGR->getDataSetType(fileNameVector);
137 
138  std::unique_ptr<te::da::DataSetTypeConverter> converterVector(new te::da::DataSetTypeConverter(dsTypeVector.get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
139 
140 
141  // Params
142  std::vector<unsigned int> vecBands;
143  vecBands.push_back(0);
144  vecBands.push_back(1);
145 
146  std::vector<te::stat::StatisticalSummary> vecStat;
147  vecStat.push_back(te::stat::MIN_VALUE);
148  vecStat.push_back(te::stat::MAX_VALUE);
149  vecStat.push_back(te::stat::SUM);
150 
151  // Output Vector
152  std::string connInfo("ppgsql://postgres:postgres@atlas.dpi.inpe.br:5433/testPostGIS");
153 
154  te::da::DataSourcePtr outDataSource(te::da::DataSourceFactory::make("POSTGIS", connInfo).release());
155  outDataSource->open();
156 
157  std::string outputdataset = "raster2vector";
158 
159  if (outDataSource->dataSetExists(outputdataset))
160  {
161  std::cout << "There is already a dataset with the requested name in the output data source. Remove it or select a new name and try again.\n";
162  return false;
163  }
164 
165  // Processing
167 
168  rst2vec->setInput(inputRst.get(),
169  dsOGR, fileNameVector, converterVector.get());
170  rst2vec->setParams(vecBands, vecStat, false, false);
171  rst2vec->setOutput(outDataSource, outputdataset);
172 
173  bool res;
174 
175  if (!rst2vec->paramsAreValid())
176  res = false;
177  else
178  res = rst2vec->run();
179 
180  if (!res)
181  {
182  dsOGR->close();
183  std::cout << "Error: could not generate the operation.";
184  }
185  dsOGR->close();
186 
187  delete rst2vec;
188 
189  return res;
190 }
void setInput(te::rst::Raster *inRaster, te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, te::da::DataSetTypeConverter *inVectorDsType, const te::da::ObjectIdSet *oidSet=0)
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
boost::shared_ptr< DataSource > DataSourcePtr
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
An converter for DataSetType.
Raster to Vector processing.
A class for handling system files and paths.
A factory for data sources.
void setParams(std::vector< unsigned int > bands, std::vector< te::stat::StatisticalSummary > statSum, bool iteratorByBox, bool texture)
This file contains include headers for the TerraLib Common Runtime module.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
This file contains include headers for the Data Access module of TerraLib.