From: jsimomaa Date: Thu, 8 Aug 2019 08:41:35 +0000 (+0300) Subject: Move tif folder to preferences for elevation server X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=47748e479895b9e74d805411f26c1d02601bf360;p=simantics%2Fdistrict.git Move tif folder to preferences for elevation server gitlab #45 APROS-15524 Change-Id: Iffbb4e318f1a3b0b868aab9d0e11211530b55c3d --- diff --git a/org.simantics.maps.elevation.server.ui/src/org/simantics/maps/elevation/server/ui/MapsElevationServerPreferencePage.java b/org.simantics.maps.elevation.server.ui/src/org/simantics/maps/elevation/server/ui/MapsElevationServerPreferencePage.java index a014c315..366ca031 100644 --- a/org.simantics.maps.elevation.server.ui/src/org/simantics/maps/elevation/server/ui/MapsElevationServerPreferencePage.java +++ b/org.simantics.maps.elevation.server.ui/src/org/simantics/maps/elevation/server/ui/MapsElevationServerPreferencePage.java @@ -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) { 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 a8163bb2..ced1c16b 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 @@ -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 getBoundingBoxes() { - return instance.tileInterface.getBoundingBoxes(); + return getInstance().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 60d7e565..f5935318 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,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); 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 6f24ad5b..8f7ecc01 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 @@ -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 envelopes = new ConcurrentHashMap<>(); private LoadingCache interfaceCache; private int openInterfacesSize; private STRtree index; @@ -94,8 +96,6 @@ public class TiffTileInterface { }); } - private Map envelopes = new ConcurrentHashMap<>(); - public Collection getBoundingBoxes() { Collection 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 diff --git a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/prefs/MapsElevationServerPreferences.java b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/prefs/MapsElevationServerPreferences.java index 8db89308..bcb59f2c 100644 --- a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/prefs/MapsElevationServerPreferences.java +++ b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/prefs/MapsElevationServerPreferences.java @@ -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); }