-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GeoTiffReaderCogOnlineTest.java
137 lines (123 loc) · 5.9 KB
/
GeoTiffReaderCogOnlineTest.java
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
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2020, Open Source Geospatial Foundation (OSGeo)
*
* 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;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
package org.geotools.gce.geotiff;
import it.geosolutions.imageio.core.BasicAuthURI;
import it.geosolutions.imageio.plugins.cog.CogImageReadParam;
import it.geosolutions.imageioimpl.plugins.cog.CogImageInputStreamSpi;
import it.geosolutions.imageioimpl.plugins.cog.CogImageReader;
import it.geosolutions.imageioimpl.plugins.cog.CogImageReaderSpi;
import it.geosolutions.imageioimpl.plugins.cog.CogSourceSPIProvider;
import it.geosolutions.imageioimpl.plugins.cog.DefaultCogImageInputStream;
import it.geosolutions.imageioimpl.plugins.cog.HttpRangeReader;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.net.URISyntaxException;
import org.geotools.api.parameter.GeneralParameterValue;
import org.geotools.api.parameter.ParameterValue;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridEnvelope2D;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.geometry.GeneralBounds;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
/** Testing {@link GeoTiffReader} with COG data */
public class GeoTiffReaderCogOnlineTest extends Assert {
private CogSourceSPIProvider getInputProvider() {
String url =
"https://s3-us-west-2.amazonaws.com/sentinel-cogs/sentinel-s2-l2a-cogs/5/C/MK/2018/10/S2B_5CMK_20181020_0_L2A/B01.tif";
BasicAuthURI cogUri = new BasicAuthURI(url, false);
HttpRangeReader rangeReader =
new HttpRangeReader(cogUri.getUri(), CogImageReadParam.DEFAULT_HEADER_LENGTH);
CogSourceSPIProvider input =
new CogSourceSPIProvider(
cogUri,
new CogImageReaderSpi(),
new CogImageInputStreamSpi(),
rangeReader.getClass().getName());
return input;
}
@Test
public void testCogRead() throws URISyntaxException, IOException {
GeoTiffReader reader = new GeoTiffReader(getInputProvider());
assertEquals("B01", reader.getGridCoverageNames()[0]);
GridCoverage2D coverage = reader.read(null);
assertNotNull(coverage);
RenderedImage image = coverage.getRenderedImage();
int numTileX = image.getNumXTiles();
int numTileY = image.getNumYTiles();
Raster raster = image.getTile(numTileX / 2, numTileY / 2);
assertEquals(256, raster.getWidth());
assertEquals(256, raster.getHeight());
assertEquals(1, raster.getNumBands());
}
@Test
public void testCogReadBandSelect() throws URISyntaxException, IOException {
GeoTiffReader reader = new GeoTiffReader(getInputProvider());
ParameterValue<int[]> bands = AbstractGridFormat.BANDS.createValue();
bands.setValue(new int[] {0, 0});
GridCoverage2D coverage = reader.read(new GeneralParameterValue[] {bands});
assertNotNull(coverage);
assertEquals(2, coverage.getRenderedImage().getSampleModel().getNumBands());
assertEquals(2, coverage.getRenderedImage().getColorModel().getNumComponents());
}
// the old cog file used to have a landsat-pds scene with a sidecar .ovr file that is now gone
@Test
@Ignore
public void testCogOverview() throws URISyntaxException, IOException {
GeoTiffReader reader = new GeoTiffReader(getInputProvider());
assertEquals("B01", reader.getGridCoverageNames()[0]);
GeneralParameterValue[] params = new GeneralParameterValue[1];
// Define a GridGeometry in order to reduce the output
final ParameterValue<GridGeometry2D> gg =
AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue();
final GeneralBounds envelope = reader.getOriginalEnvelope();
final Dimension dim = new Dimension();
dim.setSize(
reader.getOriginalGridRange().getSpan(0) / 24,
reader.getOriginalGridRange().getSpan(1) / 24);
final Rectangle rasterArea = ((GridEnvelope2D) reader.getOriginalGridRange());
rasterArea.setSize(dim);
final GridEnvelope2D range = new GridEnvelope2D(rasterArea);
gg.setValue(new GridGeometry2D(range, envelope));
params[0] = gg;
GridCoverage2D coverage = reader.read(params);
assertNotNull(coverage);
RenderedImage image = coverage.getRenderedImage();
int numTileX = image.getNumXTiles();
int numTileY = image.getNumYTiles();
CogImageReader imageReader = (CogImageReader) image.getProperty("JAI.ImageReader");
Object input = imageReader.getInput();
assertTrue(input instanceof DefaultCogImageInputStream);
@SuppressWarnings("PMD.CloseResource") // closed elsewhere
DefaultCogImageInputStream inputStream = (DefaultCogImageInputStream) input;
String url = inputStream.getUrl();
// The reader is using the .ovr file as input, it's using the overviews
assertTrue(url.endsWith(".ovr"));
// Reading the whole bbox resulted in only 1 tile:
// It's using the overview
assertEquals(1, numTileX);
assertEquals(1, numTileY);
Raster raster = image.getTile(numTileX / 2, numTileY / 2);
assertEquals(1, raster.getNumBands());
}
}