examples/attributefill/VectorToRaster.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 #include <boost/uuid/random_generator.hpp>
19 #include <boost/uuid/uuid_io.hpp>
20 
21 // Adapt the source and target datasource information to your environment!
23 {
24  std::string filename(TERRALIB_DATA_DIR "/shape/SP_cities.shp");
25 
26  std::string srcInfo("file://" + filename);
27 
28  te::da::DataSourcePtr srcDs(te::da::DataSourceFactory::make("OGR", srcInfo).release());
29  srcDs->open();
30 
31  std::string inDset = "SP_cities";
32  if (!srcDs->dataSetExists(inDset))
33  {
34  std::cout << "input dataset not found: " << inDset << std::endl;
35  return false;
36  }
37 
38  std::unique_ptr<te::da::DataSetType> dsType(srcDs->getDataSetType(inDset));
39  std::vector<te::dt::Property*> props = dsType->getProperties();
40 
41  std::vector<std::string> propName;
42 
43 
44  for (std::size_t i = 0; i < props.size(); ++i)
45  {
46  if (props[i]->getName() == "FID" || props[i]->getName() == "POPULACA")
47  propName.push_back(props[i]->getName());
48  }
49 
50  double resolutionX = 0.0111898;
51  double resolutionY = 0.00868972;
52  int columns = 1000;
53  int rows = 1000;
54 
55  boost::filesystem::path uri(TERRALIB_DATA_DIR "/geotiff/vector2raster.tif");
56  std::string dsName = "vector2raster";
57 
58  if (te::core::FileSystem::exists(uri.string()))
59  {
60  std::cout << "Output file already exists. Remove it or select a new name and try again.";
61  return false;
62  }
63 
64  std::string dsinfo("file://" + uri.string());
65  te::da::DataSourcePtr dsOGR(te::da::DataSourceFactory::make("OGR", dsinfo).release());
66  dsOGR->open();
67  if (dsOGR->dataSetExists(dsName))
68  {
69  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.";
70  return false;
71  }
72 
73  std::unique_ptr<te::da::DataSetTypeConverter> converterVector(new te::da::DataSetTypeConverter(dsType.get(), dsOGR->getCapabilities(), dsOGR->getEncoding()));
74 
76 
77  vec2rst->setInput(srcDs, inDset, std::move(converterVector));
78  vec2rst->setParams( propName,
79  resolutionX,
80  resolutionY,
81  columns,
82  rows,
83  false);
84  vec2rst->setOutput(dsOGR, dsName);
85 
86  bool res;
87 
88  if (!vec2rst->paramsAreValid())
89  res = false;
90  else
91  res = vec2rst->run();
92 
93  if (!res)
94  {
95  dsOGR->close();
96  std::cout << "Error: could not generate the operation.";
97  }
98  dsOGR->close();
99 
100  delete vec2rst;
101 
102  return res;
103 }
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
An converter for DataSetType.
void setInput(te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, std::unique_ptr< te::da::DataSetTypeConverter > inVectorDsType)
A class for handling system files and paths.
A factory for data sources.
Vector to Raster processing.
void setParams(std::vector< std::string > selectedAttVec, double resolutionX, double resolutionY, int columns, int rows, bool useDummy, int dummy=0)
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Data Access module of TerraLib.
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)