PostGISRaster.cpp
Go to the documentation of this file.
1 #include "RasterExamples.h"
2 
3 // TerraLib
4 #include <terralib/dataaccess.h>
5 #include <terralib/geometry.h>
6 #include <terralib/raster.h>
7 
8 // STL
9 #include <iostream>
10 #include <string>
11 
12 /*
13  This is an example of how to access a raster in the postgis database using the GDAL library.
14  The GDAL PostGIS raster driver is read-only support.
15 
16  For testing, a postgis specific tool was used to import a raster into the database: raster2pgsql
17 
18  The command line to import is:
19 
20  raster2pgsql raster_options_go_here raster_file someschema.sometable > out.sql
21 
22  e.g.: raster2pgsql -s 4236 -I -C -M *.tif -F -t 100x100 public.demelevation > elev.sql
23 
24  As a result an output file is generated with the script to be executed in the database.
25 
26  The command line to execute the script is:
27 
28  psql -d db_name -f out.sql
29 
30  e.g.: psql -d gisdb -f elev.sql
31 
32 Ref: https://trac.osgeo.org/gdal/wiki/frmts_wtkraster.html
33  http://postgis.refractions.net/documentation/manual-svn/using_raster.xml.html#RT_Raster_Loader
34 
35  NOTE: The GDAL library needs to be compiled with postgis driver support.
36 */
37 
39 
42 
44 {
46 
48 }
49 
51 {
52  try
53  {
54  std::cout << "This example shows how to use the GDAL PostGIS Raster support using Raster API." << std::endl << std::endl;
55 
56  // 1 - define raster info and load
57  std::map<std::string, std::string> rinfo;
58  rinfo["URI"] = "PG:host=localhost port=5432 dbname='raster_db' user='postgres' password='****' schema='public' table=cbers mode=2";
59 
61 
62  // 2 - Copy image to file format
63  std::string uriOut = TERRALIB_DATA_DIR "/geotiff/raster_pgis_rasterAPI.tif";
64 
65  te::rst::RasterPtr outraster = te::rst::CreateCopy(*(inraster.get()), uriOut);
66 
67  if (outraster)
68  {
69  std::cout << "End. Check the PostGIS Raster copy to file: raster_pgis_rasterAPI.tif." << std::endl << std::endl;
70  }
71  else
72  {
73  std::cout << "PostGIS Raster copy failed!" << std::endl << std::endl;
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 PostGISRaster(): " << e.what() << std::endl;
81  }
82  catch (...)
83  {
84  std::cout << std::endl << "An unexpected exception has occurred in PostGISRaster()!" << std::endl;
85  }
86 }
87 
89 {
90  try
91  {
92  std::cout << "This example shows how to use the GDAL PostGIS Raster support using DataSource API." << std::endl << std::endl;
93 
94  // 1 - define raster info and load
95  std::map<std::string, std::string> rinfo;
96 
97  std::string uri = "PG://postgres:*****@localhost:5432/raster_db?schema=public&table=cbers&mode=2";
98 
99  rinfo["URI"] = uri;
100 
101  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL", uri);
102  ds->open();
103  ds->isValid();
104 
105  std::vector<std::string> dsNames = ds->getDataSetNames();
106 
107  std::unique_ptr<te::da::DataSet> dataSet = ds->getDataSet(dsNames[0]);
108 
109  std::size_t rpos = te::da::GetFirstPropertyPos(dataSet.get(), te::dt::RASTER_TYPE);
110 
111  std::unique_ptr<te::rst::Raster> inraster = dataSet->getRaster(rpos);
112 
113 
114  // 2 - Copy image to file format
115  std::string uriOut = TERRALIB_DATA_DIR "/geotiff/raster_pgis_dataSourceAPI.tif";
116 
117  te::rst::RasterPtr outraster = te::rst::CreateCopy(*(inraster.get()), uriOut);
118 
119  if (outraster)
120  {
121  std::cout << "End. Check the PostGIS Raster copy to file: raster_pgis_dataSourceAPI.tif." << std::endl << std::endl;
122  }
123  else
124  {
125  std::cout << "PostGIS Raster copy failed!" << std::endl << std::endl;
126  }
127 
128  std::cout << "Done!" << std::endl << std::endl;
129  }
130  catch (const std::exception& e)
131  {
132  std::cout << std::endl << "An exception has occurred in PostGISRaster(): " << e.what() << std::endl;
133  }
134  catch (...)
135  {
136  std::cout << std::endl << "An unexpected exception has occurred in PostGISRaster()!" << std::endl;
137  }
138 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void PostGISRaster_RasterAPI()
TERASTEREXPORT te::rst::RasterPtr CreateCopy(const te::rst::Raster &rin, const std::string &uri, const std::string &rType=std::string("GDAL"))
Create a new raster from existing one.
static te::dt::Date ds(2010, 01, 01)
boost::shared_ptr< Raster > RasterPtr
void PostGISRaster()
This example shows how to use the GDAL PostGIS Raster support.
These routines show how to use the raster module and the GDAL data source module. ...
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 dataset is the unit of information manipulated by the data access module of TerraLib.
This file contains include headers for the Data Access module of TerraLib.
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.
void PostGISRaster_DataSourceAPI()