Ticket #8945: 8945-note.patch

File 8945-note.patch, 7.2 KB (added by AlfonZ, 12 years ago)

Fix with (updated) test

  • src/org/openstreetmap/josm/tools/OsmUrlToBounds.java

     
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    22package org.openstreetmap.josm.tools;
    33
     4import java.awt.HeadlessException;
    45import java.awt.Toolkit;
    56import java.io.UnsupportedEncodingException;
    67import java.net.URLDecoder;
     
    2627        Bounds b = parseShortLink(url);
    2728        if (b != null)
    2829            return b;
    29         int i = url.indexOf('?');
     30        int i = url.indexOf("#map");
     31        if (i >= 0) {
     32            // probably it's a URL following the new scheme?
     33            return parseHashURLs(url);
     34        }
     35        i = url.indexOf('?');
    3036        if (i == -1) {
    31             //probably it's a URL following the new scheme?
    32             if (url.indexOf('#') >= 0)
    33                 return parseHashURLs(url);
    34             else
    35                 return null;
     37            return null;
    3638        }
    3739        String[] args = url.substring(i+1).split("&");
    3840        HashMap<String, String> map = new HashMap<String, String>();
     
    7981     * Openstreetmap.org changed it's URL scheme in August 2013, which breaks the URL parsing.
    8082     * The following function, called by the old parse function if necessary, provides parsing new URLs
    8183     * the new URLs follow the scheme http://www.openstreetmap.org/#map=18/51.71873/8.76164&layers=CN
    82      * @param url
    83      * @return
     84     * @param url string for parsing
     85     * @return Bounds if hashurl, {@code null} otherwise
    8486     */
    8587    private static Bounds parseHashURLs(String url) {
    86         int startIndex = url.indexOf("=");
     88        int startIndex = url.indexOf("#map=");
    8789        if (startIndex == -1) return null;
    88         int endIndex = url.indexOf("&");
     90        int endIndex = url.indexOf('&', startIndex);
    8991        if (endIndex == -1) endIndex = url.length();
    9092        try
    9193        {
    92             String coordPart = url.substring(startIndex+1, endIndex);
     94            String coordPart = url.substring(startIndex+5, endIndex);
    9395            String[] parts = coordPart.split("/");
    9496            Bounds b = positionToBounds(Double.parseDouble(parts[1]),
    9597                    Double.parseDouble(parts[2]),
     
    180182
    181183    public static Bounds positionToBounds(final double lat, final double lon, final int zoom) {
    182184        int tileSizeInPixels = 256;
    183         int height = Toolkit.getDefaultToolkit().getScreenSize().height;
    184         int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    185         if (Main.isDisplayingMapView()) {
    186             height = Main.map.mapView.getHeight();
    187             width = Main.map.mapView.getWidth();
     185        int height;
     186        int width;
     187        try {
     188            height = Toolkit.getDefaultToolkit().getScreenSize().height;
     189            width = Toolkit.getDefaultToolkit().getScreenSize().width;
     190            if (Main.isDisplayingMapView()) {
     191                height = Main.map.mapView.getHeight();
     192                width = Main.map.mapView.getWidth();
     193            }
     194        } catch (HeadlessException he) {
     195            // TODO: put these dimensions somewhere appropriate
     196            height = 480;
     197            width = 640;
    188198        }
    189199        double scale = (1 << zoom) * tileSizeInPixels / (2 * Math.PI * R);
    190200        double deltaX = width / 2.0 / scale;
  • test/unit/org/openstreetmap/josm/tools/OsmUrlToBoundsTest.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.tools;
     3
     4import org.junit.Assert;
     5import org.junit.Test;
     6import org.openstreetmap.josm.data.Bounds;
     7
     8/**
     9  * Unit tests of {@link OsmUrlToBounds} class.
     10*/
     11public class OsmUrlToBoundsTest {
     12    /**
     13     * data for {@link #testParse}
     14     */
     15    private static final ParseTestItem[] parseTestData = {
     16        new ParseTestItem("http://www.openstreetmap.org", null),
     17        new ParseTestItem("http://www.openstreetmap.org/?bbox=-0.489,51.28,0.236,51.686", new Bounds(51.28, -0.489, 51.686, 0.236)),
     18        new ParseTestItem("http://www.openstreetmap.org/?minlon=-0.489&minlat=51.28&maxlon=0.236&maxlat=51.686", new Bounds(51.28, -0.489, 51.686, 0.236)),
     19        new ParseTestItem("http://www.openstreetmap.org/?maxlat=51.686&maxlon=0.236&minlat=51.28&minlon=-0.489", new Bounds(51.28, -0.489, 51.686, 0.236)),
     20        new ParseTestItem("http://www.openstreetmap.org/?zoom=17&lat=51.71873&lon=8.76164", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
     21        new ParseTestItem("http://www.openstreetmap.org/?lon=8.76164&lat=51.71873&zoom=17&foo", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
     22        new ParseTestItem("http://www.openstreetmap.org/?mlon=8.76164&mlat=51.71873", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 18)),
     23        new ParseTestItem("http://osm.org/go/euulwp", OsmUrlToBounds.positionToBounds(51.48262023925781, -0.29937744140625, 8)),
     24        new ParseTestItem("http://www.openstreetmap.org/#map=17/51.71873/8.76164", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
     25        new ParseTestItem("http://www.openstreetmap.org/#map=17/51.71873/8.76164&layers=CN", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
     26        new ParseTestItem("http%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D16%2F51.71873%2F8.76164", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 16)),
     27        new ParseTestItem("http%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D16%2F51.71873%2F8.76164%26layers%3DCN", OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 16)),
     28        new ParseTestItem("http://www.openstreetmap.org/?note=26325#map=18/40.86215/-75.75020", OsmUrlToBounds.positionToBounds(40.86215, -75.75020, 18)),
     29        new ParseTestItem("http://www.openstreetmap.org/?note=26325#map=18/40.86215/-75.75020&layers=N", OsmUrlToBounds.positionToBounds(40.86215, -75.75020, 18)),
     30        new ParseTestItem("http://www.openstreetmap.org/?mlat=51.5&mlon=-0.01#map=10/51.4831/-0.1270", OsmUrlToBounds.positionToBounds(51.4831, -0.1270, 10)),
     31        new ParseTestItem("http://www.openstreetmap.org/?mlat=51.5&mlon=-0.01#map=10/51.4831/-0.3509&layers=T", OsmUrlToBounds.positionToBounds(51.4831, -0.3509, 10)),
     32        new ParseTestItem("http://www.openstreetmap.org/#map", null),
     33        new ParseTestItem("http://www.openstreetmap.org/#map=foo", null),
     34        new ParseTestItem("http://www.openstreetmap.org/#map=fooz/foolat/foolon", null)
     35    };
     36
     37    private static class ParseTestItem {
     38        public String url;
     39        public Bounds bounds;
     40       
     41        public ParseTestItem(String url, Bounds bounds) {
     42            this.url = url;
     43            this.bounds = bounds;
     44        }
     45    }
     46
     47    /**
     48     * Test URL parsing
     49     */
     50    @Test
     51    public void testParse() {
     52        for (ParseTestItem item : parseTestData) {
     53            Bounds bounds = OsmUrlToBounds.parse(item.url);
     54            Assert.assertEquals(item.url, item.bounds, bounds);
     55        }
     56    }
     57
     58}