From: jsimomaa Date: Wed, 24 Jul 2019 06:51:14 +0000 (+0300) Subject: Add SCL interface for District Elevation server X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F43%2F3043%2F1;p=simantics%2Fdistrict.git Add SCL interface for District Elevation server gitlab #45 Change-Id: Icef31a8ac3280670f2a36a5354bea8c2dd3f8af7 --- diff --git a/org.simantics.maps.elevation.server/build.properties b/org.simantics.maps.elevation.server/build.properties index 34d2e4d2..a4fd10d4 100644 --- a/org.simantics.maps.elevation.server/build.properties +++ b/org.simantics.maps.elevation.server/build.properties @@ -1,4 +1,5 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + scl/ diff --git a/org.simantics.maps.elevation.server/scl/Simantics/District/Elevation.scl b/org.simantics.maps.elevation.server/scl/Simantics/District/Elevation.scl new file mode 100644 index 00000000..9edd6dac --- /dev/null +++ b/org.simantics.maps.elevation.server/scl/Simantics/District/Elevation.scl @@ -0,0 +1,4 @@ + +importJava "org.simantics.maps.elevation.server.SingletonTiffTileInterface" where + @JavaName lookupd + lookupElevation :: Double -> Double -> Double \ No newline at end of file diff --git a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/SingletonTiffTileInterface.java b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/SingletonTiffTileInterface.java index 3fc9616c..a8163bb2 100644 --- a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/SingletonTiffTileInterface.java +++ b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/SingletonTiffTileInterface.java @@ -26,7 +26,7 @@ public class SingletonTiffTileInterface { Path tilesFolder = Paths.get("tifsFolder"); if (TIFS_FOLDER != null) { Path potTilesFolder = Paths.get(TIFS_FOLDER); - if (!Files.exists(potTilesFolder)) { + if (Files.exists(potTilesFolder)) { // use default from working directory tilesFolder = potTilesFolder; } @@ -39,10 +39,14 @@ public class SingletonTiffTileInterface { tileInterface = new TiffTileInterface(tilesFolder); } + public static double lookupd(double x, double y) { + return lookup(x, y).doubleValue(); + } + public static Number lookup(double x, double y) { return instance.tileInterface.lookup(x, y); } - + public static Collection getBoundingBoxes() { return instance.tileInterface.getBoundingBoxes(); } 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 9e0a6514..f679762c 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,13 +1,17 @@ package org.simantics.maps.elevation.server; +import java.awt.geom.Point2D; import java.awt.image.DataBuffer; 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.opengis.geometry.DirectPosition; +import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.referencing.operation.TransformException; import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,6 +47,23 @@ public class TiffInterface { } } + public boolean contains(DirectPosition pos) { + 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); + return contains; + } catch (Exception ex) { + ex.printStackTrace(); + return false; + } + } + public Number lookup(DirectPosition pos) { ensureInit(); Object r = coverage.evaluate(pos); diff --git a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java index d6f1910e..b85f234c 100644 --- a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java +++ b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java @@ -121,16 +121,19 @@ public class TiffTileInterface { public Number lookup(double x, double y) { LOGGER.info("Looking up x={} y={}", x, y); DirectPosition p = new DirectPosition2D(c4326, x, y); - List tifFile = (List) index.query(new Envelope(new Coordinate(x, y))); - if (!tifFile.isEmpty()) { - TiffInterface tifInterface = openTifInterface(tifFile.get(0)); - try { - return tifInterface.lookup(p); - } finally { - tifInterface.close(); + List tifFiles = (List) index.query(new Envelope(new Coordinate(x, y))); + for (Path tifFile : tifFiles) { + TiffInterface tifInterface = openTifInterface(tifFile); + if (tifInterface.contains(p)) { + try { + return tifInterface.lookup(p); + } finally { + tifInterface.close(); + } + } else { + System.out.println("not found"); } - } else { - return new Double(0); // use 0 by default for now } + return new Double(0); // use 0 by default for now } } \ No newline at end of file