package org.simantics.maps.elevation.server;
import java.awt.image.DataBuffer;
+import java.io.Closeable;
import java.nio.file.Path;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Envelope2D;
+import org.geotools.referencing.CRS;
import org.opengis.geometry.DirectPosition;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.referencing.operation.MathTransform;
import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class TiffInterface {
+public class TiffInterface implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(TiffInterface.class);
private final Path tifPath;
private GridCoverage2D coverage;
- private CoordinateReferenceSystem crs;
private boolean init = false;
try {
reader = new GeoTiffReader(this.tifPath.toFile());
this.coverage = reader.read(null);
- this.crs = coverage.getCoordinateReferenceSystem();
this.init = true;
} catch (Exception e) {
LOGGER.error("Could not load {}", tifPath, e);
}
}
+ public boolean contains(DirectPosition pos) {
+ ensureInit();
+ Envelope2D e = coverage.getEnvelope2D();
+ try {
+ MathTransform transform = CRS.findMathTransform(pos.getCoordinateReferenceSystem(), getCRS(), false);
+ DirectPosition target = transform.transform(pos, null);
+ boolean contains = e.contains(target);
+ return contains;
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ return false;
+ }
+ }
+
public Number lookup(DirectPosition pos) {
ensureInit();
Object r = coverage.evaluate(pos);
case DataBuffer.TYPE_BYTE: {
// TODO: if the result is byte how does one subtract the pipeDepth form the value?
// Might not be even relevant with this use case
- return new Byte(((byte[]) r)[0]);
+ return Byte.valueOf(((byte[]) r)[0]);
}
case DataBuffer.TYPE_SHORT: // Fall through
case DataBuffer.TYPE_USHORT: // Fall through
case DataBuffer.TYPE_INT: {
int val = ((int[]) r)[0] - pipeDepthUnderGround;
- return new Integer(val);
+ return Integer.valueOf(val);
}
case DataBuffer.TYPE_FLOAT: {
float val = ((float[]) r)[0] - pipeDepthUnderGround;
- return new Float(val);
+ return Float.valueOf(val);
}
case DataBuffer.TYPE_DOUBLE: {
double val = ((double[]) r)[0] - pipeDepthUnderGround;
- return new Double(val);
+ return Double.valueOf(val);
}
default: return null;
}
}
public CoordinateReferenceSystem getCRS() {
- return crs;
+ return coverage.getCoordinateReferenceSystem();
}
}