Changeset 27483 in osm for applications/viewer/jmapviewer/src/org/openstreetmap/gui
- Timestamp:
- 2012-01-18T23:10:25+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java
r26807 r27483 4 4 5 5 import java.awt.Image; 6 import java.io.BufferedReader; 7 import java.io.ByteArrayInputStream; 6 8 import java.io.IOException; 7 import java.io.InputStream ;9 import java.io.InputStreamReader; 8 10 import java.net.URL; 9 import java.net.URLConnection;10 11 import java.util.ArrayList; 11 12 import java.util.List; … … 14 15 import java.util.concurrent.Executors; 15 16 import java.util.concurrent.Future; 17 import java.util.concurrent.TimeUnit; 18 import java.util.concurrent.TimeoutException; 16 19 import java.util.regex.Pattern; 17 20 … … 27 30 28 31 import org.openstreetmap.gui.jmapviewer.Coordinate; 32 import org.openstreetmap.josm.io.CacheCustomContent; 33 import org.openstreetmap.josm.io.UTFInputStreamReader; 29 34 import org.w3c.dom.Document; 30 35 import org.w3c.dom.Node; 31 36 import org.w3c.dom.NodeList; 37 import org.xml.sax.InputSource; 32 38 import org.xml.sax.SAXException; 33 39 … … 45 51 public BingAerialTileSource() { 46 52 super("Bing Aerial Maps", "http://example.com/"); 47 48 if (attributions == null) { 49 attributions = Executors.newSingleThreadExecutor().submit(new Callable<List<Attribution>>(){50 public List<Attribution> call() throws Exception { 51 List<Attribution> attrs = null;52 int waitTime = 1;53 do {54 55 try {56 attrs = loadAttributionText();57 System.out.println("Successfully loaded Bing attribution data.");58 return attrs;59 } catch(IOException e) {60 System.err.println("Could not connect to Bing API. Will retry in " + waitTime + " seconds.");61 Thread.sleep(waitTime * 1000L);62 waitTime *= 2;63 }64 65 } while(true);66 }67 });53 } 54 55 private static class BingAttributionData extends CacheCustomContent<IOException> { 56 57 public BingAttributionData() { 58 super("bing.attribution.xml", CacheCustomContent.INTERVAL_WEEKLY); 59 } 60 61 @Override 62 protected byte[] updateData() throws IOException { 63 URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key=" 64 + API_KEY); 65 BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream())); 66 StringBuilder content = new StringBuilder(1<<15 /* represents 32k */); 67 String line; 68 while ((line = in.readLine()) != null) { 69 content.append(line); 70 } 71 in.close(); 72 System.out.println("Successfully loaded Bing attribution data."); 73 return content.toString().getBytes(); 68 74 } 69 75 } … … 82 88 String subdomain = subdomains[t]; 83 89 84 String url = new String(imageUrlTemplate);90 String url = imageUrlTemplate; 85 91 url = subdomainPattern.matcher(url).replaceAll(subdomain); 86 92 url = quadkeyPattern.matcher(url).replaceAll(computeQuadTree(zoom, tilex, tiley)); … … 89 95 } 90 96 91 private List<Attribution> loadAttributionText() throws IOException {97 private List<Attribution> parseAttributionText(String xml) throws IOException { 92 98 try { 93 URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key="94 + API_KEY);95 URLConnection conn = u.openConnection();96 97 InputStream stream = conn.getInputStream();98 99 99 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 100 100 DocumentBuilder builder = factory.newDocumentBuilder(); 101 Document document = builder.parse(stream); 101 Document document = builder.parse(new InputSource( 102 UTFInputStreamReader.create(new ByteArrayInputStream(xml.getBytes()), "UTF-8"))); 102 103 103 104 XPathFactory xPathFactory = XPathFactory.newInstance(); … … 169 170 } 170 171 172 @Override 171 173 public TileUpdate getTileUpdate() { 172 174 return TileUpdate.IfNoneMatch; … … 212 214 @Override 213 215 public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) { 216 if (attributions == null) { 217 attributions = Executors.newSingleThreadExecutor().submit(new Callable<List<Attribution>>() { 218 219 @Override 220 public List<Attribution> call() throws Exception { 221 BingAttributionData attributionLoader = new BingAttributionData(); 222 int waitTimeSec = 1; 223 while (true) { 224 try { 225 return parseAttributionText(attributionLoader.updateIfRequiredString()); 226 } catch (IOException ex) { 227 System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds."); 228 Thread.sleep(waitTimeSec * 1000L); 229 waitTimeSec *= 2; 230 } 231 } 232 } 233 }); 234 } 214 235 try { 215 if (!attributions.isDone()) 236 final List<Attribution> data; 237 try { 238 data = attributions.get(100, TimeUnit.MILLISECONDS); 239 } catch (TimeoutException ex) { 216 240 return "Loading Bing attribution data..."; 217 if (attributions.get() == null) 241 } 242 if (data == null) { 218 243 return "Error loading Bing attribution data"; 244 } 219 245 StringBuilder a = new StringBuilder(); 220 for (Attribution attr : attributions.get()) {246 for (Attribution attr : data) { 221 247 if (zoom <= attr.maxZoom && zoom >= attr.minZoom) { 222 248 if (topLeft.getLon() < attr.max.getLon() && botRight.getLon() > attr.min.getLon()
Note:
See TracChangeset
for help on using the changeset viewer.