Revision 5526
org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.buffer/org.gvsig.raster.lib.buffer.impl/src/test/java/org/gvsig/raster/lib/buffer/impl/MemoryBufferTest.java | ||
---|---|---|
3 | 3 |
import java.util.Arrays; |
4 | 4 |
import java.util.Iterator; |
5 | 5 |
|
6 |
import org.cresques.cts.ICoordTrans; |
|
7 |
import org.cresques.cts.IProjection; |
|
8 |
import org.gvsig.fmap.crs.CRSFactory; |
|
9 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
10 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
11 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
|
12 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
6 | 13 |
import org.gvsig.raster.lib.buffer.api.Band; |
7 | 14 |
import org.gvsig.raster.lib.buffer.api.Band.BandByte; |
8 | 15 |
import org.gvsig.raster.lib.buffer.api.Band.BandDouble; |
... | ... | |
14 | 21 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
15 | 22 |
import org.gvsig.raster.lib.buffer.api.NoData; |
16 | 23 |
import org.gvsig.raster.lib.buffer.api.exceptions.BandException; |
24 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
|
17 | 25 |
import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
18 | 26 |
import org.gvsig.tools.ToolsLocator; |
19 | 27 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
... | ... | |
53 | 61 |
/** |
54 | 62 |
* Test get histogram |
55 | 63 |
*/ |
56 |
// public void testGetHistogram() {
|
|
57 |
// |
|
58 |
// SimpleTaskStatus simpleTaskStatus =
|
|
59 |
// ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("test histogram");
|
|
60 |
// Histogram histogram = testBuffer.getHistogram(simpleTaskStatus);
|
|
61 |
// assertNotNull(histogram);
|
|
62 |
// |
|
63 |
// }
|
|
64 |
// public void testGetHistogram() {
|
|
65 |
//
|
|
66 |
// SimpleTaskStatus simpleTaskStatus =
|
|
67 |
// ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("test histogram");
|
|
68 |
// Histogram histogram = testBuffer.getHistogram(simpleTaskStatus);
|
|
69 |
// assertNotNull(histogram);
|
|
70 |
//
|
|
71 |
// }
|
|
64 | 72 |
|
65 | 73 |
/** |
66 | 74 |
* Test get statistics |
... | ... | |
329 | 337 |
|
330 | 338 |
assertEquals(testBuffer.getBand(0).getDataType(), BufferManager.TYPE_FLOAT); |
331 | 339 |
assertEquals(testBuffer.getBand(1).getDataType(), BufferManager.TYPE_DOUBLE); |
332 |
// assertEquals(testBuffer.getBand(2).getDataType(), BufferManager.TYPE_USHORT); //FIXME : type unshort? |
|
340 |
// assertEquals(testBuffer.getBand(2).getDataType(), |
|
341 |
// BufferManager.TYPE_USHORT); //FIXME : type unshort? |
|
333 | 342 |
assertEquals(testBuffer.getBand(3).getDataType(), BufferManager.TYPE_SHORT); |
334 | 343 |
assertEquals(testBuffer.getBand(4).getDataType(), BufferManager.TYPE_INT); |
335 | 344 |
assertEquals(testBuffer.getBand(5).getDataType(), BufferManager.TYPE_BYTE); |
... | ... | |
353 | 362 |
assertEquals(testBuffer.getBand(0).getDataType(), BufferManager.TYPE_SHORT); |
354 | 363 |
assertEquals(testBuffer.getBand(1).getDataType(), BufferManager.TYPE_BYTE); |
355 | 364 |
} |
365 |
|
|
366 |
public void testConvert() { |
|
367 |
|
|
368 |
int[] bandDataTypes = |
|
369 |
new int[] { BufferManager.TYPE_DOUBLE, BufferManager.TYPE_DOUBLE, |
|
370 |
BufferManager.TYPE_DOUBLE }; |
|
371 |
|
|
372 |
NoData[] bandNodata = new NoData[3]; |
|
373 |
Arrays.fill(bandNodata, BufferLocator.getBufferManager().createNoData(-99999d, -99999d)); |
|
374 |
|
|
375 |
try { |
|
376 |
Envelope envelope = |
|
377 |
GeometryLocator.getGeometryManager().createEnvelope(625397.5, 4446997.5, 654602.5, |
|
378 |
4466602.5, SUBTYPES.GEOM2D); |
|
379 |
IProjection projection = CRSFactory.getCRS("EPSG:25830"); |
|
380 |
testBuffer = |
|
381 |
BufferLocator.getBufferManager().createBuffer(39, 58, bandDataTypes, |
|
382 |
bandNodata, projection, envelope); |
|
383 |
|
|
384 |
ICoordTrans ct = projection.getCT(CRSFactory.getCRS("EPSG:4326")); |
|
385 |
Buffer projectedBuffer = testBuffer.convert(ct, null); |
|
386 |
|
|
387 |
assertEquals(testBuffer.getEnvelope().convert(ct), projectedBuffer.getEnvelope()); |
|
388 |
|
|
389 |
} catch (CreateEnvelopeException | BufferException e) { |
|
390 |
fail("Can not create envelope or there are problems projecting buffers"); |
|
391 |
} |
|
392 |
} |
|
356 | 393 |
} |
org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.buffer/org.gvsig.raster.lib.buffer.impl/src/main/java/org/gvsig/raster/lib/buffer/impl/DefaultBuffer.java | ||
---|---|---|
1 | 1 |
package org.gvsig.raster.lib.buffer.impl; |
2 | 2 |
|
3 | 3 |
import java.awt.geom.AffineTransform; |
4 |
import java.awt.geom.NoninvertibleTransformException; |
|
4 | 5 |
import java.util.ArrayList; |
5 | 6 |
import java.util.Iterator; |
6 | 7 |
import java.util.List; |
... | ... | |
8 | 9 |
import org.cresques.cts.ICoordTrans; |
9 | 10 |
import org.cresques.cts.IProjection; |
10 | 11 |
import org.gvsig.fmap.geom.Geometry; |
12 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
11 | 13 |
import org.gvsig.fmap.geom.GeometryLocator; |
14 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
12 | 15 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
13 | 16 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
14 | 17 |
import org.gvsig.fmap.geom.primitive.Envelope; |
... | ... | |
30 | 33 |
import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
31 | 34 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CreateBufferException; |
32 | 35 |
import org.gvsig.raster.lib.buffer.impl.statistics.DefaultStatistics; |
36 |
import org.gvsig.tools.ToolsLocator; |
|
33 | 37 |
import org.gvsig.tools.task.SimpleTaskStatus; |
34 | 38 |
import org.slf4j.Logger; |
35 | 39 |
import org.slf4j.LoggerFactory; |
... | ... | |
88 | 92 |
GeometryLocator.getGeometryManager().createEnvelope(0, 0, columns, rows, |
89 | 93 |
Geometry.SUBTYPES.GEOM2D); |
90 | 94 |
} |
91 |
// this.bandPageManager = pageManagers;
|
|
95 |
// this.bandPageManager = pageManagers;
|
|
92 | 96 |
createBands(bandDataTypes, bandNoData, pageManagers); |
93 | 97 |
} catch (Exception e) { |
94 | 98 |
throw new CreateBufferException(e); |
... | ... | |
97 | 101 |
|
98 | 102 |
private void createBands(int[] bandDataTypes, NoData[] bandNoData, |
99 | 103 |
List<PageManager> pageManagers) throws BandException { |
100 |
|
|
104 |
|
|
101 | 105 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
102 | 106 |
|
103 | 107 |
for (int i = 0; i < bandDataTypes.length; i++) { |
... | ... | |
106 | 110 |
if (bandNoData != null && i < bandNoData.length) { |
107 | 111 |
noDataBand = bandNoData[i]; |
108 | 112 |
} |
109 |
|
|
113 |
|
|
110 | 114 |
PageManager pageManager = null; |
111 | 115 |
if (pageManagers != null && i < pageManagers.size()) { |
112 | 116 |
pageManager = pageManagers.get(i); |
... | ... | |
310 | 314 |
"Buffer envelope is null. A buffer allways has to have envelope"); |
311 | 315 |
} |
312 | 316 |
|
317 |
boolean isMyStatus = false; |
|
318 |
if (status == null) { |
|
319 |
status = |
|
320 |
ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus( |
|
321 |
String.format("Projecting buffer from %1s to %2s", ct.getPOrig().getAbrev(), ct |
|
322 |
.getPDest().getAbrev())); |
|
323 |
status.add(); |
|
324 |
isMyStatus = true; |
|
325 |
} else { |
|
326 |
status.push(); |
|
327 |
} |
|
328 |
|
|
313 | 329 |
Envelope projectedBufferEnvelope = this.getEnvelope().convert(ct); |
314 |
double projectedBufferCellSizeX = projectedBufferEnvelope.getLength(0) / this.columns; |
|
315 |
double projectedBufferCellSizeY = projectedBufferEnvelope.getLength(1) / this.rows; |
|
316 |
int projectedBufferRows = |
|
317 |
(int) Math.ceil(projectedBufferEnvelope.getLength(0) / projectedBufferCellSizeX); |
|
318 |
int projectedBufferColumns = |
|
319 |
(int) Math.ceil(projectedBufferEnvelope.getLength(1) / projectedBufferCellSizeY); |
|
330 |
// Get size of new buffer |
|
331 |
double[] newsize = |
|
332 |
getSize(this.getEnvelope(), projectedBufferEnvelope, this.getColumns(), this.getRows()); |
|
333 |
int projectedBufferColumns = (int) newsize[0]; |
|
334 |
int projectedBufferRows = (int) newsize[1]; |
|
320 | 335 |
|
321 | 336 |
int[] bandDataTypes = this.getBandTypes(); |
322 | 337 |
NoData[] bandNoData = this.getBandNoData(); |
... | ... | |
324 | 339 |
BufferLocator.getBufferManager().createSimpleBandPageManagerList(null, |
325 | 340 |
projectedBufferRows, projectedBufferColumns, bandDataTypes); |
326 | 341 |
|
342 |
status.message("Creating projected buffer"); |
|
327 | 343 |
Buffer projectedBuffer = |
328 | 344 |
BufferLocator.getBufferManager().createBuffer(projectedBufferRows, |
329 | 345 |
projectedBufferColumns, bandDataTypes, bandNoData, ct.getPDest(), |
... | ... | |
331 | 347 |
|
332 | 348 |
// This affine transform allow us to convert pixel to coordinate and |
333 | 349 |
// inverse |
350 |
AffineTransform bufferAf = |
|
351 |
calculateAffineTransform(this.getEnvelope(), this.getRows(), this.getColumns()); |
|
334 | 352 |
AffineTransform projectedBufferAf = |
335 | 353 |
calculateAffineTransform(projectedBufferEnvelope, projectedBufferRows, |
336 | 354 |
projectedBufferColumns); |
337 | 355 |
|
356 |
status.setRangeOfValues(0, projectedBuffer.getRows() * projectedBuffer.getColumns() |
|
357 |
* projectedBuffer.getBandCount()); |
|
358 |
status.message("Projecting buffer"); |
|
359 |
|
|
338 | 360 |
for (int row = 0; row < projectedBuffer.getRows(); row++) { |
361 |
|
|
362 |
if (status.isCancellationRequested()) { |
|
363 |
status.cancel(); |
|
364 |
return projectedBuffer; |
|
365 |
} |
|
366 |
|
|
339 | 367 |
for (int col = 0; col < projectedBuffer.getColumns(); col++) { |
368 |
Point point = null; |
|
369 |
try { |
|
370 |
point = |
|
371 |
GeometryLocator.getGeometryManager().createPoint(col, row, SUBTYPES.GEOM2D); |
|
340 | 372 |
|
341 |
// Point worldPoint = new Poin2: |
|
373 |
point.transform(projectedBufferAf); // projected buffer pixel to world point (target projection) |
|
374 |
point.reProject(ct.getInverted()); // projected world point to world point (source projection) |
|
375 |
point.transform(bufferAf.createInverse()); // world point to pixel source buffer |
|
342 | 376 |
|
377 |
for (int k = 0; k < this.getBandCount(); k++) { |
|
378 |
|
|
379 |
status.setCurValue(row * col * k); |
|
380 |
Band band = this.bands.get(k); |
|
381 |
Object value = null; |
|
382 |
if (point.getX() >= 0 && Math.floor(point.getX()) < this.getColumns() |
|
383 |
&& point.getY() >= 0 && Math.floor(point.getY()) < this.getRows()) { |
|
384 |
|
|
385 |
value = |
|
386 |
band.get((int) Math.floor(point.getY()), |
|
387 |
(int) Math.floor(point.getX())); |
|
388 |
|
|
389 |
} else if (band.getNoData().isDefined()) { |
|
390 |
value = band.getNoData().getValue(); |
|
391 |
} else { |
|
392 |
continue; |
|
393 |
} |
|
394 |
|
|
395 |
Band projectedBand = projectedBuffer.getBand(k); |
|
396 |
projectedBand.set(row, col, value); |
|
397 |
} |
|
398 |
|
|
399 |
} catch (CreateGeometryException e) { |
|
400 |
status.abort(); |
|
401 |
logger.error("Can not create point geometry with [{},{}] to project buffer", |
|
402 |
col, row); |
|
403 |
throw new BufferException(e); |
|
404 |
} catch (NoninvertibleTransformException e) { |
|
405 |
status.abort(); |
|
406 |
logger.error("Can not create inverse transformation of {}", bufferAf); |
|
407 |
throw new BufferException(e); |
|
408 |
} |
|
343 | 409 |
} |
344 | 410 |
} |
345 | 411 |
|
346 |
return null; |
|
412 |
if (isMyStatus) { |
|
413 |
status.terminate(); |
|
414 |
} else { |
|
415 |
status.pop(); |
|
416 |
} |
|
417 |
return projectedBuffer; |
|
347 | 418 |
} |
419 |
|
|
420 |
private double[] getSize(Envelope envelope, Envelope projectedEnvelope, int p1x, int p1y) { |
|
421 |
double sumSideOldBBox = envelope.getLength(0) + envelope.getLength(1); |
|
422 |
double sumSideNewBBox = projectedEnvelope.getLength(0) + projectedEnvelope.getLength(1); |
|
423 |
double d1x = (envelope.getLength(0) * 100) / sumSideOldBBox; |
|
424 |
double d1y = (envelope.getLength(1) * 100) / sumSideOldBBox; |
|
425 |
double d2x = (projectedEnvelope.getLength(0) * 100) / sumSideNewBBox; |
|
426 |
double d2y = (projectedEnvelope.getLength(1) * 100) / sumSideNewBBox; |
|
427 |
double p2y = (p1y * d2y) / d1y; |
|
428 |
double p2x = (p1x * d2x) / d1x; |
|
429 |
double newCellSize = projectedEnvelope.getLength(0) / p2x; |
|
430 |
return new double[] { Math.round(p2x), Math.round(p2y), newCellSize }; |
|
431 |
} |
|
348 | 432 |
|
349 | 433 |
private AffineTransform calculateAffineTransform(Envelope projectedBufferEnvelope, |
350 | 434 |
int projectedBufferRows, int projectedBufferColumns) { |
351 |
// TODO Auto-generated method stub |
|
352 |
return null; |
|
435 |
|
|
436 |
double cellSizeX = projectedBufferEnvelope.getLength(0) / projectedBufferColumns; |
|
437 |
double cellSizeY = projectedBufferEnvelope.getLength(1) / projectedBufferRows; |
|
438 |
|
|
439 |
return new AffineTransform(cellSizeX, 0d, 0d, -cellSizeY, |
|
440 |
projectedBufferEnvelope.getMinimum(0), projectedBufferEnvelope.getMaximum(1)); |
|
353 | 441 |
} |
354 | 442 |
|
355 | 443 |
public int[] getBandTypes() { |
org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.buffer/org.gvsig.raster.lib.buffer.impl/pom.xml | ||
---|---|---|
25 | 25 |
<scope>test</scope> |
26 | 26 |
</dependency> |
27 | 27 |
<dependency> |
28 |
<groupId>org.gvsig</groupId> |
|
29 |
<artifactId>org.gvsig.compat.se</artifactId> |
|
30 |
<scope>test</scope> |
|
31 |
</dependency> |
|
32 |
<dependency> |
|
28 | 33 |
<groupId>org.gvsig</groupId> |
29 | 34 |
<artifactId>org.gvsig.fmap.geometry.jts</artifactId> |
30 | 35 |
<scope>test</scope> |
31 | 36 |
</dependency> |
37 |
<dependency> |
|
38 |
<groupId>org.gvsig</groupId> |
|
39 |
<artifactId>org.gvsig.fmap.geometry.jts</artifactId> |
|
40 |
<scope>test</scope> |
|
41 |
</dependency> |
|
42 |
<dependency> |
|
43 |
<groupId>org.gvsig</groupId> |
|
44 |
<artifactId>org.gvsig.proj.lib.proj4j</artifactId> |
|
45 |
<scope>test</scope> |
|
46 |
</dependency> |
|
32 | 47 |
</dependencies> |
33 | 48 |
</project> |
Also available in: Unified diff