1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
/*
This file shows an example of how to create a legend over a raster data,
stored in a layer TerraLib.
Author: Lubia Vinhas
*/
#include "TeRaster.h"
#include "TeMySQL.h"
int main()
{
// Datatabase server parameters
string host = "localhost";
string dbname = "DB320RC1";
string user = "root";
string password = "vinhas";
TeDatabase* db = new TeMySQL();
if (!db->connect(host, user, password, dbname))
{
cout << "Error: " << db->errorMessage() << endl << endl;
cout << "Press Enter\n";
getchar();
return 1;
}
// Retrieve a layer with a raster representation
TeLayer* layer = new TeLayer("Elevation",db);
if (layer->id() < 1)
{
cout << "Cannot access layer Elevation " << endl;
cout << endl << "Press Enter\n";
getchar();
return 1;
}
// Create a new view with the same projection as the layer
TeView* view = new TeView("ViewElevation", user);
view->projection(layer->projection());
if (!db->insertView(view))
{
cout << "Couldn�t insert view into the database: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
// Create a new theme and a legend over it
TeAbstractTheme* rstTheme = new TeTheme("Elevation", layer);
rstTheme->visibleRep(TeRASTER | 0x40000000); // make visible: the raster representation
// and the legend (0x4000000)
view->add(rstTheme); // add theme to the view
if (!rstTheme->save()) // save theme definition to the database
{
cout << "Error saving theme: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
// Define red, green and blue components to build a 10 color pallete
short red[10] = { 0, 45, 99, 160, 230, 232, 235, 237, 240, 242 };
short green[10] = { 166, 182, 198, 214, 230, 195, 178, 180, 201, 242 };
short blue[10] = { 0, 0, 0, 0, 0, 46, 94, 142, 192, 242 };
// Define a slicing mode: 10 slices
TeGrouping group1;
group1.groupMode_ = TeRasterSlicing;
group1.groupNumSlices_ = 10;
group1.groupPrecision_ = 8;
// Default way of slicing a raster representation: slices of equal size
if (!rstTheme->buildGrouping(group1))
{
cout << "Error grouping theme: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
// Assign to each slice one color of the pallete
int ng;
for (ng=0; ng<rstTheme->grouping().groupNumSlices_; ng++)
{
TeColor color(red[ng],green[ng],blue[ng]);
TeVisual* visual = TeVisualFactory::make("tevisual");
visual->color(color);
visual->transparency(rstTheme->defaultLegend().visual(TePOLYGONS)->transparency());
visual->contourStyle(rstTheme->defaultLegend().visual(TePOLYGONS)->contourStyle());
visual->contourWidth(rstTheme->defaultLegend().visual(TePOLYGONS)->contourWidth());
visual->contourColor(rstTheme->defaultLegend().visual(TePOLYGONS)->contourColor());
rstTheme->setGroupingVisual(ng+1,visual,TePOLYGONS);
}
// Saves the theme legend to the database
if (!rstTheme->saveGrouping())
{
cout << "Error saving theme slicing: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
cout << "Created first theme...\n";
delete rstTheme;
// Generates a new theme with slices arbitrarily defined
rstTheme = new TeTheme("Elevation4Slices", layer);
rstTheme->visibleRep(TeRASTER | 0x40000000); // make visible: the raster representation
// and the legend (0x4000000)
view->add(rstTheme); // add theme to the view
if (!rstTheme->save()) // save theme definition to the database
{
cout << "Error saving theme: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
vector<TeSlice> mySlices;
TeSlice sl1("980","1000");
mySlices.push_back(sl1);
TeSlice sl2("1000","1020");
mySlices.push_back(sl2);
TeSlice sl3("1020","1100");
mySlices.push_back(sl3);
TeSlice sl4("1100","1200");
mySlices.push_back(sl4);
TeGrouping group2;
group2.groupMode_ = TeRasterSlicing;
group2.groupNumSlices_ = 4;
group2.groupPrecision_ = 8;
if (!rstTheme->buildGrouping(group2,mySlices))
{
cout << "Error grouping theme: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
for (ng=0; ng<rstTheme->grouping().groupNumSlices_; ng++)
{
TeColor color(red[ng*2],green[ng*2],blue[ng*2]);
TeVisual* visual2 = TeVisualFactory::make("tevisual");
visual2->color(color);
visual2->transparency(rstTheme->defaultLegend().visual(TePOLYGONS)->transparency());
visual2->contourStyle(rstTheme->defaultLegend().visual(TePOLYGONS)->contourStyle());
visual2->contourWidth(rstTheme->defaultLegend().visual(TePOLYGONS)->contourWidth());
visual2->contourColor(rstTheme->defaultLegend().visual(TePOLYGONS)->contourColor());
rstTheme->setGroupingVisual(ng+1,visual2,TePOLYGONS);
}
if (!rstTheme->saveGrouping())
{
cout << "Error saving theme slicing: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
db->close();
cout << "Created second theme...\n";
cout << "Sucess. Press enter...\n";
getchar();
return 0;
}
|