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;
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);
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) {
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;
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();
}
}
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;
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);
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;
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;
});
}
- private Map<Path, Envelope> envelopes = new ConcurrentHashMap<>();
-
public Collection<Rectangle2D> getBoundingBoxes() {
Collection<Rectangle2D> rects = envelopes.values().stream().map(env -> {
double x = env.getMinX();
}
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
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() {
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);
}