VectorToVector.cpp
Go to the documentation of this file.
1 
2 #include "../Config.h"
12 #include <terralib/se/Utils.h>
13 
14 // STL
15 #include <iostream>
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 // Boost
22 #include <boost/uuid/random_generator.hpp>
23 #include <boost/uuid/uuid_io.hpp>
24 
26 {
27  static boost::uuids::basic_random_generator<boost::mt19937> gen;
28 
29  if (dataset.get() == 0)
30  {
31  std::cout << "Can not convert a NULL dataset to a layer!" << std::endl;
32  return 0;
33  }
34 
35  boost::uuids::uuid u = gen();
36  std::string id = boost::uuids::to_string(u);
37 
38  std::string title = dataset->getTitle().empty() ? dataset->getName() : dataset->getTitle();
39 
40  te::map::DataSetLayerPtr layer(new te::map::DataSetLayer(id, title));
41  layer->setDataSetName(dataset->getName());
42  layer->setDataSourceId(source->getId());
43  layer->setVisibility(te::map::NOT_VISIBLE);
44  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
45 
46  if (dataset->size() == 0)
47  {
49  te::da::LoadProperties(dataset.get(), source->getId());
50  }
51 
53  std::unique_ptr<te::gm::Envelope> mbr(te::da::GetExtent(dataset->getName(), gp->getName(), source->getId()));
54  layer->setSRID(gp->getSRID());
55  if (mbr.get() != 0)
56  layer->setExtent(*mbr);
57  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
58 
59  return layer;
60 }
61 
63 {
64  std::string filename(TERRALIB_DATA_DIR "/shape/munic_2001.shp");
65 
66  std::string srcInfo("file://" + filename);
67 
68  static boost::uuids::basic_random_generator<boost::mt19937> gen;
69  boost::uuids::uuid u = gen();
70  std::string id = boost::uuids::to_string(u);
71  te::da::DataSourcePtr toSource(te::da::DataSourceFactory::make("OGR", srcInfo).release());
72  toSource->open();
73  toSource->setId(id);
74 
75  te::da::DataSourceManager::getInstance().insert(toSource);
76 
77  filename = TERRALIB_DATA_DIR "/shape/poligono_unico.shp";
78 
79  srcInfo = "file://" + filename;
80 
81  u = gen();
82  id = boost::uuids::to_string(u);
83  te::da::DataSourcePtr fromSource(te::da::DataSourceFactory::make("OGR", srcInfo).release());
84  fromSource->open();
85  fromSource->setId(id);
86 
87  te::da::DataSourceManager::getInstance().insert(fromSource);
88 
89  std::string toDsName = "munic_2001";
90  std::string fromDsName = "poligono_unico";
91 
92  te::da::DataSetTypePtr toDt = toSource->getDataSetType(toDsName);
93  te::da::DataSetTypePtr fromDt = toSource->getDataSetType(fromDsName);
94 
95  if (!toSource->dataSetExists(toDsName))
96  {
97  std::cout << "\"To\" dataset not found: " << toDsName << std::endl;
98  return false;
99  }
100 
101  if (!fromSource->dataSetExists(fromDsName))
102  {
103  std::cout << "\"From\" dataset not found: " << fromDsName << std::endl;
104  return false;
105  }
106 
107  std::unique_ptr<te::attributefill::VectorToVectorOp> v2v(new te::attributefill::VectorToVectorMemory());
108 
109  te::map::AbstractLayerPtr toLayer = createLayer(toSource, toDt);
110  te::map::AbstractLayerPtr fromLayer = createLayer(fromSource, fromDt);
111 
112  std::map<std::string, std::vector<te::attributefill::OperationType> > options;
113 
114  options["class_name"] = std::vector<te::attributefill::OperationType>(te::attributefill::PERCENT_TOTAL_AREA);
115 
116  std::vector<std::string> toLayerProps;
117  std::vector<te::dt::Property*> props = toDt->getProperties();
118  for (std::size_t i = 0; i < props.size(); ++i)
119  {
120  toLayerProps.push_back(props[i]->getName());
121  }
122 
123  te::da::DataSourcePtr outSource;
124  std::string outDsName;
125 
126 
127  std::string outDataSetName = "v2v_result";
128 
129  filename = TERRALIB_DATA_DIR "/shape/v2v_result.shp";
130 
131  srcInfo = "file://" + filename;
132 
133  outSource.reset(te::da::DataSourceFactory::make("OGR", srcInfo).release());
134  outSource->open();
135 
136  if (outSource->dataSetExists("v2v_result"))
137  {
138  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." << fromDsName << std::endl;
139  return false;
140  }
141 
142  v2v->setInput(fromLayer, toLayer);
143  v2v->setParams(options, toLayerProps);
144  v2v->setOutput(outSource, outDsName);
145 
146  return true;
147 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Utility functions for Symbology Enconding module.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
This is a singleton for managing all data source instances available in the system.
static te::dt::Date ds(2010, 01, 01)
A layer with reference to a dataset.
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
Vector to Vector processing.
Utility functions for the Geometry Module.
bool VectorToVector()
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
A factory for data sources.
Geometric property.
Utility functions for the data access module.
te::map::DataSetLayerPtr createLayer(te::da::DataSourcePtr source, te::da::DataSetTypePtr &dataset)
boost::intrusive_ptr< DataSetLayer > DataSetLayerPtr
Definition: DataSetLayer.h:148
Vector To Vector operation.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr