~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to examples/importGridData/importGridData.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
Import upstream version 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
5
This code is part of the TerraLib library.
 
6
This library is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU Lesser General Public
 
8
License as published by the Free Software Foundation; either
 
9
version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
14
The authors reassure the license terms regarding the warranties.
 
15
They specifically disclaim any warranties, including, but not limited to,
 
16
the implied warranties of merchantability and fitness for a particular purpose.
 
17
The library provided hereunder is on an "as is" basis, and the authors have no
 
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
 
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
 
20
indirect, special, incidental, or consequential damages arising out of the use
 
21
of this library and its documentation.
 
22
*************************************************************************************/
 
23
 
 
24
/* 
 
25
        This file shows an example of how to import, to a TerraLib database, a raster data: 
 
26
        a binary raw grid data. Each point of the grid is a float value that represents the 
 
27
        elevation over an area 
 
28
 
 
29
        Author: Lubia Vinhas  
 
30
*/
 
31
 
 
32
#include <TeDatabase.h>
 
33
#include <TeMySQL.h>
 
34
#include <TeInitRasterDecoders.h>
 
35
#include <TeImportRaster.h>
 
36
 
 
37
int main()
 
38
{
 
39
        // Initialize the raster decoder tool
 
40
        TeInitRasterDecoders();
 
41
 
 
42
        TeRasterParams parRaw;
 
43
        parRaw.nBands(1);
 
44
        parRaw.setDataType(TeFLOAT);
 
45
        parRaw.fileName_ = "../data/elevation.raw";
 
46
        parRaw.mode_ = 'r';
 
47
        TeProjection* proj = new TeNoProjection();
 
48
        parRaw.projection(proj);
 
49
        parRaw.lowerLeftResolutionSize(183557.0,8246277.0,30,30,382,422,false);
 
50
 
 
51
        // Acess input image
 
52
        TeRaster elev(parRaw);
 
53
        if (!elev.init())
 
54
        {
 
55
                cout << "Cannot access input grid!" << endl << endl;
 
56
                cout << "Press Enter\n";
 
57
                getchar();
 
58
                return 1;
 
59
        }
 
60
 
 
61
        // Datatabase server parameters
 
62
        string host = "localhost";
 
63
        string dbname = "TerraTeste";
 
64
        string user = "root";
 
65
        string password = "";
 
66
        TeDatabase* db = new TeMySQL();
 
67
        if (!db->connect(host, user, password, dbname))
 
68
        {
 
69
                elev.clear();
 
70
                cout << "Error: " << db->errorMessage() << endl << endl;
 
71
                cout << "Press Enter\n";
 
72
                getchar();
 
73
                return 1;
 
74
        }
 
75
 
 
76
        string layerName = "Elevation";
 
77
 
 
78
        // Check whether there is a layer with this name in the database
 
79
        if (db->layerExist(layerName))
 
80
        {
 
81
                cout << "The database already has an infolayer with the name \"";
 
82
                cout << layerName << "\"!" << endl << endl;
 
83
                db->close();
 
84
                cout << "Press Enter\n";
 
85
                getchar();
 
86
                return 1;
 
87
        }
 
88
 
 
89
        // Create a layer to receive the raster geometry (same projection as raster data)
 
90
        TeLayer* layer = new TeLayer(layerName, db, elev.projection());
 
91
        if (layer->id() <= 0)
 
92
        {
 
93
                elev.clear();
 
94
                db->close();
 
95
                cout << "The destination layer could not be created!\n" << db->errorMessage() << endl << endl;
 
96
                cout << "Press Enter\n";
 
97
                getchar();
 
98
                return 1;
 
99
        }
 
100
 
 
101
        // Import raster to the layer
 
102
        if (!TeImportRaster(layer, &elev, parRaw.ncols_, 2))
 
103
        {
 
104
                elev.clear();
 
105
                db->close();
 
106
                cout << "Fail to import grid!\n" << endl;
 
107
                cout << "Press Enter\n";
 
108
                getchar();
 
109
                return 1;
 
110
        }
 
111
 
 
112
        // Close database
 
113
        db->close();
 
114
        cout << "The GRID data was imported successfully into the TerraLib database!\n\n";
 
115
        cout << "Press Enter\n";
 
116
        getchar();
 
117
        return 0;
 
118
}
 
119