Changeset 31381 in osm for applications/editors


Ignore:
Timestamp:
2015-07-15T22:19:15+02:00 (9 years ago)
Author:
floscher
Message:

Move all API-URLs into the classes resposible for downloading from the respective parts

Also changed all accesses to MapillaryLayer.data to point to the Getter-method (encapsulation/data hiding).

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

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryLayer.java

    r31379 r31381  
    7373  public static MapillaryImage RED;
    7474
    75   public final MapillaryData data = MapillaryData.getInstance();
     75  private final MapillaryData data = MapillaryData.getInstance();
    7676
    7777  public ArrayList<Bounds> bounds;
     
    588588  public void layerRemoved(Layer oldLayer) {
    589589  }
    590  
     590
    591591  public void updateHelpText() {
    592592    String ret = "";
     
    596596      ret += tr("No images found");
    597597    ret += " -- " + tr(mode.toString());
    598    
     598
    599599     Main.map.statusLine.setHelpText(ret);
    600600  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillaryDownloader.java

    r31350 r31381  
    11package org.openstreetmap.josm.plugins.mapillary.downloads;
     2
     3import java.util.concurrent.ConcurrentHashMap;
     4import java.util.concurrent.Executor;
     5import java.util.concurrent.Executors;
    26
    37import org.openstreetmap.josm.Main;
     
    610import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;
    711
    8 import java.util.concurrent.ConcurrentHashMap;
    9 import java.util.concurrent.Executor;
    10 import java.util.concurrent.Executors;
    11 
    1212/**
    1313 * Class that concentrates all the ways of downloading of the plugin. All the
    1414 * download petitions will be managed one by one.
    15  * 
     15 *
    1616 * @author nokutu
    1717 *
     
    2323  public final static Executor EXECUTOR = Executors.newSingleThreadExecutor();
    2424
    25   private String[] parameters = { "lat", "lon", "distance", "limit", "min_lat",
    26       "min_lon", "max_lat", "max_lon" };
    27 
    2825  public MapillaryDownloader() {
    2926  }
     
    3229   * Gets all the images in a square. It downloads all the images of all the
    3330   * sequences that pass through the given rectangle.
    34    * 
     31   *
    3532   * @param minLatLon
    3633   *          The minimum latitude and longitude of the rectangle.
     
    3936   */
    4037  public void getImages(LatLon minLatLon, LatLon maxLatLon) {
    41     String url1 = BASE_URL;
    42     String url2 = BASE_URL;
    43     String url3 = BASE_URL;
    44     url1 += "search/im/";
    45     url2 += "search/s/";
    46     url3 += "search/im/or";
    47     ConcurrentHashMap<String, Double> hash = new ConcurrentHashMap<>();
    48     hash.put("min_lat", minLatLon.lat());
    49     hash.put("min_lon", minLatLon.lon());
    50     hash.put("max_lat", maxLatLon.lat());
    51     hash.put("max_lon", maxLatLon.lon());
    52     url1 += buildParameters(hash);
    53     url2 += buildParameters(hash);
    54     url3 += buildParameters(hash);
     38    ConcurrentHashMap<String, Double> queryStringParts = new ConcurrentHashMap<>();
     39    queryStringParts.put("min_lat", minLatLon.lat());
     40    queryStringParts.put("min_lon", minLatLon.lon());
     41    queryStringParts.put("max_lat", maxLatLon.lat());
     42    queryStringParts.put("max_lon", maxLatLon.lon());
    5543
    5644    try {
    57       Main.info("GET " + url2 + " (Mapillary plugin)");
    58       EXECUTOR.execute(new MapillarySquareDownloadManagerThread(url1, url2,
    59           url3, MapillaryLayer.getInstance()));
     45      EXECUTOR.execute(new MapillarySquareDownloadManagerThread(queryStringParts, MapillaryLayer.getInstance()));
    6046    } catch (Exception e) {
    6147      Main.error(e);
     
    6551  /**
    6652   * Gets the images within the given bounds.
    67    * 
     53   *
    6854   * @param bounds
    6955   */
     
    7157    getImages(bounds.getMin(), bounds.getMax());
    7258  }
    73 
    74   private String buildParameters(ConcurrentHashMap<String, Double> hash) {
    75     String ret = "?client_id=" + CLIENT_ID;
    76     for (int i = 0; i < parameters.length; i++)
    77       if (hash.get(parameters[i]) != null)
    78         ret += "&" + parameters[i] + "=" + hash.get(parameters[i]);
    79     return ret;
    80   }
    8159}
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillaryImageInfoDownloaderThread.java

    r31352 r31381  
    33import java.io.BufferedReader;
    44import java.io.IOException;
     5import java.io.InputStreamReader;
    56import java.net.MalformedURLException;
    67import java.net.URL;
    7 import java.io.InputStreamReader;
     8import java.util.concurrent.ExecutorService;
    89
     10import javax.json.Json;
    911import javax.json.JsonArray;
    1012import javax.json.JsonObject;
    11 import javax.json.Json;
    12 
    13 import java.util.concurrent.ExecutorService;
    1413
    1514import org.openstreetmap.josm.Main;
     
    2524 */
    2625public class MapillaryImageInfoDownloaderThread extends Thread {
    27   private final String url;
     26  private static final String URL = MapillaryDownloader.BASE_URL + "search/im/";
     27  private final String queryString;
    2828  private final ExecutorService ex;
    2929  private final MapillaryLayer layer;
    3030
    31   public MapillaryImageInfoDownloaderThread(ExecutorService ex, String url,
    32       MapillaryLayer layer) {
     31  public MapillaryImageInfoDownloaderThread(ExecutorService ex, String queryString, MapillaryLayer layer) {
    3332    this.ex = ex;
    34     this.url = url;
     33    this.queryString = queryString;
    3534    this.layer = layer;
    3635  }
     
    4039    try {
    4140      BufferedReader br = new BufferedReader(new InputStreamReader(
    42           new URL(url).openStream(), "UTF-8"));
     41          new URL(URL + queryString).openStream(), "UTF-8"));
    4342      JsonObject jsonobj = Json.createReader(br).readObject();
    4443      if (!jsonobj.getBoolean("more"))
     
    4948        data = jsonarr.getJsonObject(i);
    5049        String key = data.getString("key");
    51         for (MapillaryAbstractImage image : layer.data.getImages()) {
     50        for (MapillaryAbstractImage image : layer.getMapillaryData().getImages()) {
    5251          if (image instanceof MapillaryImage) {
    5352            if (((MapillaryImage) image).getKey().equals(key)
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySequenceDownloadThread.java

    r31373 r31381  
    3030 */
    3131public class MapillarySequenceDownloadThread extends Thread {
     32  private static final String URL = MapillaryDownloader.BASE_URL + "search/s/";
    3233
    33   private final String url;
     34  private final String queryString;
    3435  private final ExecutorService ex;
    3536  private final List<Bounds> bounds;
     
    3738  private final MapillarySquareDownloadManagerThread manager;
    3839
    39   public MapillarySequenceDownloadThread(ExecutorService ex, String url,
     40  public MapillarySequenceDownloadThread(ExecutorService ex, String queryString,
    4041      MapillaryLayer layer, MapillarySquareDownloadManagerThread manager) {
    41     this.url = url;
     42    this.queryString = queryString;
    4243    this.ex = ex;
    4344    this.bounds = layer.bounds;
     
    5051    try {
    5152      BufferedReader br;
    52       br = new BufferedReader(new InputStreamReader(new URL(url).openStream(),
    53           "UTF-8"));
     53      br = new BufferedReader(new InputStreamReader(new URL(URL + queryString).openStream(), "UTF-8"));
    5454      JsonObject jsonall = Json.createReader(br).readObject();
    5555
     
    7171                .getJsonNumber(j).doubleValue()));
    7272          } catch (IndexOutOfBoundsException e) {
    73             Main.warn("Mapillary bug at " + url);
     73            Main.warn("Mapillary bug at " + URL + queryString);
    7474            isSequenceWrong = true;
    7575          }
     
    9292        MapillaryImage.lock.lock();
    9393        for (MapillaryImage img : finalImages) {
    94           if (layer.data.getImages().contains(img)) {
     94          if (layer.getMapillaryData().getImages().contains(img)) {
    9595            sequence.add(img);
    96             ((MapillaryImage) layer.data.getImages().get(
    97                 layer.data.getImages().indexOf(img))).setSequence(sequence);
     96            ((MapillaryImage) layer.getMapillaryData().getImages().get(
     97                layer.getMapillaryData().getImages().indexOf(img))).setSequence(sequence);
    9898            finalImages.set(
    9999                finalImages.indexOf(img),
    100                 (MapillaryImage) layer.data.getImages().get(
    101                     layer.data.getImages().indexOf(img)));
     100                (MapillaryImage) layer.getMapillaryData().getImages().get(
     101                    layer.getMapillaryData().getImages().indexOf(img)));
    102102          } else {
    103103            img.setSequence(sequence);
     
    110110          manager.imagesAdded = imagesAdded;
    111111        }
    112         layer.data.addWithoutUpdate(new ArrayList<MapillaryAbstractImage>(
     112        layer.getMapillaryData().addWithoutUpdate(new ArrayList<MapillaryAbstractImage>(
    113113            finalImages));
    114114      }
    115115    } catch (IOException e) {
    116       Main.error("Error reading the url " + url
     116      Main.error("Error reading the url " + URL + queryString
    117117          + " might be a Mapillary problem.");
    118118    }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySignDownloaderThread.java

    r31350 r31381  
    1818
    1919public class MapillarySignDownloaderThread extends Thread {
    20 
    21   private final String url;
     20  private static final String URL = MapillaryDownloader.BASE_URL + "search/im/or/";
     21  private final String queryString;
    2222  private final ExecutorService ex;
    2323  private final MapillaryLayer layer;
    2424
    25   public MapillarySignDownloaderThread(ExecutorService ex, String url,
    26       MapillaryLayer layer) {
     25  public MapillarySignDownloaderThread(ExecutorService ex, String queryString, MapillaryLayer layer) {
    2726    this.ex = ex;
    28     this.url = url;
     27    this.queryString = queryString;
    2928    this.layer = layer;
    3029  }
     
    3433    BufferedReader br;
    3534    try {
    36       br = new BufferedReader(new InputStreamReader(new URL(url).openStream(),
    37           "UTF-8"));
     35      br = new BufferedReader(new InputStreamReader(new URL(URL + queryString).openStream(), "UTF-8"));
    3836      JsonObject jsonobj = Json.createReader(br).readObject();
    3937      if (!jsonobj.getBoolean("more")) {
     
    5149            for (int k = 0; k < rects.size(); k++) {
    5250              JsonObject data = rects.getJsonObject(k);
    53               for (MapillaryAbstractImage image : layer.data.getImages())
     51              for (MapillaryAbstractImage image : layer.getMapillaryData().getImages())
    5452                if (image instanceof MapillaryImage
    5553                    && ((MapillaryImage) image).getKey().equals(key))
     
    6361          for (int j = 0; j < rects.size(); j++) {
    6462            JsonObject data = rects.getJsonObject(j);
    65             for (MapillaryAbstractImage image : layer.data.getImages())
     63            for (MapillaryAbstractImage image : layer.getMapillaryData().getImages())
    6664              if (image instanceof MapillaryImage
    6765                  && ((MapillaryImage) image).getKey().equals(key))
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySquareDownloadManagerThread.java

    r31358 r31381  
    55import java.util.concurrent.ThreadPoolExecutor;
    66import java.util.concurrent.TimeUnit;
     7import java.io.UnsupportedEncodingException;
     8import java.net.URLEncoder;
     9import java.util.Locale;
    710import java.util.concurrent.ArrayBlockingQueue;
     11import java.util.concurrent.ConcurrentHashMap;
    812
    913import org.openstreetmap.josm.Main;
     
    2327public class MapillarySquareDownloadManagerThread extends Thread {
    2428
    25   private final String urlImages;
    26   private final String urlSequences;
    27   private final String urlSigns;
     29  private final String imageQueryString;
     30  private final String sequenceQueryString;
     31  private final String signQueryString;
    2832  private final MapillaryLayer layer;
    2933  public boolean imagesAdded = false;
    3034
    31   public MapillarySquareDownloadManagerThread(String urlImages, String urlSequences, String urlSigns,
    32       MapillaryLayer layer) {
    33     this.urlImages = urlImages;
    34     this.urlSequences = urlSequences;
    35     this.urlSigns = urlSigns;
     35  public MapillarySquareDownloadManagerThread(ConcurrentHashMap<String, Double> queryStringParts, MapillaryLayer layer) {
     36    this.imageQueryString = buildQueryString(queryStringParts);
     37    this.sequenceQueryString = buildQueryString(queryStringParts);
     38    this.signQueryString = buildQueryString(queryStringParts);
     39
     40    Main.info("GET " + sequenceQueryString + " (Mapillary plugin)"); // TODO: Move this line to the appropriate place, here's no GET-request
     41
    3642    this.layer = layer;
     43  }
     44
     45  //TODO: Maybe move into a separate utility class?
     46  private String buildQueryString(ConcurrentHashMap<String, Double> hash) {
     47    StringBuilder ret = new StringBuilder("?client_id=" + MapillaryDownloader.CLIENT_ID);
     48    for (String key : hash.keySet())
     49      if (key != null)
     50        try {
     51          ret.append("&" + URLEncoder.encode(key, "UTF-8"))
     52             .append("=" + URLEncoder.encode(String.format(Locale.UK, "%f", hash.get(key)), "UTF-8"));
     53        } catch (UnsupportedEncodingException e) {
     54          // This should not happen, as the encoding is hard-coded
     55        }
     56    return ret.toString();
    3757  }
    3858
     
    5373    }
    5474    layer.updateHelpText();
    55     layer.data.dataUpdated();
     75    layer.getMapillaryData().dataUpdated();
    5676    MapillaryFilterDialog.getInstance().refresh();
    5777    MapillaryMainDialog.getInstance().updateImage();
     
    6383    int page = 0;
    6484    while (!ex.isShutdown()) {
    65       ex.execute(new MapillarySequenceDownloadThread(ex, urlSequences
    66           + "&page=" + page + "&limit=10", layer, this));
     85      ex.execute(
     86        new MapillarySequenceDownloadThread(
     87          ex,
     88          sequenceQueryString + "&page=" + page + "&limit=10",
     89          layer,
     90          this
     91        )
     92      );
    6793      while (ex.getQueue().remainingCapacity() == 0)
    6894        Thread.sleep(500);
     
    7096    }
    7197    ex.awaitTermination(15, TimeUnit.SECONDS);
    72     layer.data.dataUpdated();
     98    layer.getMapillaryData().dataUpdated();
    7399  }
    74100
     
    78104    int page = 0;
    79105    while (!ex.isShutdown()) {
    80       ex.execute(new MapillaryImageInfoDownloaderThread(ex, urlImages
     106      ex.execute(new MapillaryImageInfoDownloaderThread(ex, imageQueryString
    81107          + "&page=" + page + "&limit=20", layer));
    82108      while (ex.getQueue().remainingCapacity() == 0)
     
    92118    int page = 0;
    93119    while (!ex.isShutdown()) {
    94       ex.execute(new MapillarySignDownloaderThread(ex, urlSigns + "&page="
     120      ex.execute(new MapillarySignDownloaderThread(ex, signQueryString + "&page="
    95121          + page + "&limit=20", layer));
    96122      while (ex.getQueue().remainingCapacity() == 0)
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/MapillarySequenceDownloadThreadTest.java

    r31347 r31381  
    4848
    4949        ExecutorService ex = Executors.newSingleThreadExecutor();
    50         String url = String.format(
     50        String queryString = String.format(
    5151            Locale.UK,
    52             "%ssearch/s?max_lat=%.8f&max_lon=%.8f&min_lat=%.8f&min_lon=%.8f&limit=10&client_id=%s",
    53             MapillaryDownloader.BASE_URL,
     52            "?max_lat=%.8f&max_lon=%.8f&min_lat=%.8f&min_lon=%.8f&limit=10&client_id=%s",
    5453            maxLatLon.lat(),
    5554            maxLatLon.lon(),
     
    6362        while (!ex.isShutdown() && MapillaryLayer.getInstance().getMapillaryData().getImages().size() <= 0 && page < 50) {
    6463            System.out.println("Sending sequence-request "+page+" to Mapillary-servers…");
    65             Thread downloadThread = new MapillarySequenceDownloadThread(ex, url+"&page="+page, MapillaryLayer.getInstance(), null);
     64            Thread downloadThread = new MapillarySequenceDownloadThread(ex, queryString+"&page="+page, MapillaryLayer.getInstance(), null);
    6665            downloadThread.start();
    6766            downloadThread.join();
Note: See TracChangeset for help on using the changeset viewer.