importGridData.cpp

Shows how a raster data: a binary raw grid data

00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright © 2001-2004 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 
00024 /* 
00025         This file shows an example of how to import, to a TerraLib database, a raster data: 
00026         a binary raw grid data. Each point of the grid is a float value that represents the 
00027         elevation over an area 
00028 
00029         Author: Lubia Vinhas  
00030 */
00031 
00032 #include "../../src/terralib/kernel/TeDatabase.h"
00033 #include "../../src/terralib/drivers/MySQL/TeMySQL.h"
00034 #include "../../src/terralib/kernel/TeDecoderMemoryMap.h"
00035 #include "../../src/terralib/kernel/TeImportRaster.h"
00036 
00037 int main()
00038 {
00039         // Initialize the raster decoder tool
00040         static TeDecoderMemoryMapFactory theDecoderMemoryMapFactory("MEMMAP");
00041         TeDecoderFactory::instanceName2Dec()["raw"]  = "MEMMAP";        
00042         TeDecoderFactory::instanceName2Dec()["RAW"]  = "MEMMAP";        
00043 
00044         TeRasterParams parRaw;
00045         parRaw.nBands(1);
00046         parRaw.setDataType(TeFLOAT);
00047         parRaw.fileName_ = "../data/elevation.raw";
00048         parRaw.mode_ = 'r';
00049         TeProjection* proj = new TeNoProjection();
00050         parRaw.projection(proj);
00051         parRaw.lowerLeftResolutionSize(183557.0,8246277.0,30,30,382,422,false);
00052 
00053         // Acess input image
00054         TeRaster elev(parRaw);
00055         if (!elev.init())
00056         {
00057                 cout << "Cannot access input grid!" << endl << endl;
00058                 cout << "Press Enter\n";
00059                 getchar();
00060                 return 1;
00061         }
00062 
00063         // Datatabase server parameters
00064         string host = "localhost";
00065         string dbname = "DB320RC1";
00066         string user = "root";
00067         string password = "vinhas";
00068         TeDatabase* db = new TeMySQL();
00069         if (!db->connect(host, user, password, dbname))
00070         {
00071                 elev.clear();
00072                 cout << "Error: " << db->errorMessage() << endl << endl;
00073                 cout << "Press Enter\n";
00074                 getchar();
00075                 return 1;
00076         }
00077 
00078         string layerName = "Elevation";
00079 
00080         // Check whether there is a layer with this name in the database
00081         if (db->layerExist(layerName))
00082         {
00083                 cout << "The database already has an infolayer with the name \"";
00084                 cout << layerName << "\"!" << endl << endl;
00085                 db->close();
00086                 delete db;
00087                 cout << "Press Enter\n";
00088                 getchar();
00089                 return 1;
00090         }
00091 
00092         // Create a layer to receive the raster geometry (same projection as raster data)
00093         TeLayer* layer = new TeLayer(layerName, db, elev.projection());
00094         if (layer->id() <= 0)
00095         {
00096                 elev.clear();
00097                 db->close();
00098                 delete db;
00099                 cout << "The destination layer could not be created!\n" << db->errorMessage() << endl << endl;
00100                 cout << "Press Enter\n";
00101                 getchar();
00102                 return 1;
00103         }
00104 
00105         // Import raster to the layer
00106         if (!TeImportRaster(layer, &elev, parRaw.ncols_, 2))
00107         {
00108                 elev.clear();
00109                 db->close();
00110                 delete db;
00111                 cout << "Fail to import grid!\n" << endl;
00112                 cout << "Press Enter\n";
00113                 getchar();
00114                 return 1;
00115         }
00116 
00117         // Close database
00118         db->close();
00119         delete db;
00120         cout << "The GRID data was imported successfully into the TerraLib database!\n\n";
00121         cout << "Press Enter\n";
00122         getchar();
00123         return 0;
00124 }
00125  

Generated on Sun Jul 29 04:01:03 2012 for TerraLib - Development Source by  doxygen 1.5.3