examples/dataaccess/DataSetAdapter.cpp
Go to the documentation of this file.
1 // Examples
2 #include "DataAccessExamples.h"
3 
4 // TerraLib
5 #include "../Config.h"
6 #include <terralib/common.h>
7 #include <terralib/dataaccess.h>
8 #include <terralib/datatype.h>
9 #include <terralib/geometry.h>
10 
11 // STL
12 #include <cassert>
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 
17 te::dt::Property* CreateStringProperty(const std::string& name)
18 {
19  return new te::dt::StringProperty(name, te::dt::STRING);
20 }
21 
22 te::dt::Property* CreateSimpleProperty(const std::string& name, int type)
23 {
24  return new te::dt::SimpleProperty(name, type);
25 }
26 
27 te::dt::Property* CreateGeometryProperty(const std::string& name)
28 {
29  return new te::gm::GeometryProperty(name);
30 }
31 
32 te::dt::AbstractData* TupleToStringUpperCaseConverter(te::da::DataSet* dataset, const std::vector<int>& indexes, int /*dstType*/)
33 {
34  assert(dataset);
35 
36  std::string result;
37 
38  std::vector<int>::const_iterator it;
39  for(it = indexes.begin(); it != indexes.end(); ++it)
40  result += te::common::Convert2UCase(dataset->getAsString(*it)) + ";";
41 
42  result.erase(--result.end());
43 
44  return new te::dt::String(result);
45 }
46 
48 {
49  /* Accessing a shapefile */
50  std::string connInfo("file://");
51  std::string data_dir = TERRALIB_DATA_DIR;
52  connInfo += data_dir + "/shape/munic_2001.shp";
53 
54  std::unique_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR", connInfo);
55  dsOGR->open();
56 
57  std::unique_ptr<te::da::DataSourceTransactor> ogrTransactor = dsOGR->getTransactor();
58 
59  std::unique_ptr<te::da::DataSet> ogrDataSet = dsOGR->getDataSet("munic_2001");
60  std::unique_ptr<te::da::DataSetType> dtype = dsOGR->getDataSetType("munic_2001");
61 
62  /* PostGIS database to gets the DataSource Capabilities */
63  //std::unique_ptr<te::da::DataSource> dsPGIS = te::da::DataSourceFactory::make("POSTGIS");
64 
65  //// Verifies if ogr data set -> pgis data source need an adapter
66  //bool needAdapter = te::da::DataSetAdapter::needAdapter(ogrDataSet, dsPGIS->getCapabilities());
67  //std::cout << "Need adapter? ";
68  //needAdapter ? std::cout << "Yes" << std::endl : std::cout << "No" << std::endl; // Here, result is "No".
69 
70  //// No need! But, let's create an adapter
71 
72  //// Creates the DataSet Adapter
73  //te::da::DataSetAdapter* adaptedDataSet = te::da::DataSetAdapter::add(ogrDataSet);
74  ////te::da::DataSetAdapter* adaptedDataSet = te::da::DataSetAdapter::adapt(ogrDataSet, pgisDataSource->getCapabilities()); // Here, try automatically create an adapter using DataSourceCapabilities
75 
76  //// Prints the non adapted properties
77  //std::vector<std::string> nonAdaptedProperties;
78  //adaptedDataSet->getNonAdaptedProperties(nonAdaptedProperties);
79  //std::cout << ":: No Adapted Properties ::" << std::endl;
80  //for(std::size_t i = 0; i < nonAdaptedProperties.size(); ++i)
81  // std::cout << "Property: " << nonAdaptedProperties[i] << std::endl;
82 
83  ///* Adapting... */
84 
85  //// Adapts 3 attributes to String using a function that generates a pre-defined pattern. e.g. "[ 2148 Seritinga 178 ]"
86  //std::vector<std::string> idAttributes;
87  //idAttributes.push_back("GEOCODIGO");
88  //idAttributes.push_back("NOME");
89  //idAttributes.push_back("AREA_TOT_G");
90  //adaptedDataSet->adapt(idAttributes, CreateStringProperty("GEOCODIGO_NOME_AREA_ADAPTED_TO_STRING"), te::da::TupleToStringConverter);
91 
92  //// Adapts the same 3 attributes to String using other function defined in this example! "TupleToStringUpperCaseConverter"
93  //// The function generates a pre-defined pattern (using ';' as delimiter) and the attribute values are converted to String Upper Case! e.g. "2148;SERITINGA;178"
94  //adaptedDataSet->adapt(idAttributes, CreateStringProperty("GEOCODIGO_NOME_AREA_ADAPTED_TO_STRING_2"), TupleToStringUpperCaseConverter);
95 
96  //// Adapts lat/lon to Geometry Point
97  //std::vector<std::string> latLonAttributes;
98  //latLonAttributes.push_back("LONGITUDES");
99  //latLonAttributes.push_back("LATITUDESE");
100  //adaptedDataSet->adapt(latLonAttributes, CreateGeometryProperty("LAT_LONG_ADAPTED_TO_POINT"), te::da::XYToPointConverter);
101 
102  //// Adapts the "geom" property to String
103  //adaptedDataSet->adapt("geom", CreateStringProperty("GEOM_ADAPTED_TO_STRING"));
104 
105  //// Prints the adapted data set
106  //PrintDataSet(adaptedDataSet);
107 
108  //// Here, we will adapt the previous adapted data set again!
109  //te::da::DataSetAdapter* adaptedDataSetAgain = te::da::DataSetAdapter::adapt(adaptedDataSet);
110 
111  //// Adapts the LAT_LONG_ADAPTED_TO_POINT property to 2 properties called "X" and "Y". Note the use of attribute converter functions: PointToXConverter and PointToYConverter.
112  //std::vector<std::string> geomAttribute;
113  //adaptedDataSetAgain->adapt("LAT_LONG_ADAPTED_TO_POINT", CreateSimpleProperty("X", te::dt::DOUBLE_TYPE), te::da::PointToXConverter);
114  //adaptedDataSetAgain->adapt("LAT_LONG_ADAPTED_TO_POINT", CreateSimpleProperty("Y", te::dt::DOUBLE_TYPE), te::da::PointToYConverter);
115 
116  //// Prints the adapted data set
117  //PrintDataSet(adaptedDataSetAgain);
118 
119  //// Cleaning all!
120  //delete adaptedDataSetAgain;
121  //delete adaptedDataSet;
122 
123 }
This file contains include headers for the Data Type module of TerraLib.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
Geometric property.
An atomic property like an integer or double.
te::dt::AbstractData * TupleToStringUpperCaseConverter(te::da::DataSet *dataset, const std::vector< int > &indexes, int)
te::dt::Property * CreateSimpleProperty(const std::string &name, int type)
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
It models a property definition.
Definition: Property.h:59
Examples on how to access/manipulate DataSources in TerraLib.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
The type for string types: FIXED_STRING, VAR_STRING or STRING.
A dataset is the unit of information manipulated by the data access module of TerraLib.
void DataSetAdapter()
DataSet Adapter example.
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Vector Geometry model of TerraLib.
This file contains include headers for the Data Access module of TerraLib.
te::dt::Property * CreateStringProperty(const std::string &name)
te::dt::Property * CreateGeometryProperty(const std::string &name)