Changeset 27483 in osm for applications/viewer


Ignore:
Timestamp:
2012-01-18T23:10:25+01:00 (12 years ago)
Author:
simon04
Message:

fix #7284 (JOSM) - Download dialog always loads bing attributions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

    r26807 r27483  
    44
    55import java.awt.Image;
     6import java.io.BufferedReader;
     7import java.io.ByteArrayInputStream;
    68import java.io.IOException;
    7 import java.io.InputStream;
     9import java.io.InputStreamReader;
    810import java.net.URL;
    9 import java.net.URLConnection;
    1011import java.util.ArrayList;
    1112import java.util.List;
     
    1415import java.util.concurrent.Executors;
    1516import java.util.concurrent.Future;
     17import java.util.concurrent.TimeUnit;
     18import java.util.concurrent.TimeoutException;
    1619import java.util.regex.Pattern;
    1720
     
    2730
    2831import org.openstreetmap.gui.jmapviewer.Coordinate;
     32import org.openstreetmap.josm.io.CacheCustomContent;
     33import org.openstreetmap.josm.io.UTFInputStreamReader;
    2934import org.w3c.dom.Document;
    3035import org.w3c.dom.Node;
    3136import org.w3c.dom.NodeList;
     37import org.xml.sax.InputSource;
    3238import org.xml.sax.SAXException;
    3339
     
    4551    public BingAerialTileSource() {
    4652        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();
    6874        }
    6975    }
     
    8288        String subdomain = subdomains[t];
    8389
    84         String url = new String(imageUrlTemplate);
     90        String url = imageUrlTemplate;
    8591        url = subdomainPattern.matcher(url).replaceAll(subdomain);
    8692        url = quadkeyPattern.matcher(url).replaceAll(computeQuadTree(zoom, tilex, tiley));
     
    8995    }
    9096
    91     private List<Attribution> loadAttributionText() throws IOException {
     97    private List<Attribution> parseAttributionText(String xml) throws IOException {
    9298        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 
    9999            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    100100            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")));
    102103
    103104            XPathFactory xPathFactory = XPathFactory.newInstance();
     
    169170    }
    170171
     172    @Override
    171173    public TileUpdate getTileUpdate() {
    172174        return TileUpdate.IfNoneMatch;
     
    212214    @Override
    213215    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        }
    214235        try {
    215             if (!attributions.isDone())
     236            final List<Attribution> data;
     237            try {
     238                data = attributions.get(100, TimeUnit.MILLISECONDS);
     239            } catch (TimeoutException ex) {
    216240                return "Loading Bing attribution data...";
    217             if (attributions.get() == null)
     241            }
     242            if (data == null) {
    218243                return "Error loading Bing attribution data";
     244            }
    219245            StringBuilder a = new StringBuilder();
    220             for (Attribution attr : attributions.get()) {
     246            for (Attribution attr : data) {
    221247                if (zoom <= attr.maxZoom && zoom >= attr.minZoom) {
    222248                    if (topLeft.getLon() < attr.max.getLon() && botRight.getLon() > attr.min.getLon()
Note: See TracChangeset for help on using the changeset viewer.