examples/dataaccess/ObjectId.cpp
Go to the documentation of this file.
1 // Examples
2 #include "DataAccessExamples.h"
3 
4 // TerraLib
13 
14 //
15 // STL
16 #include <cassert>
17 #include <iostream>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 std::unique_ptr<te::da::DataSource> GetPostGISConnection();
23 
24 void ObjectId()
25 {
26  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
27  if (!ds.get())
28  return;
29 
30  ds->open();
31 
32  // get a transactor to interact to the data source in the next examples
33  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
34  assert(transactor.get());
35  // now we will retrieve all cities that intersects a given box
36  te::gm::Envelope box(-43.9329795837402, -20.6328010559082, -43.4036407470703, -20.1612071990967);
37 
38  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("public.br_munic_2001", "geom", &box, te::gm::INTERSECTS);
39 
40  assert(dataset.get());
41  std::cout << "DataSet size: " << dataset->size() << std::endl;
42 
43 // letīs generate the oids using the primary key
44  std::vector<std::string> pk_name;
45  pk_name.push_back("gid");
46  te::da::ObjectIdSet* oids = te::da::GenerateOIDSet(dataset.get(), pk_name);
47 
48  assert(oids);
49 
50 // let's retrieve the identified elements via transactor
51  std::unique_ptr<te::da::DataSet> identified = transactor->getDataSet("public.br_munic_2001",oids);
52  assert(identified.get());
53  assert(dataset->size() == identified->size());
54  identified->moveBeforeFirst();
55  dataset->moveBeforeFirst(); //otherwise if will be at the end of the dataset and nothing will be printed
56 
57  std::cout << "== DataSet Retrieved From Box == " << std::endl;
58  PrintDataSet("public.br_munic_2001", dataset.get());
59 
60  std::cout << "== DataSet Retrieved From ObjectIdSet knowing pk == " << std::endl;
61  PrintDataSet("munic_2001_identified",identified.get());
62 
63 // Another way to get oids using DataSetType...tries to use the pk, uk or all properties (less geom) if pk and uk do not exist.
64  dataset->moveBeforeFirst();
65  std::unique_ptr<te::da::DataSetType> dt1 = transactor->getDataSetType("public.br_munic_2001");
66  te::da::ObjectIdSet* oids1 = te::da::GenerateOIDSet( dataset.get(), dt1.get());
67  std::unique_ptr<te::da::DataSet> identified1 = transactor->getDataSet("public.br_munic_2001",oids1);
68 
69  std::cout << "== DataSet Retrieved From ObjectIdSet using dataSetType to discover pk == " << std::endl;
70  PrintDataSet("munic_2001_identified",identified1.get());
71 
72  // Cleaning All
73  delete oids1;
74 }
75 
77 {
78  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
79  if (!ds.get())
80  return;
81 
82  // get a transactor to interact to the data source in the next examples
83  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
84  assert(transactor.get());
85 
86 
87 // now we will retrieve all cities that intersects a given box (dataset with 16 elements) and use it in other methods
88  te::gm::Envelope box(-43.9329795837402, -20.6328010559082, -43.4036407470703, -20.1612071990967);
89 
90  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("br_munic_2001_wout_pk", "geom", &box, te::gm::INTERSECTS);
91  //std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet("br_munic_2001_wout_pk");
92 
93  assert(dataset.get());
94  std::cout << "DataSet size: " << dataset->size() << std::endl;
95 
96 // Building Select from oids and property names, and using transactor->query(buildselect.get());
97  /*bool ini2 = */dataset->moveBeforeFirst();
98  std::unique_ptr<te::da::DataSetType> dt1 = transactor->getDataSetType("br_munic_2001_wout_pk");
99 
100  std::vector<std::string> pnames;
101  te::da::GetOIDPropertyNames(dt1.get(),pnames); //It will return pk or uk or all non geom properties
102 
103  te::da::ObjectIdSet* oids = te::da::GenerateOIDSet( dataset.get(), pnames);
104 
105 // Building te::da::Select expression from oids and property names
106  /*bool ini1 =*/ dataset->moveBeforeFirst();
107  std::vector<std::string> pnames1;
108  std::vector<int> ptypes1;
109  te::da::GetPropertyInfo(dataset.get(), pnames1,ptypes1);
110 
111  std::unique_ptr<te::da::Select> buildselect = te::da::BuildSelect("br_munic_2001_wout_pk", pnames,oids);
112  std::unique_ptr<te::da::Select> buildselect1 = te::da::BuildSelect("br_munic_2001_wout_pk", pnames1,oids);
113 
114 // let's retrieve the elements using a query with the te::da::Select expression based on oids
115  std::unique_ptr<te::da::DataSet> identifiedSelect = transactor->query(buildselect.get());
116  std::cout << "== DataSet Retrieved From buildselect == " << std::endl;
117  std::cout << "DataSet size: " << identifiedSelect->size() << std::endl;
118 
119  PrintDataSet("br_munic_2001_wout_pk_Select", identifiedSelect.get());
120 
121 // let's retrieve the elements using a query with the te::da::Select expression based on oids
122  std::unique_ptr<te::da::DataSet> identifiedSelect1 = transactor->query(buildselect1.get());
123  std::cout << "== DataSet Retrieved From buildselect including geom == " << std::endl;
124  std::cout << "DataSet size: " << identifiedSelect1->size() << std::endl;
125 
126  PrintDataSet("br_munic_2001_wout_pk_Select1", identifiedSelect1.get());
127 
128 // let's get the Select from the generated OIDS //?? get expression
129  te::da::Expression* select = oids->getExpression();
130  assert(select);
131 
132 // let's print the SQL
133  std::string sql;
134  te::da::SQLVisitor sqlConverter(*ds->getDialect(), sql);
135  select->accept(sqlConverter);
136 
137  std::cout << "The SQL is: " << sql << std::endl;
138 
139 // let's retrieve the identified elements
140  std::string sqlplus = "select * from public.br_munic_2001_wout_pk where ";
141  sqlplus+= sql;
142  std::unique_ptr<te::da::DataSet> identified = transactor->query(sqlplus);
143 
144  assert(identified.get());
145  std::cout << "== DataSet size= " << dataset->size() << " == " << std::endl;
146  std::cout << "== DataSet Retrieved From ObjectIdSet = 14 identified " << identified->size() <<" == " << std::endl;
147 
148  //assert(dataset->size() == identified->size()); //16 == 14??
149 
150  std::cout << "== DataSet with out pk using Expression == " << std::endl;
151 
152  PrintDataSet("br_munic_2001_wout_pk_identified", identified.get());
153 
154  // Cleaning All!
155  delete select;
156  delete oids;
157 }
It describes a primary key (pk) constraint.
void ObjectId()
ObjectId example.
TEDATAACCESSEXPORT std::unique_ptr< Select > BuildSelect(const std::string &dsname)
static te::dt::Date ds(2010, 01, 01)
This is an abstract class that models a query expression.
void ObjectId_query()
It describes an index associated to a DataSetType.
An Envelope defines a 2D rectangular region.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
Examples on how to access/manipulate DataSources in TerraLib.
A factory for data sources.
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
A class that describes a check constraint.
A visitor for building an SQL statement from a given Query hierarchy.
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
This is the factory for PostGIS data sources.
A class that implements a prepared query for PostgreSQL data access driver.
Implementation of the data source for the PostGIS driver.
void PrintDataSet(std::string datasetName, te::da::DataSet *dataset)
It prints the data in a given dataset.
Definition: PrintDataSet.cpp:7
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)