GribPolygonExample.cpp
Go to the documentation of this file.
1 #include "RasterExamples.h"
2 
3 // TerraLib
4 #include <terralib/raster.h>
5 #include <terralib/geometry.h>
6 
7 #if TE_EXAMPLE_USE_GRIB
8 #include <terralib/grib/Raster.h>
9 #include <terralib/grib/Band.h>
10 #endif // TE_EXAMPLE_USE_GRIB
11 
12 // STL
13 #include <iostream>
14 #include <map>
15 #include <memory>
16 
17 te::gm::Polygon* createPolygon(const double& llx, const double& lly, const double& urx, const double& ury)
18 {
20 
22 
23  s->setPoint(0, llx, lly); // lower left
24  s->setPoint(1, llx, ury); // upper left
25  s->setPoint(2, urx, ury); // upper rigth
26  s->setPoint(3, urx, lly); // lower rigth
27  s->setPoint(4, llx + (urx - llx) / 2.0 , lly + (ury - lly) / 2.0); // lower rigth
28  s->setPoint(5, llx, lly); // closing
29 
30  p->push_back(s);
31 
32  return p;
33 }
34 
35 // pensar na possibilidade de poder clipar o raster = o raster de saida nao precisa ser das mesmas dimensoes do de entrada!
36 
37 void Mask(te::rst::Raster* iraster,
38  const te::gm::Polygon* poly,
39  te::rst::Raster* oraster)
40 {
41  double resx = iraster->getResolutionX();
42  double resy = iraster->getResolutionY();
43 
44  const te::rst::Grid* grid = iraster->getGrid();
45 
46  const te::gm::Envelope* pmbr = poly->getMBR();
47 
48  te::gm::Coord2D cccci = grid->geoToGrid(pmbr->getLowerLeftX(), pmbr->getUpperRightY());
49  te::gm::Coord2D ccccf = grid->geoToGrid(pmbr->getUpperRightX(), pmbr->getLowerLeftY());
50 
51  int ri = (int) cccci.y;
52  int rf = (int) ccccf.y;
53 
54  int ci = (int) cccci.x;
55  int cf = (int) ccccf.x;
56 
57  double areaPixel = resx * resy;
58  double areaInside;
59  double value;
60 
61  for(int r = ri; r <= rf; ++r)
62  {
63  double lly = grid->getExtent()->getUpperRightY() - resy * (r + 1);
64  double ury = lly + resy;
65  for(int c = ci; c <= cf; ++c)
66  {
67  double llx = resx * c + grid->getExtent()->getLowerLeftX();
68 
69  double urx = llx + resx;
70 
71  std::unique_ptr<te::gm::Polygon> pixelPoly(createPolygon(llx, lly, urx, ury));
72 
73  if (pixelPoly->within(poly))
74  areaInside = areaPixel;
75  else
76  {
77  std::unique_ptr<te::gm::Geometry> inter(pixelPoly->intersection(poly));
78 
79  if(inter->getGeomTypeId() == te::gm::PolygonType)
80  {
81  te::gm::Polygon* pinter = static_cast<te::gm::Polygon*>(inter.get());
82 
83  areaInside = pinter->getArea();
84  }
85  else
86  areaInside = 0.0;
87  }
88 
89  value = areaInside / areaPixel;
90 
91  oraster->setValue(c, r, /*255 * */value);
92  }
93  }
94 }
95 
97  const te::gm::Polygon* poly,
98  const std::map<std::string, std::string>& routinfo = std::map<std::string, std::string>(),
99  const std::string& rtype = TE_DEFAULT_RASTER_TYPE)
100 {
101  te::rst::Grid* g = new te::rst::Grid(*iraster->getGrid());
102 
103  std::vector<te::rst::BandProperty*> bp;
104 
105  bp.push_back(new te::rst::BandProperty(0, te::dt::DOUBLE_TYPE));
106 
107  // create output
108  // CRIAR UM RASTER EM QUE TODOS OS BLOCOS SAO ZERO
109  std::unique_ptr<te::rst::Raster> oraster(te::rst::RasterFactory::make(rtype, g, bp, routinfo));
110 
111  for (unsigned r = 0; r < oraster->getNumberOfRows(); ++r)
112  for (unsigned c = 0; c < oraster->getNumberOfColumns(); ++c)
113  oraster->setValue(c, r, 0.0);
114 
115  Mask(iraster, poly, oraster.get());
116 
117  return oraster.release();
118 }
119 
121 {
122  try
123  {
124  std::cout << "This test uses Grib and Geometry." << std::endl << std::endl;
125 
126  // load input raster
127  //std::map<std::string, std::string> rinfo;
128 
129  //rinfo["URI"] = ""TERRALIB_DATA_DIR"/geotiff/Z500.grb";
130 
131  //te::rst::Raster* graster = te::rst::RasterFactory::open("GRIB", rinfo);
132 
133  std::unique_ptr<te::rst::Raster> iraster(te::rst::RasterFactory::open("URI", TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif"));
134 
135  // create rectangle
136  const te::gm::Envelope* mbr = iraster->getExtent();
137 
138  //te::gm::Coord2D center = mbr->getCenter();
139 
140  //ouble w = mbr->getWidth();
141  //double h = mbr->getHeight();
142 
143  std::unique_ptr<te::gm::Polygon> poly(createPolygon(mbr->getLowerLeftX() + 500.0, mbr->getLowerLeftY() + 500.0, mbr->getUpperRightX() - 500.0, mbr->getUpperRightY() - 500.0));
144 
145  // describe output
146  std::map<std::string, std::string> routinfo;
147 
148  routinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/Z500_out.tif";
149 
150  std::unique_ptr<te::rst::Raster> newRaster(Mask(iraster.get(), poly.get(), routinfo));
151 
152  std::cout << "Done!" << std::endl << std::endl;
153  }
154  catch(const std::exception& e)
155  {
156  std::cout << std::endl << "An exception has occurred in GribPolygonExample(): " << e.what() << std::endl;
157  }
158  catch(...)
159  {
160  std::cout << std::endl << "An unexpected exception has occurred in GribPolygonExample()!" << std::endl;
161  }
162 
163 }
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
void push_back(Curve *ring)
It adds the curve to the curve polygon.
void GribPolygonExample()
An example to use Grib and Geometry.
#define TE_DEFAULT_RASTER_TYPE
double y
y-coordinate.
Definition: Coord2D.h:114
A raster band description.
Definition: BandProperty.h:61
double x
x-coordinate.
Definition: Coord2D.h:113
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
te::gm::Polygon * p
Grid * getGrid()
It returns the raster grid.
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
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.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
void Mask(te::rst::Raster *iraster, const te::gm::Polygon *poly, te::rst::Raster *oraster)
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
te::gm::Polygon * createPolygon(const double &llx, const double &lly, const double &urx, const double &ury)
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.