]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java
Fixed most warnings from district codebase after JavaSE-11 switch
[simantics/district.git] / org.simantics.maps.elevation.server / src / org / simantics / maps / elevation / server / TiffTileInterface.java
index b85f234c72d935ef4121877e8d9cc299d73b3fed..6cc9cd7b79e0c544f01f14f02c7c23563ef50927 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.maps.elevation.server;
 
 import java.awt.geom.Rectangle2D;
+import java.io.Closeable;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -14,10 +15,10 @@ import java.util.stream.Stream;
 
 import org.geotools.geometry.DirectPosition2D;
 import org.geotools.geometry.Envelope2D;
+import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.referencing.CRS;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
-import org.opengis.referencing.operation.MathTransform;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -27,11 +28,12 @@ import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Envelope;
 import com.vividsolutions.jts.index.strtree.STRtree;
 
-public class TiffTileInterface {
+public class TiffTileInterface implements Closeable {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(TiffTileInterface.class);
 
     private Path tilesFolder;
+    private Map<Path, Envelope> envelopes = new ConcurrentHashMap<>();
     private LoadingCache<Path, TiffInterface> interfaceCache;
     private int openInterfacesSize;
     private STRtree index;
@@ -75,16 +77,13 @@ public class TiffTileInterface {
             TiffInterface tifInterface = openTifInterface(tifFile);
             Envelope2D coords = tifInterface.getCornerCoords();
             try {
-                MathTransform transform = CRS.findMathTransform(tifInterface.getCRS(), c4326);
-                DirectPosition2D min = new DirectPosition2D();
-                DirectPosition2D max = new DirectPosition2D();
-                transform.transform(new DirectPosition2D(coords.getMinX(), coords.getMinY()), min);
-                transform.transform(new DirectPosition2D(coords.getMaxX(), coords.getMaxY()), max);
-                Envelope envelope = new Envelope(min.getX(), max.getX(), min.getY(), max.getY());
+                ReferencedEnvelope refEnv = new ReferencedEnvelope(coords);
+                ReferencedEnvelope targetEnv = refEnv.transform(c4326, false, 30);
+
                 synchronized(index) {
-                    index.insert(envelope, tifFile);
+                    index.insert(targetEnv, tifFile);
                 }
-                envelopes.put(tifFile, envelope);
+                envelopes.put(tifFile, targetEnv);
             } catch (Exception e) {
                 LOGGER.error("Could not initialize index for file {}", tifFile, e);
             } finally {
@@ -97,8 +96,6 @@ public class TiffTileInterface {
         });
     }
 
-    private Map<Path, Envelope> envelopes = new ConcurrentHashMap<>();
-
     public Collection<Rectangle2D> getBoundingBoxes() {
         Collection<Rectangle2D> rects = envelopes.values().stream().map(env -> {
             double x = env.getMinX();
@@ -114,13 +111,14 @@ public class TiffTileInterface {
         try {
             c4326 = CRS.decode("EPSG:4326");
         } catch (Exception e) {
-            LOGGER.error("Could not initialize epsg:4326", e);
+            LOGGER.error("Could not initialize EPSG:4326", e);
         }
     }
 
     public Number lookup(double x, double y) {
-        LOGGER.info("Looking up x={} y={}", x, y);
+        LOGGER.trace("Looking up x={} y={}", x, y);
         DirectPosition p = new DirectPosition2D(c4326, x, y);
+        @SuppressWarnings("unchecked")
         List<Path> tifFiles = (List<Path>) index.query(new Envelope(new Coordinate(x, y)));
         for (Path tifFile : tifFiles) {
             TiffInterface tifInterface = openTifInterface(tifFile);
@@ -131,9 +129,20 @@ public class TiffTileInterface {
                     tifInterface.close();
                 }
             } else {
-                System.out.println("not found");
+                //System.out.println("not found");
             }
         }
-        return new Double(0); // use 0 by default for now
+        return Double.valueOf(0); // use 0 by default for now
+    }
+
+    @Override
+    public void close() throws IOException {
+        interfaceCache.invalidateAll();
+        interfaceCache.cleanUp();
+        
+        envelopes.clear();
+        envelopes = null;
+        index = null;
+        interfaceCache = null;
     }
 }
\ No newline at end of file