TsGrid.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/unittest/raster/TsGrid.cpp
22 
23  \brief A test suit for the Grid class.
24  */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
28 
29 #include <terralib/raster.h>
30 #include <terralib/geometry.h>
31 #include "../Config.h"
32 
33 // Boost
34 #include <boost/test/unit_test.hpp>
35 #include <boost/shared_ptr.hpp>
36 
38 
39 BOOST_AUTO_TEST_CASE (gridConstructor1_test)
40 {
41  te::rst::Grid grid( 2u, 2u, new te::gm::Envelope( 1.0, 1.0, 5.0, 3.0 ), 12345 );
42 
43  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
44  BOOST_CHECK( grid.getNumberOfRows() == 2 );
45  BOOST_CHECK_CLOSE( 2.0, grid.getResolutionX(), 0.0000000001 );
46  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
47  BOOST_CHECK( grid.getSRID() == 12345 );
48  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_llx, 0.0000000001 );
49  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_lly, 0.0000000001 );
50  BOOST_CHECK_CLOSE( 5.0, grid.getExtent()->m_urx, 0.0000000001 );
51  BOOST_CHECK_CLOSE( 3.0, grid.getExtent()->m_ury, 0.0000000001 );
52 
53  double x = 0;
54  double y = 0;
55 
56  grid.gridToGeo( 0.0 , 0.0, x, y );
57  BOOST_CHECK_CLOSE( 2.0, x, 0.0000000001 );
58  BOOST_CHECK_CLOSE( 2.5, y, 0.0000000001 );
59 
60  grid.gridToGeo( 1.0 , 1.0, x, y );
61  BOOST_CHECK_CLOSE( 4.0, x, 0.0000000001 );
62  BOOST_CHECK_CLOSE( 1.5, y, 0.0000000001 );
63 
64  grid.geoToGrid(2.0 , 2.5, x, y );
65  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
66  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
67 
68  grid.geoToGrid( 4.0 , 1.5, x, y );
69  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
70  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
71 }
72 
73 BOOST_AUTO_TEST_CASE (gridConstructor1NoEnv_test)
74 {
75  te::rst::Grid grid( 2u, 2u, (te::gm::Envelope*)0, 12345 );
76 
77  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
78  BOOST_CHECK( grid.getNumberOfRows() == 2 );
79  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionX(), 0.0000000001 );
80  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
81  BOOST_CHECK( grid.getSRID() == 12345 );
82  BOOST_CHECK_CLOSE( 0.0, grid.getExtent()->m_llx, 0.0000000001 );
83  BOOST_CHECK_CLOSE( -2.0, grid.getExtent()->m_lly, 0.0000000001 );
84  BOOST_CHECK_CLOSE( 2.0, grid.getExtent()->m_urx, 0.0000000001 );
85  BOOST_CHECK_CLOSE( 0.0, grid.getExtent()->m_ury, 0.0000000001 );
86 
87  double x = 0;
88  double y = 0;
89 
90  grid.gridToGeo( 0.0 , 0.0, x, y );
91  BOOST_CHECK_CLOSE( 0.5, x, 0.0000000001 );
92  BOOST_CHECK_CLOSE( -0.5, y, 0.0000000001 );
93 
94  grid.gridToGeo( 1.0 , 1.0, x, y );
95  BOOST_CHECK_CLOSE( 1.5, x, 0.0000000001 );
96  BOOST_CHECK_CLOSE( -1.5, y, 0.0000000001 );
97 
98  grid.geoToGrid(0.5 , -0.5, x, y );
99  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
100  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
101 
102  grid.geoToGrid( 1.5 , -1.5, x, y );
103  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
104  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
105 }
106 
107 BOOST_AUTO_TEST_CASE (gridConstructor2_test)
108 {
109  te::gm::Coord2D ulc;
110  ulc.x = 1;
111  ulc.y = 3;
112 
113  te::rst::Grid grid( 2u, 2u, 2.0, 1.0, &ulc, 12345 );
114 
115  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
116  BOOST_CHECK( grid.getNumberOfRows() == 2 );
117  BOOST_CHECK_CLOSE( 2.0, grid.getResolutionX(), 0.0000000001 );
118  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
119  BOOST_CHECK( grid.getSRID() == 12345 );
120  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_llx, 0.0000000001 );
121  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_lly, 0.0000000001 );
122  BOOST_CHECK_CLOSE( 5.0, grid.getExtent()->m_urx, 0.0000000001 );
123  BOOST_CHECK_CLOSE( 3.0, grid.getExtent()->m_ury, 0.0000000001 );
124 
125  double x = 0;
126  double y = 0;
127 
128  grid.gridToGeo( 0.0 , 0.0, x, y );
129  BOOST_CHECK_CLOSE( 2.0, x, 0.0000000001 );
130  BOOST_CHECK_CLOSE( 2.5, y, 0.0000000001 );
131 
132  grid.gridToGeo( 1.0 , 1.0, x, y );
133  BOOST_CHECK_CLOSE( 4.0, x, 0.0000000001 );
134  BOOST_CHECK_CLOSE( 1.5, y, 0.0000000001 );
135 
136  grid.geoToGrid(2.0 , 2.5, x, y );
137  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
138  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
139 
140  grid.geoToGrid( 4.0 , 1.5, x, y );
141  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
142  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
143 }
144 
145 BOOST_AUTO_TEST_CASE (gridConstructor2NoUlc_test)
146 {
147  te::rst::Grid grid( 2u, 2u, 1.0, 1.0, (te::gm::Coord2D*)0, 12345 );
148 
149  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
150  BOOST_CHECK( grid.getNumberOfRows() == 2 );
151  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionX(), 0.0000000001 );
152  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
153  BOOST_CHECK( grid.getSRID() == 12345 );
154  BOOST_CHECK_CLOSE( 0.0, grid.getExtent()->m_llx, 0.0000000001 );
155  BOOST_CHECK_CLOSE( -2.0, grid.getExtent()->m_lly, 0.0000000001 );
156  BOOST_CHECK_CLOSE( 2.0, grid.getExtent()->m_urx, 0.0000000001 );
157  BOOST_CHECK_CLOSE( 0.0, grid.getExtent()->m_ury, 0.0000000001 );
158 
159  double x = 0;
160  double y = 0;
161 
162  grid.gridToGeo( 0.0 , 0.0, x, y );
163  BOOST_CHECK_CLOSE( 0.5, x, 0.0000000001 );
164  BOOST_CHECK_CLOSE( -0.5, y, 0.0000000001 );
165 
166  grid.gridToGeo( 1.0 , 1.0, x, y );
167  BOOST_CHECK_CLOSE( 1.5, x, 0.0000000001 );
168  BOOST_CHECK_CLOSE( -1.5, y, 0.0000000001 );
169 
170  grid.geoToGrid(0.5 , -0.5, x, y );
171  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
172  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
173 
174  grid.geoToGrid( 1.5 , -1.5, x, y );
175  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
176  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
177 }
178 
179 BOOST_AUTO_TEST_CASE (gridConstructor3_test)
180 {
181  te::rst::Grid grid( 2u, 2u, 2.0, 1.0, new te::gm::Envelope( 1.0, 1.0, 5.0, 3.0 ), 12345 );
182 
183  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
184  BOOST_CHECK( grid.getNumberOfRows() == 2 );
185  BOOST_CHECK_CLOSE( 2.0, grid.getResolutionX(), 0.0000000001 );
186  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
187  BOOST_CHECK( grid.getSRID() == 12345 );
188  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_llx, 0.0000000001 );
189  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_lly, 0.0000000001 );
190  BOOST_CHECK_CLOSE( 5.0, grid.getExtent()->m_urx, 0.0000000001 );
191  BOOST_CHECK_CLOSE( 3.0, grid.getExtent()->m_ury, 0.0000000001 );
192 
193  double x = 0;
194  double y = 0;
195 
196  grid.gridToGeo( 0.0 , 0.0, x, y );
197  BOOST_CHECK_CLOSE( 2.0, x, 0.0000000001 );
198  BOOST_CHECK_CLOSE( 2.5, y, 0.0000000001 );
199 
200  grid.gridToGeo( 1.0 , 1.0, x, y );
201  BOOST_CHECK_CLOSE( 4.0, x, 0.0000000001 );
202  BOOST_CHECK_CLOSE( 1.5, y, 0.0000000001 );
203 
204  grid.geoToGrid(2.0 , 2.5, x, y );
205  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
206  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
207 
208  grid.geoToGrid( 4.0 , 1.5, x, y );
209  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
210  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
211 }
212 
213 BOOST_AUTO_TEST_CASE (gridConstructor4_test)
214 {
215  te::rst::Grid grid( 2.0, 1.0, new te::gm::Envelope( 1.0, 1.0, 5.0, 3.0 ), 12345 );
216 
217  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
218  BOOST_CHECK( grid.getNumberOfRows() == 2 );
219  BOOST_CHECK_CLOSE( 2.0, grid.getResolutionX(), 0.0000000001 );
220  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
221  BOOST_CHECK( grid.getSRID() == 12345 );
222  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_llx, 0.0000000001 );
223  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_lly, 0.0000000001 );
224  BOOST_CHECK_CLOSE( 5.0, grid.getExtent()->m_urx, 0.0000000001 );
225  BOOST_CHECK_CLOSE( 3.0, grid.getExtent()->m_ury, 0.0000000001 );
226 
227  double x = 0;
228  double y = 0;
229 
230  grid.gridToGeo( 0.0 , 0.0, x, y );
231  BOOST_CHECK_CLOSE( 2.0, x, 0.0000000001 );
232  BOOST_CHECK_CLOSE( 2.5, y, 0.0000000001 );
233 
234  grid.gridToGeo( 1.0 , 1.0, x, y );
235  BOOST_CHECK_CLOSE( 4.0, x, 0.0000000001 );
236  BOOST_CHECK_CLOSE( 1.5, y, 0.0000000001 );
237 
238  grid.geoToGrid(2.0 , 2.5, x, y );
239  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
240  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
241 
242  grid.geoToGrid( 4.0 , 1.5, x, y );
243  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
244  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
245 }
246 
247 BOOST_AUTO_TEST_CASE (gridConstructor5_test)
248 {
249  double geoTrans[ 6 ];
250  geoTrans[ 0 ] = 2.0;
251  geoTrans[ 1 ] = 0.0;
252  geoTrans[ 2 ] = 2.0;
253  geoTrans[ 3 ] = 0.0;
254  geoTrans[ 4 ] = -1.0;
255  geoTrans[ 5 ] = 2.5;
256 
257  te::rst::Grid grid( geoTrans, 2u, 2u, 12345 );
258 
259  BOOST_CHECK( grid.getNumberOfColumns() == 2 );
260  BOOST_CHECK( grid.getNumberOfRows() == 2 );
261  BOOST_CHECK_CLOSE( 2.0, grid.getResolutionX(), 0.0000000001 );
262  BOOST_CHECK_CLOSE( 1.0, grid.getResolutionY(), 0.0000000001 );
263  BOOST_CHECK( grid.getSRID() == 12345 );
264  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_llx, 0.0000000001 );
265  BOOST_CHECK_CLOSE( 1.0, grid.getExtent()->m_lly, 0.0000000001 );
266  BOOST_CHECK_CLOSE( 5.0, grid.getExtent()->m_urx, 0.0000000001 );
267  BOOST_CHECK_CLOSE( 3.0, grid.getExtent()->m_ury, 0.0000000001 );
268 
269  double x = 0;
270  double y = 0;
271 
272  grid.gridToGeo( 0.0 , 0.0, x, y );
273  BOOST_CHECK_CLOSE( 2.0, x, 0.0000000001 );
274  BOOST_CHECK_CLOSE( 2.5, y, 0.0000000001 );
275 
276  grid.gridToGeo( 1.0 , 1.0, x, y );
277  BOOST_CHECK_CLOSE( 4.0, x, 0.0000000001 );
278  BOOST_CHECK_CLOSE( 1.5, y, 0.0000000001 );
279 
280  grid.geoToGrid(2.0 , 2.5, x, y );
281  BOOST_CHECK_CLOSE( 0.0, x, 0.0000000001 );
282  BOOST_CHECK_CLOSE( 0.0, y, 0.0000000001 );
283 
284  grid.geoToGrid( 4.0 , 1.5, x, y );
285  BOOST_CHECK_CLOSE( 1.0, x, 0.0000000001 );
286  BOOST_CHECK_CLOSE( 1.0, y, 0.0000000001 );
287 }
288 
289 BOOST_AUTO_TEST_SUITE_END()
unsigned int getNumberOfRows() const
Returns the grid number of rows.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
int getSRID() const
Returns the grid spatial reference system identifier.
BOOST_AUTO_TEST_CASE(gridConstructor1NoEnv_test)
Definition: TsGrid.cpp:73
double m_urx
Upper right corner x-coordinate.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
double m_llx
Lower left corner x-coordinate.
An Envelope defines a 2D rectangular region.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
BOOST_AUTO_TEST_SUITE(grid_tests) BOOST_AUTO_TEST_CASE(gridConstructor1_test)
Definition: TsGrid.cpp:37
double m_lly
Lower left corner y-coordinate.
double m_ury
Upper right corner y-coordinate.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
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