X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.maps.elevation.server%2Fsrc%2Forg%2Fsimantics%2Fmaps%2Felevation%2Fserver%2FTiffInterface.java;h=c3a27a4f04e754aeddf1f96b89eea85ae02cdef9;hb=a995d5d46f03f25bd77fb3f6d8349839886b2ec2;hp=14485230b9b04c99a50ce4527fb914c231008a1c;hpb=c6ddbec532645f52583c1ab9691cfb802724fca7;p=simantics%2Fdistrict.git diff --git a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java index 14485230..c3a27a4f 100644 --- a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java +++ b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java @@ -1,23 +1,26 @@ 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; @@ -31,7 +34,6 @@ public class TiffInterface { 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); @@ -42,17 +44,45 @@ public class TiffInterface { } } + 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); final int dataType = coverage.getRenderedImage().getSampleModel().getDataType(); + int pipeDepthUnderGround = MapsElevationServerPreferences.pipeDepthUnderGround(); switch (dataType) { - case DataBuffer.TYPE_BYTE: return new Byte(((byte[]) r)[0]); + 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 Byte.valueOf(((byte[]) r)[0]); + } case DataBuffer.TYPE_SHORT: // Fall through case DataBuffer.TYPE_USHORT: // Fall through - case DataBuffer.TYPE_INT: return new Integer(((int[]) r)[0]); - case DataBuffer.TYPE_FLOAT: return new Float(((float[]) r)[0]); - case DataBuffer.TYPE_DOUBLE: return new Double(((double[]) r)[0]); + case DataBuffer.TYPE_INT: { + int val = ((int[]) r)[0] - pipeDepthUnderGround; + return Integer.valueOf(val); + } + case DataBuffer.TYPE_FLOAT: { + float val = ((float[]) r)[0] - pipeDepthUnderGround; + return Float.valueOf(val); + } + case DataBuffer.TYPE_DOUBLE: { + double val = ((double[]) r)[0] - pipeDepthUnderGround; + return Double.valueOf(val); + } default: return null; } } @@ -72,6 +102,6 @@ public class TiffInterface { } public CoordinateReferenceSystem getCRS() { - return crs; + return coverage.getCoordinateReferenceSystem(); } }