examples/cellspace/main.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file examples/cellspace/main.cpp
22 
23  \brief A examples for the TerraLib Cellular Space Module.
24 */
25 
26 // TerraLib
27 #include "../Config.h"
30 #include <terralib/core/plugin.h>
31 
43 
44 // Examples
45 
46 // STL
47 #include <cassert>
48 #include <cstdlib>
49 #include <exception>
50 #include <iostream>
51 #include <map>
52 
53 // Boost
54 #include <boost/uuid/random_generator.hpp>
55 #include <boost/uuid/uuid_io.hpp>
56 
57 void LoadModules();
58 
59 void CreateCellSpace();
60 void CreateMaskCellSpace();
61 
62 int main(int /*argc*/, char** /*argv*/)
63 {
64  try
65  {
66  // Initialize the Terralib support
68 
69  LoadModules();
70 
71  /*
72  Before running, make sure that the TERRALIB_DATA_DIR variable, of CMAKE, was set correctly with the
73  address of the folder "data" provided by the link: http://www.dpi.inpe.br/terralib5-devel/data.zip
74  */
75 
76  try
77  {
78 
81 
82  }
83  catch (const std::exception& e)
84  {
85  std::cout << "Error: " << e.what() << std::endl;
86  }
87  catch (...)
88  {
89  std::cout << "Unexpected error" << std::endl;
90  }
91 
93 
95  }
96  catch(const std::exception& e)
97  {
98  std::cout << std::endl << "An exception has occurred: " << e.what() << std::endl;
99 
100  return EXIT_FAILURE;
101  }
102  catch(...)
103  {
104  std::cout << std::endl << "An unexpected exception has occurred!" << std::endl;
105 
106  return EXIT_FAILURE;
107  }
108 
109  return EXIT_SUCCESS;
110 }
111 
113 {
114  std::string data_dir = TERRALIB_DATA_DIR;
115  std::string filename(data_dir + "/shape/Intersection/brazil_cells.shp");
116  std::string ogrInfo("file://" + filename);
117 
118  boost::uuids::basic_random_generator<boost::mt19937> gen;
119  boost::uuids::uuid u = gen();
120  std::string id_ds = boost::uuids::to_string(u);
121 
123  ds->setConnInfo(ogrInfo);
124  ds->setAccessDriver("OGR");
125  ds->setId(id_ds);
126 
127  te::gm::Envelope env;
128  env.m_llx = -73.99;
129  env.m_lly = -33.75;
130  env.m_urx = -33.75;
131  env.m_ury = 5.27;
132 
133  int srid = 4618; // LatLong SAD64
134 
136  cp.createCellSpace(ds, "brazil_cells", 0.25, 0.25, env, srid, te::cellspace::CellularSpacesOperations::CELLSPACE_POLYGONS, 0); // without mask
137 }
138 
140 {
141  boost::uuids::basic_random_generator<boost::mt19937> gen;
142  boost::uuids::uuid u = gen();
143 
144  std::string id_source = boost::uuids::to_string(u);
145 
146  std::string data_dir = TERRALIB_DATA_DIR;
147 
148  std::string connInfo("file://");
149  connInfo += data_dir + "/shp/munic_2001.shp";
150 
151  te::da::DataSourcePtr source( te::da::DataSourceFactory::make("OGR", connInfo) );
152  source->open();
153  source->setId(id_source);
154 
156 
157  if (source.get())
158  {
159  std::unique_ptr<te::da::DataSetType> dsType = source->getDataSetType("munic_2001");
160  std::unique_ptr<te::da::DataSet> dataset = source->getDataSet("munic_2001");
161 
162  std::unique_ptr<te::gm::GeometryProperty> gp(te::da::GetFirstGeomProperty(dsType.get()));
163 
164  u = gen();
165  std::string id_layer = boost::uuids::to_string(u);
166 
167  std::unique_ptr<te::gm::Envelope> mbr(te::da::GetExtent(dsType->getName(), gp->getName(), id_source));
168 
169  std::unique_ptr<te::map::DataSetLayer> layer(new te::map::DataSetLayer(id_layer));
170  layer->setDataSetName(dsType->getName());
171  layer->setDataSourceId(id_source);
172  layer->setVisibility(te::map::NOT_VISIBLE);
173  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
174  layer->setSRID(gp->getSRID());
175 
176  if (mbr.get())
177  layer->setExtent(*mbr);
178 
179  std::string data_dir = TERRALIB_DATA_DIR;
180  std::string filename(data_dir + "/shape/Intersection/brazil_cells_mask.shp");
181  std::string ogrInfo("file://" + filename);
182 
183  std::string id_source = boost::uuids::to_string(u);
184 
186  ds->setConnInfo(ogrInfo);
187  ds->setAccessDriver("OGR");
188  ds->setId(id_source);
189 
191  cp.createCellSpace(ds, "brazil_cells_mask", 1, 1, *mbr, gp->getSRID(), te::cellspace::CellularSpacesOperations::CELLSPACE_POLYGONS, layer.get()); // without mask
192  }
193 }
This class provide cellular spaces operations.
void CreateCellSpace()
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
An utility class to control the startup and cleanup of the TerraLib Platform and its resources...
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Include files for Core Plugin Library.
It describes a primary key (pk) constraint.
void LoadModules()
Load terralib modules.
boost::shared_ptr< DataSource > DataSourcePtr
This is a singleton for managing all data source instances available in the system.
double m_urx
Upper right corner x-coordinate.
static te::dt::Date ds(2010, 01, 01)
A layer with reference to a dataset.
A singleton to keep all the registered data sources.
It describes an index associated to a DataSetType.
int main(int, char **)
static PluginManager & instance()
Access the singleton.
double m_llx
Lower left corner x-coordinate.
An Envelope defines a 2D rectangular region.
void finalize()
It finalizes the TerraLib Platform.
static TerraLib & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
void CreateMaskCellSpace()
A factory for data sources.
Geometric property.
Utility functions for the data access module.
void initialize()
It initializes the TerraLib Platform.
double m_lly
Lower left corner y-coordinate.
A conteiner class for keeping information about a data source.
A class that describes a check constraint.
double m_ury
Upper right corner y-coordinate.
A class that represents a data source component.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void clear()
Stop and unload all plugins, then clear the internal list of plugins.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
void createCellSpace(te::da::DataSourceInfoPtr outputSource, const std::string &name, const double &resX, const double &resY, const te::gm::Envelope &env, const int srid, CellSpaceType type, te::map::AbstractLayerPtr layerBase)
It creates a Cellular Space.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr