X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.maps%2Fsrc%2Forg%2Fsimantics%2Fmaps%2Fsg%2FMapNode.java;h=5bdd2555c7bb373b53a6c34789f13f92fd0ae688;hb=refs%2Fheads%2Fmaster;hp=f9c6abf9af2fee69d4871965ac006cec329c9d51;hpb=0e8411115bcfa632f880953c7e49c5389de28a31;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.maps/src/org/simantics/maps/sg/MapNode.java b/org.simantics.district.maps/src/org/simantics/maps/sg/MapNode.java index f9c6abf9..5bdd2555 100644 --- a/org.simantics.district.maps/src/org/simantics/maps/sg/MapNode.java +++ b/org.simantics.district.maps/src/org/simantics/maps/sg/MapNode.java @@ -25,6 +25,8 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.image.ImageObserver; import java.awt.image.VolatileImage; +import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -36,6 +38,8 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; +import org.simantics.maps.MapScalingTransform; import org.simantics.maps.WebService; import org.simantics.maps.osm.OSMTileProvider; import org.simantics.maps.pojo.TileJobQueue; @@ -60,8 +64,8 @@ public class MapNode extends G2DNode implements ITileListener { private static final Logger LOGGER = LoggerFactory.getLogger(MapNode.class); - private final double MAP_SCALE = 1; - private final int MAX_TILE_LEVEL = 19; + //private final double MAP_SCALE = 1; + //private final int MAX_TILE_LEVEL = 19; private final int TILE_PIXEL_SIZE = 256; private final int VIEWBOX_QUIET_TIME = 500; @@ -169,23 +173,31 @@ public class MapNode extends G2DNode implements ITileListener { } }; + private IPreferenceChangeListener listener; + public void init() { - // Construct WebService from client properties - String url; - if (MapsClientPreferences.useBuiltinServer()) - url = MapsClientPreferences.possibleBuiltinServerURL(); - else - url = MapsClientPreferences.tileserverURL(); - if (!url.endsWith("/")) - url = url + "/"; - try { - ITileProvider provider = new OSMTileProvider(new WebService(url), TILE_PIXEL_SIZE); + OSMTileProvider provider = new OSMTileProvider(new WebService(computeUrl()), TILE_PIXEL_SIZE); + + listener = event -> { + // if tiles or style change we want to flush the tile cache + if (MapsClientPreferences.P_CURRENT_MBTILES.equals(event.getKey()) || MapsClientPreferences.P_CURRENT_TM2STYLE.equals(event.getKey())) { + if (tileCache != null) { + tileCache.clear(); + } + try { + provider.setWebService(new WebService(computeUrl())); + } catch (MalformedURLException | URISyntaxException e) { + LOGGER.error("Unable to update WebService with new url", e); + } + } + }; + MapsClientPreferences.addPreferenceChangeListenerMapsServer(listener); // Try to load eclipse specific implementation of TileJobQueue, if it doesn't exist, fall back to pojo implementation try { Class proxyClass = (Class) Class.forName("org.simantics.maps.eclipse.TileJobQueue"); - job = (ITileJobQueue)proxyClass.newInstance(); + job = (ITileJobQueue)proxyClass.getDeclaredConstructor().newInstance(); } catch (ClassNotFoundException e1) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { @@ -218,6 +230,25 @@ public class MapNode extends G2DNode implements ITileListener { } } + private static String computeUrl() { + // Construct WebService from client properties + String url; + if (MapsClientPreferences.useBuiltinServer()) + url = MapsClientPreferences.possibleBuiltinServerURL(); + else + url = MapsClientPreferences.tileserverURL(); + if (!url.endsWith("/")) + url = url + "/"; + return url; + } + + @Override + public void cleanup() { + MapsClientPreferences.removePreferenceChangeListenerMapsServer(listener); + tileCache.clear(); + job.clear(); + } + @SyncField("enabled") public void setEnabled(Boolean enabled) { this.enabled = enabled; @@ -233,14 +264,16 @@ public class MapNode extends G2DNode implements ITileListener { AffineTransform ot = g2d.getTransform(); + //System.err.println("ot " + ot); g2d.transform(transform); AffineTransform tr = g2d.getTransform(); - + //System.err.println("tr " + tr); // Graphics2D g = (Graphics2D)g2d.create(); // AffineTransform tr = (AffineTransform)g.getTransform().clone(); double scaleX = Math.abs(tr.getScaleX()); + //System.err.println("scaleX : " + scaleX); double scaleY = Math.abs(tr.getScaleY()); if (scaleX <= 0 || scaleY <= 0) { // Make sure that we don't end up in an eternal loop below. @@ -255,20 +288,21 @@ public class MapNode extends G2DNode implements ITileListener { Rectangle2D b = (Rectangle2D)((Rectangle)g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS)).getBounds2D(); // getClipBounds is not accurate enough, use original clipbounds and scale here Rectangle2D viewbox = new Rectangle2D.Double(offsetX/scaleX, offsetY/scaleY, b.getWidth()/sp.getWidth(), b.getHeight()/sp.getHeight()); //g.getClipBounds(); - if (enabled) { - - double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE; - int level = 0; - double tileSize = 360 * MAP_SCALE*2; - while (level < MAX_TILE_LEVEL) { - double ratio = smallerViewboxDimension / tileSize; - if (ratio >= 0.85) { - break; - } - tileSize *= 0.5; - level++; - } + + int level = MapScalingTransform.zoomLevel(ot); + +// double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE; +// int level = 0; +// double tileSize = 360 * MAP_SCALE*2.5; +// while (level < MAX_TILE_LEVEL) { +// double ratio = smallerViewboxDimension / tileSize; +// if (ratio >= 0.85) { +// break; +// } +// tileSize *= 0.5; +// level++; +// } /* * To convert y-coordinates to map coordinates in ruler, use: * double val = (y-offsetY)/scaleY; @@ -281,26 +315,31 @@ public class MapNode extends G2DNode implements ITileListener { double miny = Math.min(360, Math.max(viewbox.getMinY()+180, 0)); double maxy = Math.min(360, Math.max(viewbox.getMaxY()+180, 0)); - + //System.err.println("minx " + minx + " maxx " + maxx + " miny " + miny + " maxy " + maxy); g2d.setTransform(new AffineTransform()); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); int levels = (1 << level); - + //System.err.println("level " + level); + //System.err.println("levels " + levels); // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames int left = (int)Math.floor( (minx + 180) / 360 * (1<= levels) continue; - for(int ty = top; ty <= bottom; ty++) { - if(ty < 0 || ty >= levels) continue; + //System.err.println("tileSize " + tsx); + for (int tx = left; tx <= right; tx++) { + if (tx < 0 || tx >= levels) + continue; + for (int ty = top; ty <= bottom; ty++) { + if (ty < 0 || ty >= levels) + continue; TileKey tile = new TileKey(level, tx, ty); - double y = (double)ty - (double)levels/2; // In level 0 we have only one tile - paintTile(tileCache, g2d, tr, tile, tx*tsx-180, y*tsx, tsx); + double y = (double) ty - (double) levels / 2; // In level 0 we have only one tile + + paintTile(tileCache, g2d, tr, tile, tx * tsx - 180, y * tsx, tsx); } } // g.dispose();