]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Move tif folder to preferences for elevation server 69/3069/2
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 8 Aug 2019 08:41:35 +0000 (11:41 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Thu, 8 Aug 2019 08:41:56 +0000 (08:41 +0000)
gitlab #45
APROS-15524

Change-Id: Iffbb4e318f1a3b0b868aab9d0e11211530b55c3d

org.simantics.maps.elevation.server.ui/src/org/simantics/maps/elevation/server/ui/MapsElevationServerPreferencePage.java
org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/SingletonTiffTileInterface.java
org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java
org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java
org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/prefs/MapsElevationServerPreferences.java

index a014c315aa02c094d4139f650233ea807d4fa443..366ca0312303a08e566c71b61d3c9054037c534a 100644 (file)
@@ -7,11 +7,13 @@ import org.eclipse.jface.preference.BooleanFieldEditor;
 import org.eclipse.jface.preference.FieldEditorPreferencePage;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.preference.IntegerFieldEditor;
+import org.eclipse.jface.preference.StringFieldEditor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Group;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.simantics.maps.elevation.server.SingletonTiffTileInterface;
 import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,6 +40,9 @@ public class MapsElevationServerPreferencePage extends FieldEditorPreferencePage
         BooleanFieldEditor automatically = new BooleanFieldEditor(MapsElevationServerPreferences.P_USE_ELEVATION_SERVER, "Use elevation server", serverGroup);
         addField(automatically);
 
+        StringFieldEditor tifFolderPath = new StringFieldEditor(MapsElevationServerPreferences.P_TIFF_FOLDER_PATH, "Tiff folder path", serverGroup);
+        addField(tifFolderPath);
+        
         IntegerFieldEditor pipeDepth = new IntegerFieldEditor(MapsElevationServerPreferences.P_PIPE_DEPTH_UNDER_GROUND, "Pipe depth under ground", serverGroup);
         pipeDepth.setValidRange(Integer.MIN_VALUE, Integer.MAX_VALUE);
         addField(pipeDepth);
@@ -55,6 +60,21 @@ public class MapsElevationServerPreferencePage extends FieldEditorPreferencePage
     protected void performApply() {
         super.performApply();
     }
+    
+    @Override
+    public boolean performOk() {
+        boolean ok = super.performOk();
+        if (ok) {
+            try {
+                SingletonTiffTileInterface.reloadElevationServer();
+            } catch (Exception e) {
+                LOGGER.error("Could not reload elevation server", e);
+                setErrorMessage(e.getMessage());
+                return false;
+            }
+        }
+        return ok;
+    }
 
     @Override
     public void init(IWorkbench workbench) {
index a8163bb200b3b8c2570ba2408ddbbdddaf65e307..ced1c16bc2c567f96782edbde78c184acfdae844 100644 (file)
@@ -7,6 +7,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Collection;
 
+import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -14,40 +15,59 @@ public class SingletonTiffTileInterface {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(SingletonTiffTileInterface.class);
 
-    private static final String TIFS_FOLDER = System.getProperty("tifsFolder");
-
-    private static SingletonTiffTileInterface instance = new SingletonTiffTileInterface();
-
+    private static SingletonTiffTileInterface instance;
     private TiffTileInterface tileInterface;
 
-    private SingletonTiffTileInterface() {
+    static {
         // register spis
         Activator.registerSpis();
-        Path tilesFolder = Paths.get("tifsFolder");
-        if (TIFS_FOLDER != null) {
-            Path potTilesFolder = Paths.get(TIFS_FOLDER);
-            if (Files.exists(potTilesFolder)) {
-                // use default from working directory
-                tilesFolder = potTilesFolder;
-            }
-        }
+    }
+    
+    private SingletonTiffTileInterface() {
+        Path tilesFolder = Paths.get(MapsElevationServerPreferences.tifFolderPath());
         try {
             Files.createDirectories(tilesFolder);
         } catch (IOException e) {
             LOGGER.error("Could not create directories {}", tilesFolder.toAbsolutePath(), e);
         }
+        LOGGER.info("Elevation server looking for tif files at {}", tilesFolder.toAbsolutePath());
         tileInterface = new TiffTileInterface(tilesFolder);
     }
 
+    private static SingletonTiffTileInterface getInstance() {
+        if (instance == null ) {
+            synchronized (SingletonTiffTileInterface.class) {
+                if (instance == null) {
+                    instance = new SingletonTiffTileInterface();
+                }
+            }
+        }
+        return instance;
+    }
+
+    public static synchronized void reloadElevationServer() {
+        if (instance != null) {
+            try {
+                instance.tileInterface.close();
+            } catch (IOException e) {
+                LOGGER.error("Could not close current elevation server interface", e);
+            } finally {
+                instance = null;
+            }
+        }
+        // let's re-initialize
+        getInstance();
+    }
+    
     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);
+        return getInstance().tileInterface.lookup(x, y);
     }
 
     public static Collection<Rectangle2D> getBoundingBoxes() {
-        return instance.tileInterface.getBoundingBoxes();
+        return getInstance().tileInterface.getBoundingBoxes();
     }
 }
index 60d7e5650a0c20e3addfdb195427922fe9508ee0..f5935318348612517d6a6db3317e7b9b325ce934 100644 (file)
@@ -1,6 +1,7 @@
 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;
@@ -14,7 +15,7 @@ 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);
 
index 6f24ad5b65073eec799ac66247c90180d14b9f4e..8f7ecc010d285e5ba41d55728fc2fc0e904668ed 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;
@@ -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;
@@ -94,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();
@@ -133,4 +133,15 @@ public class TiffTileInterface {
         }
         return new Double(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
index 8db89308a0e4ff1fa2cc981a31e73d2da6fbc46a..bcb59f2c9d01631cbaa586c7fd471434c1d79350 100644 (file)
@@ -9,6 +9,7 @@ public class MapsElevationServerPreferences {
     public static final String P_NODE = Activator.PLUGIN_ID;
 
     public static final String P_USE_ELEVATION_SERVER = "org.simantics.maps.elevation.server.useElevationServer";
+    public static final String P_TIFF_FOLDER_PATH = "org.simantics.maps.elevation.server.tifsFolderPath";
     public static final String P_PIPE_DEPTH_UNDER_GROUND = "org.simantics.maps.elevation.server.pipeDepthUnderGround";
 
     public static Preferences getPreferences() {
@@ -19,6 +20,10 @@ public class MapsElevationServerPreferences {
         return getPreferences().getBoolean(P_USE_ELEVATION_SERVER, false);
     }
 
+    public static String tifFolderPath() {
+        return getPreferences().get(P_TIFF_FOLDER_PATH, "tifsFolder");
+    }
+    
     public static int pipeDepthUnderGround() {
        return getPreferences().getInt(P_PIPE_DEPTH_UNDER_GROUND, -1);
     }