Skip to content

Commit e9bbcef

Browse files
author
taylor.smock
committed
Fix #24097: Zoom to imagery layer
This fixes two issues: 1. Adds implementation for `visitBoundingBox` used by the `Zoom to layer` action 2. Uses `addLayer(Layer, boolean)` to avoid zooming to the bounds of the layer on layer add Also, clean up some deprecation warnings. git-svn-id: https://josm.openstreetmap.de/svn/trunk@19289 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent c56f6ba commit e9bbcef

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void actionPerformed(ActionEvent e) {
168168
final ImageryInfo infoToAdd = convertImagery(info);
169169
if (infoToAdd != null) {
170170
layer = ImageryLayer.create(infoToAdd);
171-
getLayerManager().addLayer(layer);
171+
getLayerManager().addLayer(layer, false);
172172
AlignImageryPanel.addNagPanelIfNeeded(infoToAdd);
173173
}
174174
} catch (IllegalArgumentException | ReportedException ex) {

src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
import java.io.File;
2727
import java.io.IOException;
2828
import java.net.MalformedURLException;
29-
import java.net.URL;
29+
import java.net.URI;
30+
import java.net.URISyntaxException;
3031
import java.time.Instant;
3132
import java.util.ArrayList;
3233
import java.util.Arrays;
@@ -291,10 +292,10 @@ protected void initTileSource(T tileSource) {
291292
tileLoader = getTileLoaderFactory().makeTileLoader(this, headers, minimumTileExpire);
292293

293294
try {
294-
if ("file".equalsIgnoreCase(new URL(tileSource.getBaseUrl()).getProtocol())) {
295+
if ("file".equalsIgnoreCase(new URI(tileSource.getBaseUrl()).toURL().getProtocol())) {
295296
tileLoader = new OsmTileLoader(this);
296297
}
297-
} catch (MalformedURLException e) {
298+
} catch (URISyntaxException | MalformedURLException e) {
298299
// ignore, assume that this is not a file
299300
Logging.log(Logging.LEVEL_DEBUG, e);
300301
}
@@ -514,9 +515,9 @@ public void actionPerformed(ActionEvent ae) {
514515
private static void sendOsmTileRequest(Tile tile, String request) {
515516
if (tile != null) {
516517
try {
517-
new Notification(HttpClient.create(new URL(tile.getUrl() + '/' + request))
518+
new Notification(HttpClient.create(new URI(tile.getUrl() + '/' + request).toURL())
518519
.connect().fetchContent()).setIcon(JOptionPane.INFORMATION_MESSAGE).show();
519-
} catch (IOException ex) {
520+
} catch (URISyntaxException | IOException ex) {
520521
Logging.error(ex);
521522
}
522523
}
@@ -1873,6 +1874,9 @@ public String getToolTipText() {
18731874

18741875
@Override
18751876
public void visitBoundingBox(BoundingXYVisitor v) {
1877+
if (this.getInfo() != null) {
1878+
v.visit(this.getInfo().getBounds());
1879+
}
18761880
}
18771881

18781882
/**

test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77
import static org.junit.jupiter.api.Assertions.assertEquals;
88
import static org.junit.jupiter.api.Assertions.assertTrue;
99

10+
import java.util.Arrays;
1011
import java.util.List;
1112

1213
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
1314
import org.junit.jupiter.api.Test;
1415
import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
16+
import org.openstreetmap.josm.data.Bounds;
1517
import org.openstreetmap.josm.data.imagery.ImageryInfo;
18+
import org.openstreetmap.josm.data.osm.DataSet;
19+
import org.openstreetmap.josm.data.projection.ProjectionRegistry;
1620
import org.openstreetmap.josm.gui.MainApplication;
21+
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
1722
import org.openstreetmap.josm.gui.layer.TMSLayer;
1823
import org.openstreetmap.josm.gui.layer.WMSLayer;
24+
import org.openstreetmap.josm.gui.util.GuiHelper;
1925
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
2026
import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
27+
import org.openstreetmap.josm.testutils.annotations.Main;
2128
import org.openstreetmap.josm.testutils.annotations.OsmApi;
2229
import org.openstreetmap.josm.testutils.annotations.Projection;
2330

@@ -98,4 +105,44 @@ void testActionPerformedDisabled() {
98105
}
99106
assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
100107
}
108+
109+
110+
/**
111+
* Non-regression test for <a href="https://josm.openstreetmap.de/ticket/24097">#24097</a>: Zoom to imagery layer
112+
* This tests two things:
113+
* <ul>
114+
* <li>Imagery layer zoom to action works properly</li>
115+
* <li>Imagery layer bounds is not zoomed to on layer add</li>
116+
* </ul>
117+
*/
118+
@Main
119+
@Test
120+
void testNonRegression24097() {
121+
// First, add a new data layer
122+
MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(),
123+
"AddImageryLayerActionTest#testNonRegression24097", null));
124+
// Now zoom to a random area
125+
MainApplication.getMap().mapView.zoomTo(new Bounds(39.0665807, -108.5212326, 39.0793079, -108.4986591));
126+
// Initialize the zoom actions
127+
MainApplication.getMenu().initialize();
128+
final Bounds startingBounds = MainApplication.getMap().mapView.getRealBounds();
129+
ImageryInfo testInfo = new ImageryInfo("Test", "https://127.0.0.1/{zoom}/{x}/{y}.png", "tms", null, null, "Test");
130+
testInfo.setBounds(new ImageryInfo.ImageryBounds("-0.001,-0.001,0.001,0.001", ","));
131+
new AddImageryLayerAction(testInfo).actionPerformed(null);
132+
GuiHelper.runInEDTAndWait(() -> { /* Sync GUI thread */ });
133+
// There is a bit of zooming done during the load of the imagery
134+
assertTrue(startingBounds.toBBox().bboxIsFunctionallyEqual(MainApplication.getMap().mapView.getRealBounds().toBBox(), 0.001),
135+
"Adding an imagery layer should not zoom to the imagery layer bounds");
136+
assertEquals(1, MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).size());
137+
final TMSLayer tmsLayer = MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).get(0);
138+
final AutoScaleAction autoScaleAction = Arrays.stream(tmsLayer.getMenuEntries()).filter(AutoScaleAction.class::isInstance)
139+
.map(AutoScaleAction.class::cast).findFirst().orElseThrow();
140+
autoScaleAction.actionPerformed(null);
141+
// We can't check the bbox here, since the mapView doesn't have any actual width/height.
142+
// So we just check the center.
143+
assertTrue(new Bounds(-0.001, -0.001, 0.001, 0.001)
144+
.contains(ProjectionRegistry.getProjection().eastNorth2latlon(
145+
MainApplication.getMap().mapView.getCenter())),
146+
"The action should have zoomed to the bbox for the imagery layer");
147+
}
101148
}

0 commit comments

Comments
 (0)