Changeset 31381 in osm for applications/editors
- Timestamp:
- 2015-07-15T22:19:15+02:00 (9 years ago)
- 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 73 73 public static MapillaryImage RED; 74 74 75 p ublicfinal MapillaryData data = MapillaryData.getInstance();75 private final MapillaryData data = MapillaryData.getInstance(); 76 76 77 77 public ArrayList<Bounds> bounds; … … 588 588 public void layerRemoved(Layer oldLayer) { 589 589 } 590 590 591 591 public void updateHelpText() { 592 592 String ret = ""; … … 596 596 ret += tr("No images found"); 597 597 ret += " -- " + tr(mode.toString()); 598 598 599 599 Main.map.statusLine.setHelpText(ret); 600 600 } -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillaryDownloader.java
r31350 r31381 1 1 package org.openstreetmap.josm.plugins.mapillary.downloads; 2 3 import java.util.concurrent.ConcurrentHashMap; 4 import java.util.concurrent.Executor; 5 import java.util.concurrent.Executors; 2 6 3 7 import org.openstreetmap.josm.Main; … … 6 10 import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer; 7 11 8 import java.util.concurrent.ConcurrentHashMap;9 import java.util.concurrent.Executor;10 import java.util.concurrent.Executors;11 12 12 /** 13 13 * Class that concentrates all the ways of downloading of the plugin. All the 14 14 * download petitions will be managed one by one. 15 * 15 * 16 16 * @author nokutu 17 17 * … … 23 23 public final static Executor EXECUTOR = Executors.newSingleThreadExecutor(); 24 24 25 private String[] parameters = { "lat", "lon", "distance", "limit", "min_lat",26 "min_lon", "max_lat", "max_lon" };27 28 25 public MapillaryDownloader() { 29 26 } … … 32 29 * Gets all the images in a square. It downloads all the images of all the 33 30 * sequences that pass through the given rectangle. 34 * 31 * 35 32 * @param minLatLon 36 33 * The minimum latitude and longitude of the rectangle. … … 39 36 */ 40 37 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()); 55 43 56 44 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())); 60 46 } catch (Exception e) { 61 47 Main.error(e); … … 65 51 /** 66 52 * Gets the images within the given bounds. 67 * 53 * 68 54 * @param bounds 69 55 */ … … 71 57 getImages(bounds.getMin(), bounds.getMax()); 72 58 } 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 }81 59 } -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillaryImageInfoDownloaderThread.java
r31352 r31381 3 3 import java.io.BufferedReader; 4 4 import java.io.IOException; 5 import java.io.InputStreamReader; 5 6 import java.net.MalformedURLException; 6 7 import java.net.URL; 7 import java. io.InputStreamReader;8 import java.util.concurrent.ExecutorService; 8 9 10 import javax.json.Json; 9 11 import javax.json.JsonArray; 10 12 import javax.json.JsonObject; 11 import javax.json.Json;12 13 import java.util.concurrent.ExecutorService;14 13 15 14 import org.openstreetmap.josm.Main; … … 25 24 */ 26 25 public 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; 28 28 private final ExecutorService ex; 29 29 private final MapillaryLayer layer; 30 30 31 public MapillaryImageInfoDownloaderThread(ExecutorService ex, String url, 32 MapillaryLayer layer) { 31 public MapillaryImageInfoDownloaderThread(ExecutorService ex, String queryString, MapillaryLayer layer) { 33 32 this.ex = ex; 34 this. url = url;33 this.queryString = queryString; 35 34 this.layer = layer; 36 35 } … … 40 39 try { 41 40 BufferedReader br = new BufferedReader(new InputStreamReader( 42 new URL( url).openStream(), "UTF-8"));41 new URL(URL + queryString).openStream(), "UTF-8")); 43 42 JsonObject jsonobj = Json.createReader(br).readObject(); 44 43 if (!jsonobj.getBoolean("more")) … … 49 48 data = jsonarr.getJsonObject(i); 50 49 String key = data.getString("key"); 51 for (MapillaryAbstractImage image : layer. data.getImages()) {50 for (MapillaryAbstractImage image : layer.getMapillaryData().getImages()) { 52 51 if (image instanceof MapillaryImage) { 53 52 if (((MapillaryImage) image).getKey().equals(key) -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySequenceDownloadThread.java
r31373 r31381 30 30 */ 31 31 public class MapillarySequenceDownloadThread extends Thread { 32 private static final String URL = MapillaryDownloader.BASE_URL + "search/s/"; 32 33 33 private final String url;34 private final String queryString; 34 35 private final ExecutorService ex; 35 36 private final List<Bounds> bounds; … … 37 38 private final MapillarySquareDownloadManagerThread manager; 38 39 39 public MapillarySequenceDownloadThread(ExecutorService ex, String url,40 public MapillarySequenceDownloadThread(ExecutorService ex, String queryString, 40 41 MapillaryLayer layer, MapillarySquareDownloadManagerThread manager) { 41 this. url = url;42 this.queryString = queryString; 42 43 this.ex = ex; 43 44 this.bounds = layer.bounds; … … 50 51 try { 51 52 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")); 54 54 JsonObject jsonall = Json.createReader(br).readObject(); 55 55 … … 71 71 .getJsonNumber(j).doubleValue())); 72 72 } catch (IndexOutOfBoundsException e) { 73 Main.warn("Mapillary bug at " + url);73 Main.warn("Mapillary bug at " + URL + queryString); 74 74 isSequenceWrong = true; 75 75 } … … 92 92 MapillaryImage.lock.lock(); 93 93 for (MapillaryImage img : finalImages) { 94 if (layer. data.getImages().contains(img)) {94 if (layer.getMapillaryData().getImages().contains(img)) { 95 95 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); 98 98 finalImages.set( 99 99 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))); 102 102 } else { 103 103 img.setSequence(sequence); … … 110 110 manager.imagesAdded = imagesAdded; 111 111 } 112 layer. data.addWithoutUpdate(new ArrayList<MapillaryAbstractImage>(112 layer.getMapillaryData().addWithoutUpdate(new ArrayList<MapillaryAbstractImage>( 113 113 finalImages)); 114 114 } 115 115 } catch (IOException e) { 116 Main.error("Error reading the url " + url116 Main.error("Error reading the url " + URL + queryString 117 117 + " might be a Mapillary problem."); 118 118 } -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySignDownloaderThread.java
r31350 r31381 18 18 19 19 public 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; 22 22 private final ExecutorService ex; 23 23 private final MapillaryLayer layer; 24 24 25 public MapillarySignDownloaderThread(ExecutorService ex, String url, 26 MapillaryLayer layer) { 25 public MapillarySignDownloaderThread(ExecutorService ex, String queryString, MapillaryLayer layer) { 27 26 this.ex = ex; 28 this. url = url;27 this.queryString = queryString; 29 28 this.layer = layer; 30 29 } … … 34 33 BufferedReader br; 35 34 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")); 38 36 JsonObject jsonobj = Json.createReader(br).readObject(); 39 37 if (!jsonobj.getBoolean("more")) { … … 51 49 for (int k = 0; k < rects.size(); k++) { 52 50 JsonObject data = rects.getJsonObject(k); 53 for (MapillaryAbstractImage image : layer. data.getImages())51 for (MapillaryAbstractImage image : layer.getMapillaryData().getImages()) 54 52 if (image instanceof MapillaryImage 55 53 && ((MapillaryImage) image).getKey().equals(key)) … … 63 61 for (int j = 0; j < rects.size(); j++) { 64 62 JsonObject data = rects.getJsonObject(j); 65 for (MapillaryAbstractImage image : layer. data.getImages())63 for (MapillaryAbstractImage image : layer.getMapillaryData().getImages()) 66 64 if (image instanceof MapillaryImage 67 65 && ((MapillaryImage) image).getKey().equals(key)) -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySquareDownloadManagerThread.java
r31358 r31381 5 5 import java.util.concurrent.ThreadPoolExecutor; 6 6 import java.util.concurrent.TimeUnit; 7 import java.io.UnsupportedEncodingException; 8 import java.net.URLEncoder; 9 import java.util.Locale; 7 10 import java.util.concurrent.ArrayBlockingQueue; 11 import java.util.concurrent.ConcurrentHashMap; 8 12 9 13 import org.openstreetmap.josm.Main; … … 23 27 public class MapillarySquareDownloadManagerThread extends Thread { 24 28 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; 28 32 private final MapillaryLayer layer; 29 33 public boolean imagesAdded = false; 30 34 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 36 42 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(); 37 57 } 38 58 … … 53 73 } 54 74 layer.updateHelpText(); 55 layer. data.dataUpdated();75 layer.getMapillaryData().dataUpdated(); 56 76 MapillaryFilterDialog.getInstance().refresh(); 57 77 MapillaryMainDialog.getInstance().updateImage(); … … 63 83 int page = 0; 64 84 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 ); 67 93 while (ex.getQueue().remainingCapacity() == 0) 68 94 Thread.sleep(500); … … 70 96 } 71 97 ex.awaitTermination(15, TimeUnit.SECONDS); 72 layer. data.dataUpdated();98 layer.getMapillaryData().dataUpdated(); 73 99 } 74 100 … … 78 104 int page = 0; 79 105 while (!ex.isShutdown()) { 80 ex.execute(new MapillaryImageInfoDownloaderThread(ex, urlImages106 ex.execute(new MapillaryImageInfoDownloaderThread(ex, imageQueryString 81 107 + "&page=" + page + "&limit=20", layer)); 82 108 while (ex.getQueue().remainingCapacity() == 0) … … 92 118 int page = 0; 93 119 while (!ex.isShutdown()) { 94 ex.execute(new MapillarySignDownloaderThread(ex, urlSigns+ "&page="120 ex.execute(new MapillarySignDownloaderThread(ex, signQueryString + "&page=" 95 121 + page + "&limit=20", layer)); 96 122 while (ex.getQueue().remainingCapacity() == 0) -
applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/MapillarySequenceDownloadThreadTest.java
r31347 r31381 48 48 49 49 ExecutorService ex = Executors.newSingleThreadExecutor(); 50 String url= String.format(50 String queryString = String.format( 51 51 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", 54 53 maxLatLon.lat(), 55 54 maxLatLon.lon(), … … 63 62 while (!ex.isShutdown() && MapillaryLayer.getInstance().getMapillaryData().getImages().size() <= 0 && page < 50) { 64 63 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); 66 65 downloadThread.start(); 67 66 downloadThread.join();
Note:
See TracChangeset
for help on using the changeset viewer.