RasterizePolygonSet.cpp
Go to the documentation of this file.
1 #include "RasterExamples.h"
2 
3 // TerraLib
4 #include <terralib/gdal/Utils.h>
5 #include <terralib/dataaccess.h>
7 #include <terralib/geometry.h>
8 #include <terralib/raster.h>
9 
10 // STL
11 #include <iostream>
12 #include <string>
13 
15 {
16  try
17  {
18  std::cout << "This is a test to open a shapefile and save some polygons in a raster file." << std::endl << std::endl;
19 
20  std::string ogrInfo("file://");
21  ogrInfo += TERRALIB_DATA_DIR"/shape/munic_2001.shp";
22  std::unique_ptr<te::da::DataSource> ds_pols = te::da::DataSourceFactory::make("OGR", ogrInfo);
23  ds_pols->open();
24 
25 // get a transactor to interact to the data source
26  std::unique_ptr<te::da::DataSourceTransactor> transactor = ds_pols->getTransactor();
27  std::vector<std::string> datasets = transactor->getDataSetNames();
28 
29  for(unsigned int i=0; i<datasets.size(); ++i)
30  {
31 // retrieve the dataset by its name
32  std::unique_ptr<te::da::DataSet> dataset = transactor->getDataSet(datasets[i]);
33 
34  while(dataset->moveNext())
35  {
36 // select arbitrary places using attribute "nome" from database
37  if((dataset->getString("nome")=="Curitiba")||
38  (dataset->getString("nome")=="Porto Alegre"))
39  {
40  std::cout << " Drawing city " << dataset->getString("nome") << std::endl;
41 
42 // retrieve polygon's geometry
43  std::size_t geomPos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
44  std::unique_ptr<te::gm::Geometry> geometry = dataset->getGeometry(geomPos);
45  te::gm::Coord2D ll = geometry->getMBR()->getLowerLeft();
46  te::gm::Coord2D ur = geometry->getMBR()->getUpperRight();
47  te::gm::Envelope* envelope = new te::gm::Envelope(ll.x, ll.y, ur.x, ur.y);
48 
49 // define raster properties
50  int srid = 29183;
51  double factor = 0.0005;
52  double zerox = envelope->getLowerLeftX();
53  double geot[6] = {zerox, factor, 0.0, ur.y, 0.0, -factor};
54 
55 // describes the raster that you want
56  std::string rname = dataset->getString("nome")+".tif";
57  te::rst::Grid* grid = new te::rst::Grid((unsigned)(envelope->getWidth()/factor + 1),
58  (unsigned)(envelope->getHeight()/factor + 1));
59  grid->setGeoreference(geot, srid);
60  std::vector<te::rst::BandProperty*> bprops;
61  bprops.push_back(new te::rst::BandProperty(0, te::dt::UCHAR_TYPE));
62 
63 // create raster and rasterize with the selected polygon
64  std::map<std::string, std::string> orinfo;
65  orinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/" + rname;
66  te::rst::Raster* rout = te::rst::RasterFactory::make(grid, bprops, orinfo);
67  std::vector<te::gm::Geometry*> vgeometry;
68  vgeometry.push_back(geometry.get());
69  std::vector<double> colors;
70  colors.push_back(127.0);
71  rout->rasterize(vgeometry, colors, 0);
72  delete rout;
73  }
74  }
75  }
76  std::cout << "Done!" << std::endl << std::endl;
77  }
78  catch(const std::exception& e)
79  {
80  std::cout << std::endl << "An exception has occurred in RasterizePolygonSet(): " << e.what() << std::endl;
81  }
82  catch(...)
83  {
84  std::cout << std::endl << "An unexpected exception has occurred in RasterizePolygonSet()!" << std::endl;
85  }
86 }
void RasterizePolygonSet()
Test to open a shapefile and save some polygons in a raster file.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
double y
y-coordinate.
Definition: Coord2D.h:114
A raster band description.
Definition: BandProperty.h:61
double x
x-coordinate.
Definition: Coord2D.h:113
double getWidth() const
It returns the envelope width.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void setGeoreference(const te::gm::Coord2D &ulLocation, int srid, double resX, double resY)
Sets the information needed to georeference the grid.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
virtual void rasterize(std::vector< te::gm::Geometry * > g, std::vector< double > vp, std::size_t b=0)
Rasterizes a given vector of geometries.
A factory for data sources.
These routines show how to use the raster module and the GDAL data source module. ...
static Raster * make()
It creates and returns an empty raster with default raster driver.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
This file contains include headers for the Vector Geometry model of TerraLib.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
This file contains include headers for the Data Access module of TerraLib.
double getHeight() const
It returns the envelope height.
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.