QueryExample_2.cpp
Go to the documentation of this file.
1 // Examples
2 #include "DataAccessExamples.h"
3 
4 // TerraLib
5 
6 // STL
7 #include <iostream>
8 #include <exception>
9 
10 std::unique_ptr<te::da::DataSource> GetPostGISConnection();
11 
13 {
14  try
15  {
16  std::unique_ptr<te::da::DataSource> ds = GetPostGISConnection();
17  if (!ds.get())
18  return;
19 
20  ds->open();
21 
22 // get a transactor to interact to the data source
23  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
24 
25 // quering a table called public.munic_2001 using query object
26  {
27  te::da::Fields* fields = new te::da::Fields;
28  te::da::PropertyName* pName = new te::da::PropertyName("area_tot_g");
29 
30  te::da::Expression* e_min = new te::da::Min(pName);
31  te::da::Field* f_min = new te::da::Field(*e_min, "MIN");
32 
33  te::da::Expression* e_max = new te::da::Max(pName);
34  te::da::Field* f_max = new te::da::Field(*e_max, "MAX");
35 
36  te::da::Expression* e_count = new te::da::Count(pName);
37  te::da::Field* f_count = new te::da::Field(*e_count, "COUNT");
38 
39  te::da::Expression* e_sum = new te::da::Sum(pName);
40  te::da::Field* f_sum = new te::da::Field(*e_sum, "SUM");
41 
42  te::da::Expression* e_mean = new te::da::Avg(pName);
43  te::da::Field* f_mean = new te::da::Field(*e_mean, "MEAN");
44 
45  te::da::Expression* e_stddev = new te::da::StdDev(pName);
46  te::da::Field* f_stddev = new te::da::Field(*e_stddev, "STD_DEV");
47 
48  te::da::Expression* e_variance = new te::da::Variance(pName);
49  te::da::Field* f_variance = new te::da::Field(*e_variance, "VARIANCE");
50 
51  te::da::Expression* e_amplitude = new te::da::Sub(*e_max, *e_min);
52  te::da::Field* f_amplitude = new te::da::Field(*e_amplitude, "AMPLITUDE");
53 
54  fields->push_back(f_min);
55  fields->push_back(f_max);
56  fields->push_back(f_count);
57  fields->push_back(f_sum);
58  fields->push_back(f_mean);
59  fields->push_back(f_stddev);
60  fields->push_back(f_variance);
61  fields->push_back(f_amplitude);
62 
63  te::da::FromItem* t1 = new te::da::DataSetName("munic_2001", "t1");
64  te::da::From* from = new te::da::From;
65  from->push_back(t1);
66 
67  te::da::Select select(fields, from);
68 
69  std::unique_ptr<te::da::DataSet> dataset = transactor->query(select);
70  dataset->moveBeforeFirst();
71  int i = 0;
72 
73  while(dataset->moveNext())
74  {
75  std::string min = dataset->getAsString(0);
76  std::cout << "Min: " << min <<std::endl;
77 
78  std::string max = dataset->getAsString(1);
79  std::cout << "Max: " << max <<std::endl;
80 
81  std::string count = dataset->getAsString(2);
82  std::cout << "Count: " << count <<std::endl;
83 
84  std::string sum = dataset->getAsString(3);
85  std::cout << "Sum: " << sum <<std::endl;
86 
87  std::string mean = dataset->getAsString(4);
88  std::cout << "Mean: " << mean <<std::endl;
89 
90  std::string stddev = dataset->getAsString(5);
91  std::cout << "Std_dev: " << stddev <<std::endl;
92 
93  std::string variance = dataset->getAsString(6);
94  std::cout << "Variance: " << variance <<std::endl;
95 
96  std::string amplitude = dataset->getAsString(7);
97  std::cout << "Amplitude: " << amplitude <<std::endl;
98  ++i;
99  }
100 
101 // quering a table called public.munic_2001 using native interface and returning all neighbours of 'Ouro Preto'
102  {
103  std::string sql("SELECT * , st_intersects(g1.geom, g2.geom) "
104  "FROM munic_2001 AS g1, munic_2001 AS g2 "
105  "WHERE g1.nome = 'Ouro Preto' "
106  "AND st_intersects(g1.geom, g2.geom) "
107  ) ;
108 
109  std::unique_ptr<te::da::DataSet> dataset = transactor->query(sql);
110  PrintDataSet("All neighbours of Ouro Preto", dataset.get());
111  }
112 
113  bool trans_connected;
114  trans_connected = transactor->isInTransaction();
115  }
116 
117 // At the end, let's release transactor and close data source
118  delete transactor.release();
119  ds->close();
120  }
121  catch(const std::exception& e)
122  {
123  std::cout << std::endl << "An exception has occurred in QueryExample: " << e.what() << std::endl;
124  }
125  catch(...)
126  {
127  std::cout << std::endl << "An unexpected exception has occurred in QueryExample!" << std::endl;
128  }
129 
130 
131 }
132 
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
void QueryExample()
Quering a dataset.
A class that models the name of any property of an object.
TEMNTEXPORT te::gm::Point Max(te::gm::Point &p1, te::gm::Point &p2)
Count statistical function.
Definition: Count.h:46
static te::dt::Date ds(2010, 01, 01)
Avg statistical function.
Definition: Avg.h:46
This is an abstract class that models a query expression.
StdDev statistical function.
Definition: StdDev.h:46
TESAEXPORT double Sum(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate sum of a specific attribute from a gpm.
Examples on how to access/manipulate DataSources in TerraLib.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
std::unique_ptr< te::da::DataSource > GetPostGISConnection()
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
The subtraction operator.
Variance statistical function.
Definition: Variance.h:46
TEMNTEXPORT te::gm::Point Min(te::gm::Point &p1, te::gm::Point &p2)
void PrintDataSet(std::string datasetName, te::da::DataSet *dataset)
It prints the data in a given dataset.
Definition: PrintDataSet.cpp:7