Changeset 32069 in osm for applications/editors


Ignore:
Timestamp:
2016-02-15T14:58:49+01:00 (9 years ago)
Author:
floscher
Message:

[mapillary] Add URL-factory-methods to search for only blurred, commented images and those with recognized objects

Also removes trailing slash from some generated URLs to align with the API documentation.

Location:
applications/editors/josm/plugins/mapillary
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/io/download/MapillaryImageInfoDownloadThread.java

    r31843 r32069  
    4646  public void run() {
    4747    try (
    48       BufferedReader br = new BufferedReader(new InputStreamReader(MapillaryURL.searchImageURL(bounds, page).openStream(), "UTF-8"));
     48      BufferedReader br = new BufferedReader(new InputStreamReader(
     49          MapillaryURL.searchImageInfoURL(bounds, page, null).openStream(), "UTF-8"
     50      ));
    4951    ) {
    5052      JsonObject jsonobj = Json.createReader(br).readObject();
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/io/download/MapillarySequenceDownloadThread.java

    r32064 r32069  
    5353  public void run() {
    5454    try (
    55             BufferedReader br = new BufferedReader(new InputStreamReader(
    56                     MapillaryURL.searchSequenceURL(bounds, page).openStream(),
    57                     "UTF-8"
    58             ));
     55        BufferedReader br = new BufferedReader(new InputStreamReader(
     56            MapillaryURL.searchSequenceURL(bounds, page).openStream(), "UTF-8"
     57        ));
    5958    ) {
    6059      JsonObject jsonall = Json.createReader(br).readObject();
     
    121120      }
    122121    } catch (IOException e) {
    123       Main.error("Error reading the url " + MapillaryURL.searchSequenceURL(bounds, page) + " might be a Mapillary problem.", e);
     122      Main.error(String.format(
     123          "Error reading the url %s, this might be a Mapillary problem.",
     124          MapillaryURL.searchSequenceURL(bounds, page)
     125      ), e);
    124126    }
    125127    MapillaryData.dataUpdated();
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/io/download/MapillaryTrafficSignDownloadThread.java

    r31951 r32069  
    1818import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;
    1919import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL;
     20import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL.IMAGE_SELECTOR;
    2021
    2122/**
     
    4849    try (
    4950      BufferedReader br = new BufferedReader(new InputStreamReader(
    50         MapillaryURL.searchTrafficSignURL(bounds, page).openStream(), "UTF-8"
     51        MapillaryURL.searchImageInfoURL(bounds, page, IMAGE_SELECTOR.OBJ_REC_ONLY).openStream(), "UTF-8"
    5152      ));
    5253    ) {
     
    7879          for (int j = 0; j < rects.size(); j++) {
    7980            JsonObject data = rects.getJsonObject(j);
    80             for (MapillaryAbstractImage image : MapillaryLayer.getInstance().getData().getImages())
    81               if (image instanceof MapillaryImage && ((MapillaryImage) image).getKey().equals(key))
     81            for (MapillaryAbstractImage image : MapillaryLayer.getInstance().getData().getImages()) {
     82              if (image instanceof MapillaryImage && ((MapillaryImage) image).getKey().equals(key)) {
    8283                ((MapillaryImage) image).addSign(data.getString("type"));
     84              }
     85            }
    8386          }
    8487        }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryURL.java

    r32064 r32069  
    2020  private static final String BASE_WEBSITE_URL = "https://www.mapillary.com/";
    2121
     22  public enum IMAGE_SELECTOR {
     23    BLURRED_ONLY, COMMENTED_ONLY, OBJ_REC_ONLY // null is used when all images should be selected
     24  }
     25
    2226  private MapillaryURL() {
    2327    // Private constructor to avoid instantiation
     
    3337  public static URL browseEditURL(String imgKey) {
    3438    ValidationUtil.throwExceptionForInvalidImgKey(imgKey, false);
    35     return string2URL(BASE_WEBSITE_URL + "map/e/" + imgKey);
     39    return string2URL(BASE_WEBSITE_URL, "map/e/", imgKey);
    3640  }
    3741
     
    4549  public static URL browseImageURL(String key) {
    4650    ValidationUtil.throwExceptionForInvalidImgKey(key, false);
    47     return string2URL(BASE_WEBSITE_URL + "map/im/" + key);
     51    return string2URL(BASE_WEBSITE_URL, "map/im/", key);
    4852  }
    4953
     
    5256   */
    5357  public static URL browseUploadImageURL() {
    54     return string2URL(BASE_WEBSITE_URL + "map/upload/im/");
     58    return string2URL(BASE_WEBSITE_URL, "map/upload/im");
    5559  }
    5660
     
    6771    parts.put("response_type", "token");
    6872    parts.put("scope", "user:read public:upload public:write");
    69     return string2URL(BASE_WEBSITE_URL + "connect"+queryString(parts));
     73    return string2URL(BASE_WEBSITE_URL, "connect", queryString(parts));
    7074  }
    7175
     
    7579   * @param bounds the bounds in which you want to search for images
    7680   * @param page number of the page to retrieve from the API
     81   * @param selector
    7782   * @return the API-URL which gives you the images in the given bounds as JSON
    7883   */
    79   public static URL searchImageURL(Bounds bounds, int page) {
     84  public static URL searchImageInfoURL(Bounds bounds, int page, IMAGE_SELECTOR selector) {
     85    String selectorString = "";
     86    if (selector != null) {
     87      switch (selector) {
     88      case BLURRED_ONLY:
     89        selectorString = "/b";
     90        break;
     91      case COMMENTED_ONLY:
     92        selectorString = "/cm";
     93        break;
     94      case OBJ_REC_ONLY:
     95        selectorString = "/or";
     96        break;
     97      default:
     98        selectorString = "";
     99        break;
     100      }
     101    }
    80102    HashMap<String, String> parts = new HashMap<>();
    81103    putBoundsInQueryStringParts(parts, bounds);
    82104    parts.put("page", Integer.toString(page));
    83105    parts.put("limit", "20");
    84     return string2URL(BASE_API_URL + "search/im/" + queryString(parts));
     106    return string2URL(BASE_API_URL, "search/im", selectorString, queryString(parts));
    85107  }
    86108
     
    97119    parts.put("page", Integer.toString(page));
    98120    parts.put("limit", "10");
    99     return string2URL(BASE_API_URL + "search/s/" + queryString(parts));
    100   }
    101 
    102   /**
    103    * Gives you the API-URL where you get the traffic signs for 20 images within the given bounds.
    104    * For the signs from more than 20 images you have to use different URLs with different page numbers.
    105    * @param bounds the bounds in which you want to search for traffic signs
    106    * @param page number of the page to retrieve from the API
    107    * @return the API-URL which gives you the traffic signs in the given bounds as JSON
    108    */
    109   public static URL searchTrafficSignURL(Bounds bounds, int page) {
    110     HashMap<String, String> parts = new HashMap<>();
    111     putBoundsInQueryStringParts(parts, bounds);
    112     parts.put("page", Integer.toString(page));
    113     parts.put("limit", "20");
    114     return string2URL(BASE_API_URL + "search/im/or/" + queryString(parts));
     121    return string2URL(BASE_API_URL, "search/s", queryString(parts));
    115122  }
    116123
     
    119126   */
    120127  public static URL uploadSecretsURL() {
    121     return string2URL(BASE_API_URL + "me/uploads/secrets/" + queryString(null));
     128    return string2URL(BASE_API_URL, "me/uploads/secrets", queryString(null));
    122129  }
    123130
     
    126133   */
    127134  public static URL userURL() {
    128     return string2URL(BASE_API_URL + "me/" + queryString(null));
     135    return string2URL(BASE_API_URL, "me", queryString(null));
    129136  }
    130137
     
    172179   * @return the URL that is constructed from the given string
    173180   */
    174   private static URL string2URL(String string) {
     181  private static URL string2URL(String... strings) {
     182    StringBuilder builder = new StringBuilder();
     183    for (int i = 0; strings != null && i < strings.length; i++) {
     184      builder.append(strings[i]);
     185    }
    175186    try {
    176       return new URL(string);
     187      return new URL(builder.toString());
    177188    } catch (MalformedURLException e) {
    178       Main.error(new Exception("The "+MapillaryURL.class.getSimpleName()+" class produces malformed URLs!", e));
     189      Main.error(new Exception(String.format(
     190          "The class '%s' produces malformed URLs like '%s'!",
     191          MapillaryURL.class.getName(),
     192          builder
     193      ), e));
    179194      return null;
    180195    }
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryURLTest.java

    r31972 r32069  
    44import static org.junit.Assert.assertNull;
    55import static org.junit.Assert.assertTrue;
    6 import static org.junit.Assert.fail;
    76
    87import java.lang.reflect.InvocationTargetException;
     
    1312import org.junit.Test;
    1413import org.openstreetmap.josm.data.Bounds;
     14import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL.IMAGE_SELECTOR;
    1515
    1616public class MapillaryURLTest {
     17  private static final String CLIENT_ID_QUERY_PART = "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz";
     18  private static final String LIMIT_20_QUERY_PART = "limit=20";
     19
    1720  @Test
    1821  public void testBrowseEditURL() throws MalformedURLException {
     
    2124        MapillaryURL.browseEditURL("1234567890123456789012")
    2225    );
    23     try {
    24       MapillaryURL.browseEditURL(null);
    25       fail();
    26     } catch (IllegalArgumentException e) {}
    27     try {
    28       MapillaryURL.browseEditURL("123456789012345678901");
    29       fail();
    30     } catch (IllegalArgumentException e) {}
    31     try {
    32       MapillaryURL.browseEditURL("123456789012345678901+");
    33       fail();
    34     } catch (IllegalArgumentException e) {}
     26  }
     27
     28  @Test(expected=IllegalArgumentException.class)
     29  public void testIllegalBrowseEditURL() {
     30    MapillaryURL.browseEditURL(null);
     31  }
     32
     33  @Test(expected=IllegalArgumentException.class)
     34  public void testIllegalBrowseEditURL2() {
     35    MapillaryURL.browseEditURL("123456789012345678901");
     36  }
     37
     38  @Test(expected=IllegalArgumentException.class)
     39  public void testIllegalBrowseEditURL3() {
     40    MapillaryURL.browseEditURL("12345678901234567890123");
     41  }
     42
     43  @Test(expected=IllegalArgumentException.class)
     44  public void testIllegalBrowseEditURL4() {
     45    MapillaryURL.browseEditURL("123456789012345678901+");
    3546  }
    3647
     
    4152        MapillaryURL.browseImageURL("1234567890123456789012")
    4253    );
    43     try {
    44       MapillaryURL.browseImageURL(null);
    45       fail();
    46     } catch (IllegalArgumentException e) {}
    47     try {
    48       MapillaryURL.browseImageURL("123456789012345678901");
    49       fail();
    50     } catch (IllegalArgumentException e) {}
    51     try {
    52       MapillaryURL.browseImageURL("123456789012345678901+");
    53       fail();
    54     } catch (IllegalArgumentException e) {}
     54  }
     55
     56  @Test(expected=IllegalArgumentException.class)
     57  public void testIllegalBrowseImageURL() {
     58    MapillaryURL.browseImageURL(null);
     59  }
     60
     61  @Test(expected=IllegalArgumentException.class)
     62  public void testIllegalBrowseImageURL2() {
     63    MapillaryURL.browseImageURL("123456789012345678901");
     64  }
     65
     66  @Test(expected=IllegalArgumentException.class)
     67  public void testIllegalBrowseImageURL3() {
     68    MapillaryURL.browseImageURL("12345678901234567890123");
     69  }
     70
     71  @Test(expected=IllegalArgumentException.class)
     72  public void testIllegalBrowseImageURL4() {
     73    MapillaryURL.browseImageURL("123456789012345678901+");
    5574  }
    5675
    5776  @Test
    5877  public void testBrowseUploadImageURL() throws MalformedURLException {
    59     assertEquals(new URL("https://www.mapillary.com/map/upload/im/"), MapillaryURL.browseUploadImageURL());
     78    assertEquals(new URL("https://www.mapillary.com/map/upload/im"), MapillaryURL.browseUploadImageURL());
    6079  }
    6180
     
    6584        MapillaryURL.connectURL("http://redirect-host/ä"),
    6685        "https://www.mapillary.com/connect",
    67         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     86        CLIENT_ID_QUERY_PART,
    6887        "scope=user%3Aread+public%3Aupload+public%3Awrite",
    6988        "response_type=token",
     
    7493        MapillaryURL.connectURL(null),
    7594        "https://www.mapillary.com/connect",
    76         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     95        CLIENT_ID_QUERY_PART,
    7796        "scope=user%3Aread+public%3Aupload+public%3Awrite",
    7897        "response_type=token"
     
    82101        MapillaryURL.connectURL(""),
    83102        "https://www.mapillary.com/connect",
    84         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     103        CLIENT_ID_QUERY_PART,
    85104        "scope=user%3Aread+public%3Aupload+public%3Awrite",
    86105        "response_type=token"
     
    91110  public void testSearchImageURL() {
    92111    assertUrlEquals(
    93         MapillaryURL.searchImageURL(new Bounds(1.1, 2.22, 3.333, 4.4444), 42),
    94         "https://a.mapillary.com/v2/search/im/",
    95         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     112        MapillaryURL.searchImageInfoURL(new Bounds(1.1, 2.22, 3.333, 4.4444), 42, null),
     113        "https://a.mapillary.com/v2/search/im",
     114        CLIENT_ID_QUERY_PART,
    96115        "min_lon=2.220000",
    97116        "max_lon=4.444400",
    98117        "min_lat=1.100000",
    99118        "max_lat=3.333000",
    100         "limit=20",
     119        LIMIT_20_QUERY_PART,
    101120        "page=42"
    102121    );
    103122    assertUrlEquals(
    104         MapillaryURL.searchImageURL(null, -73),
    105         "https://a.mapillary.com/v2/search/im/",
    106         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
    107         "limit=20",
     123        MapillaryURL.searchImageInfoURL(null, -73, null),
     124        "https://a.mapillary.com/v2/search/im",
     125        CLIENT_ID_QUERY_PART,
     126        LIMIT_20_QUERY_PART,
    108127        "page=-73"
    109128    );
     
    114133    assertUrlEquals(
    115134        MapillaryURL.searchSequenceURL(new Bounds(-55.55555, -66.666666, 77.7777777, 88.88888888, false), 42),
    116         "https://a.mapillary.com/v2/search/s/",
    117         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     135        "https://a.mapillary.com/v2/search/s",
     136        CLIENT_ID_QUERY_PART,
    118137        "min_lon=-66.666666",
    119138        "max_lon=88.888889",
     
    125144    assertUrlEquals(
    126145        MapillaryURL.searchSequenceURL(null, -73),
    127         "https://a.mapillary.com/v2/search/s/",
    128         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     146        "https://a.mapillary.com/v2/search/s",
     147        CLIENT_ID_QUERY_PART,
    129148        "limit=10",
    130149        "page=-73"
     
    135154  public void testSearchTrafficSignURL() {
    136155    assertUrlEquals(
    137         MapillaryURL.searchTrafficSignURL(new Bounds(1.1, 2.22, 3.333, 4.4444), -42),
    138         "https://a.mapillary.com/v2/search/im/or/",
    139         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
     156        MapillaryURL.searchImageInfoURL(new Bounds(1.1, 2.22, 3.333, 4.4444), -42, IMAGE_SELECTOR.OBJ_REC_ONLY),
     157        "https://a.mapillary.com/v2/search/im/or",
     158        CLIENT_ID_QUERY_PART,
    140159        "min_lon=2.220000",
    141160        "max_lon=4.444400",
    142161        "min_lat=1.100000",
    143162        "max_lat=3.333000",
    144         "limit=20",
     163        LIMIT_20_QUERY_PART,
    145164        "page=-42"
    146165    );
    147166    assertUrlEquals(
    148         MapillaryURL.searchTrafficSignURL(null, 73),
    149         "https://a.mapillary.com/v2/search/im/or/",
    150         "client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz",
    151         "limit=20",
     167        MapillaryURL.searchImageInfoURL(null, 73, IMAGE_SELECTOR.OBJ_REC_ONLY),
     168        "https://a.mapillary.com/v2/search/im/or",
     169        CLIENT_ID_QUERY_PART,
     170        LIMIT_20_QUERY_PART,
    152171        "page=73"
    153172    );
     
    157176  public void testUploadSecretsURL() throws MalformedURLException {
    158177    assertEquals(
    159         new URL("https://a.mapillary.com/v2/me/uploads/secrets/?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"),
     178        new URL("https://a.mapillary.com/v2/me/uploads/secrets?"+CLIENT_ID_QUERY_PART),
    160179        MapillaryURL.uploadSecretsURL()
    161180    );
     
    165184  public void testUserURL() throws MalformedURLException {
    166185    assertEquals(
    167         new URL("https://a.mapillary.com/v2/me/?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"),
     186        new URL("https://a.mapillary.com/v2/me?"+CLIENT_ID_QUERY_PART),
    168187        MapillaryURL.userURL()
    169188    );
     
    172191  @Test
    173192  public void testString2MalformedURL()
    174       throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    175     Method method = MapillaryURL.class.getDeclaredMethod("string2URL", String.class);
     193      throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
     194    Method method = MapillaryURL.class.getDeclaredMethod("string2URL", String[].class);
    176195    method.setAccessible(true);
    177     assertNull(method.invoke(null, "bla"));
     196    assertNull(method.invoke(null, new Object[]{new String[]{"malformed URL"}})); // this simply invokes string2URL("malformed URL")
    178197  }
    179198
Note: See TracChangeset for help on using the changeset viewer.