svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / test / java / org / gvsig / raster / lib / buffer / impl / MemoryBufferTest.java @ 44831
History | View | Annotate | Download (13.2 KB)
1 | 44831 | jjdelcerro | package org.gvsig.raster.lib.buffer.impl; |
---|---|---|---|
2 | |||
3 | import java.util.Arrays; |
||
4 | import java.util.Iterator; |
||
5 | |||
6 | import org.cresques.cts.ICoordTrans; |
||
7 | import org.cresques.cts.IProjection; |
||
8 | |||
9 | import org.gvsig.fmap.crs.CRSFactory; |
||
10 | import org.gvsig.fmap.geom.Geometry; |
||
11 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
12 | import org.gvsig.fmap.geom.GeometryLocator; |
||
13 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
14 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
15 | import org.gvsig.raster.lib.buffer.api.Band; |
||
16 | import org.gvsig.raster.lib.buffer.api.Band.BandByte; |
||
17 | import org.gvsig.raster.lib.buffer.api.Band.BandDouble; |
||
18 | import org.gvsig.raster.lib.buffer.api.Band.BandFloat; |
||
19 | import org.gvsig.raster.lib.buffer.api.Band.BandInt; |
||
20 | import org.gvsig.raster.lib.buffer.api.Band.BandShort; |
||
21 | import org.gvsig.raster.lib.buffer.api.Buffer; |
||
22 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
23 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
24 | import org.gvsig.raster.lib.buffer.api.NoData; |
||
25 | import org.gvsig.raster.lib.buffer.api.exceptions.BandException; |
||
26 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
||
27 | import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
||
28 | import org.gvsig.tools.ToolsLocator; |
||
29 | import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
||
30 | import org.gvsig.tools.locator.LocatorException; |
||
31 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
32 | |||
33 | /**
|
||
34 | * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
||
35 | *
|
||
36 | */
|
||
37 | public class MemoryBufferTest extends AbstractLibraryAutoInitTestCase { |
||
38 | |||
39 | private final int BUFFER_ROWS = 5; |
||
40 | private final int BUFFER_COLUMNS = 5; |
||
41 | |||
42 | private final Number defaultNoDataValue = 10; |
||
43 | private final Number noDataValue = 20; |
||
44 | private Buffer testBuffer = null; |
||
45 | |||
46 | @Override
|
||
47 | protected void doSetUp() throws Exception { |
||
48 | |||
49 | int[] bandDataTypes = |
||
50 | new int[] { BufferManager.TYPE_BYTE, BufferManager.TYPE_SHORT, BufferManager.TYPE_INT, BufferManager.TYPE_FLOAT, |
||
51 | BufferManager.TYPE_DOUBLE }; |
||
52 | |||
53 | NoData[] bandNodata = new NoData[6]; |
||
54 | Arrays.fill(bandNodata,
|
||
55 | BufferLocator.getBufferManager().createNoData(noDataValue, defaultNoDataValue)); |
||
56 | |||
57 | testBuffer = |
||
58 | BufferLocator.getBufferManager().createBuffer(BUFFER_ROWS, BUFFER_COLUMNS, |
||
59 | bandDataTypes, bandNodata, null, null); |
||
60 | } |
||
61 | |||
62 | /**
|
||
63 | * Test get statistics
|
||
64 | */
|
||
65 | public void testGetStatistics() { |
||
66 | |||
67 | SimpleTaskStatus simpleTaskStatus = |
||
68 | ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("test statistics");
|
||
69 | Statistics statistics = testBuffer.getStatistics(simpleTaskStatus); |
||
70 | assertNotNull(statistics); |
||
71 | } |
||
72 | |||
73 | /**
|
||
74 | * Test iterator method
|
||
75 | */
|
||
76 | public void testIterator() { |
||
77 | |||
78 | Iterator<Band> iterator = testBuffer.iterator();
|
||
79 | assertNotNull(iterator); |
||
80 | |||
81 | int cont = 0; |
||
82 | while (iterator.hasNext()) {
|
||
83 | assertNotNull(iterator.next()); |
||
84 | cont++; |
||
85 | } |
||
86 | assertEquals(5, cont);
|
||
87 | } |
||
88 | |||
89 | /**
|
||
90 | * Test get band count
|
||
91 | */
|
||
92 | public void testGetBandCount() { |
||
93 | assertEquals(5, testBuffer.getBandCount());
|
||
94 | } |
||
95 | |||
96 | /**
|
||
97 | * Test get bands
|
||
98 | */
|
||
99 | public void testGetBands() { |
||
100 | Band[] bands = testBuffer.getBands();
|
||
101 | assertEquals(testBuffer.getBandCount(), bands.length); |
||
102 | } |
||
103 | |||
104 | /**
|
||
105 | * Test get column method
|
||
106 | */
|
||
107 | public void testGetColumns() { |
||
108 | assertEquals(BUFFER_COLUMNS, testBuffer.getColumns()); |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * Test get row method
|
||
113 | */
|
||
114 | public void testGetRows() { |
||
115 | assertEquals(BUFFER_ROWS, testBuffer.getRows()); |
||
116 | } |
||
117 | |||
118 | /**
|
||
119 | * Test is inside method
|
||
120 | */
|
||
121 | public void testIsInsideIntInt() { |
||
122 | assertTrue(testBuffer.isInside(2, 2)); |
||
123 | assertFalse(testBuffer.isInside(10, 10)); |
||
124 | assertFalse(testBuffer.isInside(-5, -10)); |
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * Test add band method
|
||
129 | */
|
||
130 | public void testAddBand() { |
||
131 | |||
132 | Band band = null;
|
||
133 | Band wrongBand = null;
|
||
134 | |||
135 | try {
|
||
136 | band = |
||
137 | BufferLocator.getBufferManager().createBand(BufferManager.TYPE_FLOAT, BUFFER_ROWS, |
||
138 | BUFFER_COLUMNS, null, null); |
||
139 | wrongBand = |
||
140 | BufferLocator.getBufferManager().createBand(BufferManager.TYPE_FLOAT, |
||
141 | BUFFER_ROWS * 2, BUFFER_COLUMNS * 2, null, null); |
||
142 | } catch (LocatorException | BandException e) {
|
||
143 | fail("Can not create bands to test add band method");
|
||
144 | } |
||
145 | |||
146 | testBuffer.addBand(band); |
||
147 | |||
148 | Exception e = null; |
||
149 | try {
|
||
150 | testBuffer.addBand(wrongBand); |
||
151 | } catch (RuntimeException runtimeException) { |
||
152 | // Expected exception
|
||
153 | e = runtimeException; |
||
154 | } |
||
155 | assertNotNull(e); |
||
156 | } |
||
157 | |||
158 | /**
|
||
159 | * Test set band method
|
||
160 | */
|
||
161 | public void testSetBand() { |
||
162 | |||
163 | Band newBandByte = null;
|
||
164 | Band newBandDouble = null;
|
||
165 | try {
|
||
166 | newBandByte = |
||
167 | BufferLocator.getBufferManager().createBand(BufferManager.TYPE_BYTE, BUFFER_ROWS, |
||
168 | BUFFER_COLUMNS, null, null); |
||
169 | newBandDouble = |
||
170 | BufferLocator.getBufferManager().createBand(BufferManager.TYPE_DOUBLE, BUFFER_ROWS, |
||
171 | BUFFER_COLUMNS, null, null); |
||
172 | } catch (LocatorException | BandException e) {
|
||
173 | fail("Can not create band to set band to buffer");
|
||
174 | } |
||
175 | |||
176 | try {
|
||
177 | testBuffer.setBand(0, newBandByte);
|
||
178 | } catch (BandException e) {
|
||
179 | fail("Cant not set new band to buffer");
|
||
180 | } |
||
181 | |||
182 | assertEquals(5, testBuffer.getBandCount());
|
||
183 | |||
184 | Band band = testBuffer.getBand(0);
|
||
185 | for (int i = 0; i < band.getRows(); i++) { |
||
186 | for (int j = 0; j < band.getColumns(); j++) { |
||
187 | assertEquals(band.get(i, j), newBandByte.get(i, j)); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | Exception e = null; |
||
192 | try {
|
||
193 | testBuffer.setBand(0, newBandDouble);
|
||
194 | } catch (BandException exception) {
|
||
195 | // Expected exception
|
||
196 | e = exception; |
||
197 | } |
||
198 | assertNotNull(e); |
||
199 | } |
||
200 | |||
201 | /**
|
||
202 | * Test remove band method
|
||
203 | */
|
||
204 | public void testRemoveBand() { |
||
205 | |||
206 | testBuffer.removeBand(0);
|
||
207 | assertEquals(4, testBuffer.getBandCount());
|
||
208 | testBuffer.removeBand(1);
|
||
209 | assertEquals(3, testBuffer.getBandCount());
|
||
210 | } |
||
211 | |||
212 | /**
|
||
213 | * Test get band method
|
||
214 | */
|
||
215 | public void testGetBand() { |
||
216 | Band band = testBuffer.getBand(0);
|
||
217 | assertNotNull(band); |
||
218 | assertEquals(BufferManager.TYPE_BYTE, band.getDataType()); |
||
219 | |||
220 | band = testBuffer.getBand(4);
|
||
221 | assertEquals(BufferManager.TYPE_DOUBLE, band.getDataType()); |
||
222 | } |
||
223 | |||
224 | /**
|
||
225 | * Test get band byte method
|
||
226 | */
|
||
227 | public void testGetBandByte() { |
||
228 | |||
229 | BandByte bandByte = testBuffer.getBandByte(0);
|
||
230 | assertNotNull(bandByte); |
||
231 | assertEquals(BufferManager.TYPE_BYTE, bandByte.getDataType()); |
||
232 | |||
233 | Exception e = null; |
||
234 | try {
|
||
235 | testBuffer.getBandDouble(1);
|
||
236 | } catch (RuntimeException runtimeException) { |
||
237 | // Expected exception
|
||
238 | e = runtimeException; |
||
239 | } |
||
240 | assertNotNull(e); |
||
241 | } |
||
242 | |||
243 | /**
|
||
244 | * Test get band short
|
||
245 | */
|
||
246 | public void testGetBandShort() { |
||
247 | BandShort bandShort = testBuffer.getBandShort(1);
|
||
248 | assertNotNull(bandShort); |
||
249 | assertEquals(BufferManager.TYPE_SHORT, bandShort.getDataType()); |
||
250 | |||
251 | Exception e = null; |
||
252 | try {
|
||
253 | testBuffer.getBandDouble(0);
|
||
254 | } catch (RuntimeException runtimeException) { |
||
255 | // Expected exception
|
||
256 | e = runtimeException; |
||
257 | } |
||
258 | assertNotNull(e); |
||
259 | } |
||
260 | |||
261 | /**
|
||
262 | * Test get band int
|
||
263 | */
|
||
264 | public void testGetBandInt() { |
||
265 | BandInt bandInt = testBuffer.getBandInt(2);
|
||
266 | assertNotNull(bandInt); |
||
267 | assertEquals(BufferManager.TYPE_INT, bandInt.getDataType()); |
||
268 | |||
269 | Exception e = null; |
||
270 | try {
|
||
271 | testBuffer.getBandDouble(1);
|
||
272 | } catch (RuntimeException runtimeException) { |
||
273 | // Expected exception
|
||
274 | e = runtimeException; |
||
275 | } |
||
276 | assertNotNull(e); |
||
277 | } |
||
278 | |||
279 | /**
|
||
280 | * Test get band float
|
||
281 | */
|
||
282 | public void testGetBandFloat() { |
||
283 | BandFloat bandFloat = testBuffer.getBandFloat(3);
|
||
284 | assertNotNull(bandFloat); |
||
285 | assertEquals(BufferManager.TYPE_FLOAT, bandFloat.getDataType()); |
||
286 | |||
287 | Exception e = null; |
||
288 | try {
|
||
289 | testBuffer.getBandDouble(3);
|
||
290 | } catch (RuntimeException runtimeException) { |
||
291 | // Expected exception
|
||
292 | e = runtimeException; |
||
293 | } |
||
294 | assertNotNull(e); |
||
295 | } |
||
296 | |||
297 | /**
|
||
298 | * Test get band double
|
||
299 | */
|
||
300 | public void testGetBandDouble() { |
||
301 | BandDouble bandByte = testBuffer.getBandDouble(4);
|
||
302 | assertNotNull(bandByte); |
||
303 | assertEquals(BufferManager.TYPE_DOUBLE, bandByte.getDataType()); |
||
304 | |||
305 | Exception e = null; |
||
306 | try {
|
||
307 | testBuffer.getBandDouble(1);
|
||
308 | } catch (RuntimeException runtimeException) { |
||
309 | // Expected exception
|
||
310 | e = runtimeException; |
||
311 | } |
||
312 | assertNotNull(e); |
||
313 | } |
||
314 | |||
315 | /**
|
||
316 | *
|
||
317 | */
|
||
318 | public void testSwitchBandsIntArray() { |
||
319 | |||
320 | int[] positions = new int[] { 4, 2, 1, 3, 0 }; |
||
321 | testBuffer.switchBands(positions); |
||
322 | |||
323 | assertEquals(testBuffer.getBand(0).getDataType(), BufferManager.TYPE_DOUBLE);
|
||
324 | assertEquals(testBuffer.getBand(1).getDataType(), BufferManager.TYPE_INT);
|
||
325 | assertEquals(testBuffer.getBand(2).getDataType(), BufferManager.TYPE_SHORT);
|
||
326 | assertEquals(testBuffer.getBand(3).getDataType(), BufferManager.TYPE_FLOAT);
|
||
327 | assertEquals(testBuffer.getBand(4).getDataType(), BufferManager.TYPE_BYTE);
|
||
328 | } |
||
329 | |||
330 | /**
|
||
331 | * Test swing band method
|
||
332 | */
|
||
333 | public void testSwitchBandsIntInt() { |
||
334 | |||
335 | Band oldBand = testBuffer.getBand(0);
|
||
336 | testBuffer.switchBands(0, 1); |
||
337 | Band newBand = testBuffer.getBand(1);
|
||
338 | |||
339 | for (int i = 0; i < oldBand.getRows(); i++) { |
||
340 | for (int j = 0; j < oldBand.getColumns(); j++) { |
||
341 | assertEquals(oldBand.get(i, j), newBand.get(i, j)); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | assertEquals(testBuffer.getBand(0).getDataType(), BufferManager.TYPE_SHORT);
|
||
346 | assertEquals(testBuffer.getBand(1).getDataType(), BufferManager.TYPE_BYTE);
|
||
347 | } |
||
348 | |||
349 | public void testConvert() { |
||
350 | |||
351 | int[] bandDataTypes = |
||
352 | new int[] { BufferManager.TYPE_DOUBLE, BufferManager.TYPE_DOUBLE, |
||
353 | BufferManager.TYPE_DOUBLE }; |
||
354 | |||
355 | NoData[] bandNodata = new NoData[3]; |
||
356 | Arrays.fill(bandNodata, BufferLocator.getBufferManager().createNoData(-99999d, -99999d)); |
||
357 | |||
358 | try {
|
||
359 | Envelope envelope = |
||
360 | GeometryLocator.getGeometryManager().createEnvelope(625397.5, 4446997.5, 654602.5, |
||
361 | 4466602.5, SUBTYPES.GEOM2D);
|
||
362 | IProjection projection = CRSFactory.getCRS("EPSG:25830");
|
||
363 | testBuffer = |
||
364 | BufferLocator.getBufferManager().createBuffer(39, 58, bandDataTypes, bandNodata, |
||
365 | projection, envelope); |
||
366 | |||
367 | ICoordTrans ct = projection.getCT(CRSFactory.getCRS("EPSG:4326"));
|
||
368 | Buffer projectedBuffer = testBuffer.convert(ct, null); |
||
369 | |||
370 | assertEquals(testBuffer.getEnvelope().convert(ct), projectedBuffer.getEnvelope()); |
||
371 | |||
372 | } catch (CreateEnvelopeException | BufferException e) {
|
||
373 | fail("Can not create envelope or there are problems projecting buffers");
|
||
374 | } |
||
375 | } |
||
376 | |||
377 | public void testCreateInterpolated() { |
||
378 | |||
379 | double[][] dataDouble = |
||
380 | new double[][] { { 10, 10, 10, 10, 10 }, { 9, 9, 9, 9, 9 }, { 8, 8, 8, 8, 8 }, |
||
381 | { 6, 6, 6, 6, 6 }, { 1, 1, 1, 1, 1 } }; |
||
382 | |||
383 | Band band = testBuffer.getBand(4);
|
||
384 | for (int i = 0; i < band.getRows(); i++) { |
||
385 | for (int j = 0; j < band.getColumns(); j++) { |
||
386 | band.set(i, j, dataDouble[i][j]); |
||
387 | } |
||
388 | } |
||
389 | |||
390 | Buffer interpolatedBuffer = null; |
||
391 | try {
|
||
392 | interpolatedBuffer = testBuffer.createInterpolated(BUFFER_ROWS * 5, BUFFER_COLUMNS * 5, |
||
393 | Buffer.INTERPOLATION_NearestNeighbour, null); |
||
394 | } catch (LocatorException | BufferException e) {
|
||
395 | fail("Can not interpolate buffer");
|
||
396 | } |
||
397 | |||
398 | assertEquals(testBuffer.getBandCount(), interpolatedBuffer.getBandCount()); |
||
399 | assertEquals(BUFFER_COLUMNS * 5, interpolatedBuffer.getColumns());
|
||
400 | assertEquals(BUFFER_ROWS * 5, interpolatedBuffer.getRows());
|
||
401 | assertTrue(Arrays.equals(testBuffer.getBandTypes(), interpolatedBuffer.getBandTypes()));
|
||
402 | assertTrue(Arrays.equals(testBuffer.getBandNoData(), interpolatedBuffer.getBandNoData()));
|
||
403 | // assertEquals(testBuffer.getEnvelope(), interpolatedBuffer.getEnvelope());
|
||
404 | assertEquals(testBuffer.getProjection(), interpolatedBuffer.getProjection()); |
||
405 | } |
||
406 | |||
407 | public void testPixelSize() throws Exception { |
||
408 | assertEquals(1d, testBuffer.getPixelSizeX());
|
||
409 | assertEquals(1d, testBuffer.getPixelSizeY());
|
||
410 | |||
411 | Buffer testBuffer2 = BufferLocator.getBufferManager().createBuffer(BUFFER_ROWS, BUFFER_COLUMNS,
|
||
412 | new int[] { BufferManager.TYPE_BYTE }, new NoData[1], null, GeometryLocator.getGeometryManager().createEnvelope(0d, 0d, 100d, 100d, Geometry.SUBTYPES.GEOM2D)); |
||
413 | |||
414 | assertEquals(20d, testBuffer2.getPixelSizeX());
|
||
415 | assertEquals(20d, testBuffer2.getPixelSizeY());
|
||
416 | } |
||
417 | } |