Raster1Bit.cpp
Go to the documentation of this file.
1 #include "RasterExamples.h"
2 
3 // TerraLib
4 #include <terralib/gdal/Utils.h>
5 #include <terralib/geometry.h>
6 #include <terralib/raster.h>
7 
8 // STL
9 #include <iostream>
10 #include <string>
11 
12 void Raster1Bit()
13 {
14  try
15  {
16  std::cout << "This example shows how to create an 1bit Raster in memory, and how to save an 1bit Raster in Tiff format." << std::endl << std::endl;
17 
18 // 1. create empty 1bit image in memory
19  // define grid
20  te::rst::Grid* grid = new te::rst::Grid(32, 32);
21 
22  // define band properties
23  std::vector<te::rst::BandProperty*> bprops;
24  bprops.push_back(new te::rst::BandProperty(0, te::dt::R1BIT_TYPE));
25 
26  // define tiles information (in this case, only one block used)
27  bprops[0]->m_blkh = grid->getNumberOfRows();
28  bprops[0]->m_blkw = grid->getNumberOfColumns();
29  bprops[0]->m_nblocksx = 1;
30  bprops[0]->m_nblocksy = 1;
31 
32  te::rst::Raster* mem1bitraster = te::rst::RasterFactory::make("MEM", grid, bprops, std::map<std::string, std::string>());
33 
34  // fill raster (only 0's and 1's are allowed)
35  for (unsigned r = 0; r < mem1bitraster->getNumberOfRows(); r++)
36  for (unsigned c = 0; c < mem1bitraster->getNumberOfColumns(); c++)
37  {
38  if ((c > 5 && c < 25) && (r > 10 && r < 20))
39  mem1bitraster->setValue(c, r, 1);
40  else
41  mem1bitraster->setValue(c, r, 0);
42  }
43 
44  // clean up
45  delete mem1bitraster;
46 
47 // 2. create tiff with 1bit using gdal, need to set NBIT=1
48  std::map<std::string, std::string> rinfo;
49  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/creating-1bit-raster.tif";
50  rinfo["NBITS"] = "1";
51 
52  bprops.clear();
53  bprops.push_back(new te::rst::BandProperty(0, te::dt::UCHAR_TYPE, "1bit band 0"));
54 
55  te::rst::Grid* tif1bitrastergrid = new te::rst::Grid(32, 32);
56 
57  te::rst::Raster* tif1bitraster = te::rst::RasterFactory::make("GDAL", tif1bitrastergrid, bprops, rinfo);
58 
59  // fill raster (only 0's and 1's are allowed)
60  for (unsigned r = 0; r < tif1bitraster->getNumberOfRows(); r++)
61  for (unsigned c = 0; c < tif1bitraster->getNumberOfColumns(); c++)
62  {
63  if ((c > 5 && c < 25) && (r > 10 && r < 20))
64  tif1bitraster->setValue(c, r, 1);
65  else
66  tif1bitraster->setValue(c, r, 0);
67  }
68 
69  // clean up
70  delete tif1bitraster;
71 
72  std::cout << "Done!" << std::endl << std::endl;
73  }
74  catch(const std::exception& e)
75  {
76  std::cout << std::endl << "An exception has occurred in Raster1Bit(): " << e.what() << std::endl;
77  }
78  catch(...)
79  {
80  std::cout << std::endl << "An unexpected exception has occurred in Raster1Bit()!" << std::endl;
81  }
82 }
unsigned int getNumberOfRows() const
Returns the grid number of rows.
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.
A raster band description.
Definition: BandProperty.h:61
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
void Raster1Bit()
This example shows how to create an 1bit Raster in memory, and how to save an 1bit Raster in Tiff for...
Definition: Raster1Bit.cpp:12
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
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.
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
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.