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=HEAD;hp=f679762cb8998f221d0f9b7fe44a8608599b9248;hpb=c53dbb24511e2920c610caed7b65c3d92b4d76e8;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 f679762c..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,28 +1,26 @@ package org.simantics.maps.elevation.server; -import java.awt.geom.Point2D; 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.geometry.TransformedDirectPosition; +import org.geotools.referencing.CRS; import org.opengis.geometry.DirectPosition; -import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; -import org.opengis.referencing.operation.TransformException; +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; @@ -36,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); @@ -51,12 +48,9 @@ public class TiffInterface { ensureInit(); Envelope2D e = coverage.getEnvelope2D(); try { - TransformedDirectPosition tdp = new TransformedDirectPosition(pos.getCoordinateReferenceSystem(), crs, null); - tdp.transform(pos); - - Point2D p = tdp.toPoint2D(); - - boolean contains = e.contains(p); + 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(); @@ -73,21 +67,21 @@ public class TiffInterface { 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; } @@ -108,6 +102,6 @@ public class TiffInterface { } public CoordinateReferenceSystem getCRS() { - return crs; + return coverage.getCoordinateReferenceSystem(); } }