Skip to content

Commit

Permalink
Merge pull request #1659 from lat-lon/closeGeoTiffStreamsAddedClearPo…
Browse files Browse the repository at this point in the history
…ol-XPLANBOX-2608

Improves resource handling and added possibility to close streams in pools
  • Loading branch information
copierrj authored Mar 6, 2024
2 parents 99b6f46 + 839100b commit e6acf35
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ void shutdown() {
}
}

public void removeAndClose(File file) throws IOException {
pool.removeAndClose(file.getCanonicalFile().toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
----------------------------------------------------------------------------*/
package org.deegree.commons.gdal.pool;

import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.Closeable;
Expand Down Expand Up @@ -153,4 +154,12 @@ private synchronized BlockingQueue<T> getQueue(final String key) {
return queue;
}

public void removeAndClose(final String key) {
LOG.debug("Destroying resource, key: " + key);
BlockingQueue<T> queue = getQueue(key);
T resource = queue.poll();
closeQuietly(resource);
keyTracker.remove(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.deegree.tile.persistence.gdal;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.deegree.commons.gdal.GdalDataset;
Expand All @@ -47,7 +48,7 @@
* @author <a href="mailto:schneider@occamlabs.de">Markus Schneider</a>
* @since 3.4
*/
class GdalTileDataLevel implements TileDataLevel {
public class GdalTileDataLevel implements TileDataLevel {

private final TileMatrix metadata;

Expand Down Expand Up @@ -120,4 +121,8 @@ public List<String> getStyles() {
return null;
}

public void clearPool() throws IOException {
gdalSettings.getDatasetPool().removeAndClose(file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ public List<String> getStyles() {
return null;
}

public void clearPool() {
this.readerPool.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,20 @@ Map<String, TileDataSet> buildTileDataSetMap() throws IOException {
continue;
}

ImageInputStream iis = createImageInputStream(file);
reader.setInput(iis, false, true);
IIOMetadata md = reader.getImageMetadata(0);
Envelope envelope = getEnvelope(md, reader.getWidth(0), reader.getHeight(0), null);

if (envelope == null) {
throw new ResourceInitException(
"No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF.");
try (ImageInputStream iis = createImageInputStream(file)) {
reader.setInput(iis, false, true);
IIOMetadata md = reader.getImageMetadata(0);
Envelope envelope = getEnvelope(md, reader.getWidth(0), reader.getHeight(0), null);
reader.dispose();

if (envelope == null) {
throw new ResourceInitException(
"No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF.");
}
LOG.debug("Envelope from GeoTIFF was {}.", envelope);

map.put(id, builder.buildTileDataSet(tds, location, envelope));
}

LOG.debug("Envelope from GeoTIFF was {}.", envelope);

map.put(id, builder.buildTileDataSet(tds, location, envelope));
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ public ImageReaderFactory(File file) {

@Override
public void destroyObject(PooledObject<ImageReader> pooledObject) throws Exception {
ImageReader reader = (ImageReader) pooledObject;
reader.dispose();
ImageReader reader = pooledObject.getObject();
try (ImageInputStream input = (ImageInputStream) reader.getInput()) {
reader.dispose();
}
}

@Override
Expand All @@ -95,16 +97,15 @@ public void passivateObject(PooledObject<ImageReader> pooledObject) throws Excep

@Override
public PooledObject<ImageReader> makeObject() throws Exception {
ImageInputStream iis = null;
ImageReader reader = null;
Iterator<ImageReader> readers = getImageReadersBySuffix("tiff");
while (readers.hasNext() && !(reader instanceof TIFFImageReader)) {
reader = readers.next();
}
iis = createImageInputStream(file);
ImageInputStream iis = createImageInputStream(file);
// already checked in provider
reader.setInput(iis);
return new DefaultPooledObject<ImageReader>(reader);
return new DefaultPooledObject<>(reader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public GeoTiffTileMatrixSetBuilder(GeoTIFFTileMatrixSetConfig cfg, ResourceMetad

@Override
public TileMatrixSet build() {
ImageReader reader = null;
ImageInputStream iis = null;

try {
ICRS crs = null;
Expand All @@ -99,6 +97,7 @@ public TileMatrixSet build() {

ImageIO.scanForPlugins();

ImageReader reader = null;
Iterator<ImageReader> readers = getImageReadersBySuffix("tiff");
while (readers.hasNext() && !(reader instanceof TIFFImageReader)) {
reader = readers.next();
Expand All @@ -110,42 +109,43 @@ public TileMatrixSet build() {
throw new ResourceInitException("The file " + file + " does not exist.");
}

iis = createImageInputStream(file);
// this is already checked in provider
reader.setInput(iis, false, true);
int num = reader.getNumImages(true);
IIOMetadata md = reader.getImageMetadata(0);
Envelope envelope = getEnvelopeAndCrs(md, reader.getWidth(0), reader.getHeight(0), crs);
try (ImageInputStream iis = createImageInputStream(file);) {
// this is already checked in provider
reader.setInput(iis, false, true);
int num = reader.getNumImages(true);
IIOMetadata md = reader.getImageMetadata(0);
Envelope envelope = getEnvelopeAndCrs(md, reader.getWidth(0), reader.getHeight(0), crs);

if (envelope == null) {
throw new ResourceInitException(
"No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF.");
}
if (envelope == null) {
throw new ResourceInitException(
"No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF.");
}

LOG.debug("Envelope from GeoTIFF was {}.", envelope);

SpatialMetadata smd = new SpatialMetadata(envelope, singletonList(envelope.getCoordinateSystem()));

List<TileMatrix> matrices = new ArrayList<TileMatrix>();

for (int i = 0; i < num; ++i) {
int tw = reader.getTileWidth(i);
int th = reader.getTileHeight(i);
int width = reader.getWidth(i);
int height = reader.getHeight(i);
int numx = (int) Math.ceil((double) width / (double) tw);
int numy = (int) Math.ceil((double) height / (double) th);
double res = Math.max(envelope.getSpan0() / width, envelope.getSpan1() / height);
String id = Double.toString(res / DEFAULT_PIXEL_SIZE);
TileMatrix tmd = new TileMatrix(id, smd, BigInteger.valueOf(tw), BigInteger.valueOf(th), res,
BigInteger.valueOf(numx), BigInteger.valueOf(numy));
matrices.add(tmd);
LOG.debug("Level {} has {}x{} tiles of {}x{} pixels, resolution is {}",
new Object[] { i, numx, numy, tw, th, res });
LOG.debug("Envelope from GeoTIFF was {}.", envelope);

SpatialMetadata smd = new SpatialMetadata(envelope, singletonList(envelope.getCoordinateSystem()));

List<TileMatrix> matrices = new ArrayList<TileMatrix>();

for (int i = 0; i < num; ++i) {
int tw = reader.getTileWidth(i);
int th = reader.getTileHeight(i);
int width = reader.getWidth(i);
int height = reader.getHeight(i);
int numx = (int) Math.ceil((double) width / (double) tw);
int numy = (int) Math.ceil((double) height / (double) th);
double res = Math.max(envelope.getSpan0() / width, envelope.getSpan1() / height);
String id = Double.toString(res / DEFAULT_PIXEL_SIZE);
TileMatrix tmd = new TileMatrix(id, smd, BigInteger.valueOf(tw), BigInteger.valueOf(th), res,
BigInteger.valueOf(numx), BigInteger.valueOf(numy));
matrices.add(tmd);
LOG.debug("Level {} has {}x{} tiles of {}x{} pixels, resolution is {}",
new Object[] { i, numx, numy, tw, th, res });
}
reader.dispose();
return new TileMatrixSet(file.getName().substring(0, file.getName().length() - 4), null, matrices, smd,
metadata);
}

return new TileMatrixSet(file.getName().substring(0, file.getName().length() - 4), null, matrices, smd,
metadata);
}
catch (Exception e) {
throw new ResourceInitException("Could not create tile matrix set. Reason: " + e.getLocalizedMessage(), e);
Expand Down

0 comments on commit e6acf35

Please sign in to comment.