Changeset 19289 in josm


Ignore:
Timestamp:
2025-01-24T14:05:46+01:00 (2 months ago)
Author:
taylor.smock
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

    r19050 r19289  
    169169            if (infoToAdd != null) {
    170170                layer = ImageryLayer.create(infoToAdd);
    171                 getLayerManager().addLayer(layer);
     171                getLayerManager().addLayer(layer, false);
    172172                AlignImageryPanel.addNagPanelIfNeeded(infoToAdd);
    173173            }
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

    r19280 r19289  
    2727import java.io.IOException;
    2828import java.net.MalformedURLException;
    29 import java.net.URL;
     29import java.net.URI;
     30import java.net.URISyntaxException;
    3031import java.time.Instant;
    3132import java.util.ArrayList;
     
    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);
     
    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            }
     
    18741875    @Override
    18751876    public void visitBoundingBox(BoundingXYVisitor v) {
     1877        if (this.getInfo() != null) {
     1878            v.visit(this.getInfo().getBounds());
     1879        }
    18761880    }
    18771881
  • trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java

    r19155 r19289  
    88import static org.junit.jupiter.api.Assertions.assertTrue;
    99
     10import java.util.Arrays;
    1011import java.util.List;
    1112
     
    1314import org.junit.jupiter.api.Test;
    1415import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     16import org.openstreetmap.josm.data.Bounds;
    1517import org.openstreetmap.josm.data.imagery.ImageryInfo;
     18import org.openstreetmap.josm.data.osm.DataSet;
     19import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    1620import org.openstreetmap.josm.gui.MainApplication;
     21import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1722import org.openstreetmap.josm.gui.layer.TMSLayer;
    1823import org.openstreetmap.josm.gui.layer.WMSLayer;
     24import org.openstreetmap.josm.gui.util.GuiHelper;
    1925import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    2026import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
     27import org.openstreetmap.josm.testutils.annotations.Main;
    2128import org.openstreetmap.josm.testutils.annotations.OsmApi;
    2229import org.openstreetmap.josm.testutils.annotations.Projection;
     
    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}
Note: See TracChangeset for help on using the changeset viewer.