ExportingOGR.cpp
Go to the documentation of this file.
1 // Examples
2 #include "../Config.h"
3 #include "DataAccessExamples.h"
4 
5 // Terralib
6 #include <terralib/common.h>
7 #include <terralib/dataaccess.h>
8 #include <terralib/datatype.h>
9 #include <terralib/geometry.h>
10 #include <terralib/memory.h>
11 #include <terralib/raster.h>
12 #include <terralib/srs.h>
13 
16 
17 #include <iostream>
18 #include <memory>
19 #include <vector>
20 #include <string>
21 
22 #include <boost/foreach.hpp>
23 
24 // utilitary funcion create a dataset in memory
25 te::da::DataSet* create_ds_memory(const std::string& datasetName, te::da::DataSetType* datasettype);
26 
27 void openFile(const std::string& filename, const std::string dstype);
28 void openDirectory(const std::string& filename, const std::string dstype);
29 void saveUsingOGR(const std::string& filename, const std::string drivername);
30 
32 {
33  std::string data_dir = TERRALIB_DATA_DIR;
34  std::string aux;
35  std::cout << "Inform a directory to write the data generated\n(ENTER to access the default \'" << TERRALIB_DATA_DIR << "/shape\'): ";
36  std::getline (std::cin, aux);
37  if (!aux.empty())
38  data_dir = aux;
39 
40  saveUsingOGR(data_dir + "/shape/testeOGR.shp", "ESRI Shapefile");
41  saveUsingOGR(data_dir + "/shape/testeOGR.kml", "KML");
42  saveUsingOGR(data_dir + "/shape/testeOGR.mif", "MapInfo File");
43  saveUsingOGR(data_dir + "/shape/testeOGR.geojson", "GeoJSON");
44  saveUsingOGR(data_dir + "/shape/testeOGR.gml", "GML");
45 
46  std::cout << std::endl << "Checking the directory: \n" << data_dir;
47  openDirectory(data_dir, "OGR");
48 }
49 
50 void openFile(const std::string& filename, const std::string dstype)
51 {
52  std::string connInfo("file://");
53  connInfo += filename;
54 
55  std::unique_ptr<te::da::DataSource> dsptr = te::da::DataSourceFactory::make(dstype, connInfo);
56  dsptr->open();
57 
58  std::vector<std::string> dsNames = dsptr->getDataSetNames();
59 
60  std::cout << std::endl << "Datasets in " << filename << ":" << std::endl;
61 
62  BOOST_FOREACH(std::string s, dsNames)
63  {
64  std::cout << "\t" << "\t" << s << std::endl;
65  }
66  dsNames.clear();
67  dsptr->close();
68 }
69 
70 void openDirectory(const std::string& filename, const std::string dstype)
71 {
72  std::string connInfo("file://");
73  connInfo += filename;
74 
75  std::unique_ptr<te::da::DataSource> dsptr = te::da::DataSourceFactory::make(dstype, connInfo);
76  dsptr->open();
77 
78  std::vector<std::string> dsNames = dsptr->getDataSetNames();
79 
80  std::cout << std::endl << "Datasets in " << filename << ":" << std::endl;
81 
82  BOOST_FOREACH(std::string s, dsNames)
83  {
84  std::cout << "\t" << "\t" << s << std::endl;
85  }
86  dsNames.clear();
87  dsptr->close();
88 }
89 
90 void saveUsingOGR(const std::string& filename, const std::string drivername)
91 {
92  std::unique_ptr<te::da::DataSetType> dt(new te::da::DataSetType("testeOGR"));
93 
95  prop01->setAutoNumber(true);
96 
98 
99  te::gm::GeometryProperty* prop03 = new te::gm::GeometryProperty("location", 0, te::gm::PointType, true);
100  prop03->setSRID(4618);
101 
102  te::dt::DateTimeProperty* prop04 = new te::dt::DateTimeProperty( "date", te::dt::DATE, true);
103 
104  te::dt::NumericProperty* prop05 = new te::dt::NumericProperty("value", 7, 4, true);
105 
106  dt->add(prop01);
107  dt->add(prop02);
108  dt->add(prop03);
109  dt->add(prop04);
110  dt->add(prop05);
111 
112  std::unique_ptr<te::da::DataSet> dataset(create_ds_memory("teste10", dt.get()));
113 
114  std::string connInfo("file://");
115  connInfo += filename;
116  //connInfo += ("?&DRIVER=" + drivername);
117 
118  std::unique_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR", connInfo);
119  dsOGR->open();
120 
121  dataset->moveBeforeFirst();
122 
123  te::da::Create(dsOGR.get(), dt.get(), dataset.get());
124 
125  dsOGR->close();
126  dsOGR.release();
127  connInfo.clear();
128 }
129 
130 te::da::DataSet* create_ds_memory(const std::string& datasetName, te::da::DataSetType* datasettype)
131 {
132  te::mem::DataSet* ds = new te::mem::DataSet(datasettype);
133 
134  te::gm::Point* auxPoint1 = new te::gm::Point(23.5, 78.6, 4326);
135  te::mem::DataSetItem* dsItem01 = new te::mem::DataSetItem(ds);
136  dsItem01->setInt32(0, 1); //id
137  dsItem01->setInt32(1, 233); //sensor_id
138  dsItem01->setGeometry(2, auxPoint1); //location
139 
140  boost::gregorian::date d1(boost::gregorian::greg_year(2010),boost::gregorian::greg_month(1),15);
141  te::dt::DateTime* datetime1 = new te::dt::Date(d1);
142 
143  dsItem01->setDateTime(3, datetime1); //measure_date
144  dsItem01->setNumeric(4, "65.89"); //measure_value
145 
146  te::gm::Point* auxPoint2 = new te::gm::Point(25.7, 80.5, 4326);
147  te::mem::DataSetItem* dsItem02 = new te::mem::DataSetItem(ds);
148  dsItem02->setInt32(0, 2); //id
149  dsItem02->setInt32(1, 245); //sensor_id
150  dsItem02->setGeometry(2, auxPoint2); //location
151  dsItem02->setDateTime(3, static_cast<te::dt::DateTime*>(datetime1->clone())); //measure_date
152  dsItem02->setNumeric(4, "80.90"); //measure_value
153 
154  te::gm::Point* auxPoint3 = new te::gm::Point(24.6, 83.5, 4326);
155  te::mem::DataSetItem* dsItem03 = new te::mem::DataSetItem(ds);
156  dsItem03->setInt32(0, 3); //id
157  dsItem03->setInt32(1, 255); //sensor_id
158  dsItem03->setGeometry(2, auxPoint3); //location
159  dsItem03->setDateTime(3, static_cast<te::dt::DateTime*>(datetime1->clone())); //measure_date
160  dsItem03->setNumeric(4, "87.90"); //measure_value
161 
162  boost::gregorian::date d2(boost::gregorian::greg_year(2010),boost::gregorian::greg_month(2),15);
163  te::dt::DateTime* datetime2 = new te::dt::Date(d2);
164 
165  te::gm::Point* auxPoint4 = new te::gm::Point(23.5, 78.6, 4326);
166  te::mem::DataSetItem* dsItem04 = new te::mem::DataSetItem(ds);
167  dsItem04->setInt32(0, 4); //id
168  dsItem04->setInt32(1, 233); //sensor_id
169  dsItem04->setGeometry(2, auxPoint4); //location
170  dsItem04->setDateTime(3, datetime2); //measure_date
171  dsItem04->setNumeric(4, "90.89"); //measure_value
172 
173  te::gm::Point* auxPoint5 = new te::gm::Point(25.7, 80.5, 4326);
174  te::mem::DataSetItem* dsItem05 = new te::mem::DataSetItem(ds);
175  dsItem05->setInt32(0, 5); //id
176  dsItem05->setInt32(1, 245); //sensor_id
177  dsItem05->setGeometry(2, auxPoint5); //location
178  dsItem05->setDateTime(3, static_cast<te::dt::DateTime*>(datetime2->clone())); //measure_date
179  dsItem05->setNumeric(4, "73.90"); //measure_value
180 
181  te::gm::Point* auxPoint6 = new te::gm::Point(24.6, 83.5, 4326);
182  te::mem::DataSetItem* dsItem06 = new te::mem::DataSetItem(ds);
183  dsItem06->setInt32(0, 6); //id
184  dsItem06->setInt32(1, 255); //sensor_id
185  dsItem06->setGeometry(2, auxPoint6); //location
186  dsItem06->setDateTime(3, static_cast<te::dt::DateTime*>(datetime2->clone())); //measure_date
187  dsItem06->setNumeric(4, "45.90"); //measure_value
188 
189  boost::gregorian::date d3(boost::gregorian::greg_year(2010),boost::gregorian::greg_month(3),15);
190  te::dt::DateTime* datetime3 = new te::dt::Date(d3);
191 
192  te::gm::Point* auxPoint7 = new te::gm::Point(23.5, 78.6, 4326);
193  te::mem::DataSetItem* dsItem07 = new te::mem::DataSetItem(ds);
194  dsItem07->setInt32(0, 7); //id
195  dsItem07->setInt32(1, 233); //sensor_id
196  dsItem07->setGeometry(2, auxPoint7); //location
197  dsItem07->setDateTime(3, datetime3); //measure_date
198  dsItem07->setNumeric(4, "76.89"); //measure_value
199 
200  te::gm::Point* auxPoint8 = new te::gm::Point(25.7, 80.5, 4326);
201  te::mem::DataSetItem* dsItem08 = new te::mem::DataSetItem(ds);
202  dsItem08->setInt32(0, 8); //id
203  dsItem08->setInt32(1, 245); //sensor_id
204  dsItem08->setGeometry(2, auxPoint8); //location
205  dsItem08->setDateTime(3, static_cast<te::dt::DateTime*>(datetime3->clone())); //measure_date
206  dsItem08->setNumeric(4, "78.15"); //measure_value
207 
208  te::gm::Point* auxPoint9 = new te::gm::Point(24.6, 83.5, 4326);
209  te::mem::DataSetItem* dsItem09 = new te::mem::DataSetItem(ds);
210  dsItem09->setInt32(0, 9); //id
211  dsItem09->setInt32(1, 255); //sensor_id
212  dsItem09->setGeometry(2, auxPoint9); //location
213  dsItem09->setDateTime(3, static_cast<te::dt::DateTime*>(datetime3->clone())); //measure_date
214  dsItem09->setNumeric(4, "50.32"); //measure_value
215 
216  //acrescentar esse metodo no data set em memoria!!!!
217  ds->add(dsItem01);
218  ds->add(dsItem02);
219  ds->add(dsItem03);
220  ds->add(dsItem04);
221  ds->add(dsItem05);
222  ds->add(dsItem06);
223  ds->add(dsItem07);
224  ds->add(dsItem08);
225  ds->add(dsItem09);
226 
227  return ds;
228 }
This file contains include headers for the Data Type module of TerraLib.
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
This file contains include headers for the memory data source of TerraLib.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
This file contains include headers for TerraLib Spatial Reference System module.
An atomic property like an integer or double.
te::da::DataSet * create_ds_memory(const std::string &datasetName, te::da::DataSetType *datasettype)
A class that models the description of a dataset.
Definition: DataSetType.h:72
This is a singleton for managing all data source instances available in the system.
static te::dt::Date ds(2010, 01, 01)
void openDirectory(const std::string &filename, const std::string dstype)
void setNumeric(std::size_t i, const std::string &value)
It sets the value of the i-th property.
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
void openFile(const std::string &filename, const std::string dstype)
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
The type for arbitrary precison numbers, like numeric(p, q).
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
A point with x and y coordinate values.
Definition: Point.h:50
A base class for date data types.
Definition: Date.h:53
void setDateTime(std::size_t i, te::dt::DateTime *value)
It sets the value of the i-th property.
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
Examples on how to access/manipulate DataSources in TerraLib.
void DataSet()
static te::dt::TimeDuration dt(20, 30, 50, 11)
A factory for data sources.
void saveUsingOGR(const std::string &filename, const std::string drivername)
virtual AbstractData * clone() const =0
It returns a clone of this object.
void ExportingOGR()
An example using OGR data source driver to export some ;.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
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.