[30223] | 1 | // License: GPL. For details, see Readme.txt file.
|
---|
[25369] | 2 | package org.openstreetmap.gui.jmapviewer.tilesources;
|
---|
[24706] | 3 |
|
---|
| 4 | import java.awt.Image;
|
---|
| 5 | import java.io.IOException;
|
---|
[27507] | 6 | import java.net.MalformedURLException;
|
---|
[24706] | 7 | import java.net.URL;
|
---|
| 8 | import java.util.ArrayList;
|
---|
| 9 | import java.util.List;
|
---|
[26084] | 10 | import java.util.Locale;
|
---|
[24763] | 11 | import java.util.concurrent.Callable;
|
---|
[27519] | 12 | import java.util.concurrent.ExecutionException;
|
---|
[24763] | 13 | import java.util.concurrent.Future;
|
---|
[31539] | 14 | import java.util.concurrent.FutureTask;
|
---|
[27483] | 15 | import java.util.concurrent.TimeUnit;
|
---|
| 16 | import java.util.concurrent.TimeoutException;
|
---|
[26084] | 17 | import java.util.regex.Pattern;
|
---|
[24706] | 18 |
|
---|
[26084] | 19 | import javax.xml.parsers.DocumentBuilder;
|
---|
| 20 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
| 21 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 22 | import javax.xml.xpath.XPath;
|
---|
| 23 | import javax.xml.xpath.XPathConstants;
|
---|
| 24 | import javax.xml.xpath.XPathExpression;
|
---|
| 25 | import javax.xml.xpath.XPathExpressionException;
|
---|
| 26 | import javax.xml.xpath.XPathFactory;
|
---|
[24706] | 27 |
|
---|
[25369] | 28 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
[34760] | 29 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
[29187] | 30 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
[31301] | 31 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
[26084] | 32 | import org.w3c.dom.Document;
|
---|
| 33 | import org.w3c.dom.Node;
|
---|
| 34 | import org.w3c.dom.NodeList;
|
---|
[27483] | 35 | import org.xml.sax.InputSource;
|
---|
[24706] | 36 | import org.xml.sax.SAXException;
|
---|
| 37 |
|
---|
[32181] | 38 | /**
|
---|
| 39 | * Tile source for the Bing Maps REST Imagery API.
|
---|
| 40 | * @see <a href="https://msdn.microsoft.com/en-us/library/ff701724.aspx">MSDN</a>
|
---|
| 41 | */
|
---|
[32025] | 42 | public class BingAerialTileSource extends TMSTileSource {
|
---|
[28553] | 43 |
|
---|
[31434] | 44 | private static final String API_KEY = "Arzdiw4nlOJzRwOz__qailc8NiR31Tt51dN2D7cm57NrnceZnCpgOkmJhNpGoppU";
|
---|
[27519] | 45 | private static volatile Future<List<Attribution>> attributions; // volatile is required for getAttribution(), see below.
|
---|
[26085] | 46 | private static String imageUrlTemplate;
|
---|
| 47 | private static Integer imageryZoomMax;
|
---|
| 48 | private static String[] subdomains;
|
---|
[24706] | 49 |
|
---|
[26084] | 50 | private static final Pattern subdomainPattern = Pattern.compile("\\{subdomain\\}");
|
---|
| 51 | private static final Pattern quadkeyPattern = Pattern.compile("\\{quadkey\\}");
|
---|
| 52 | private static final Pattern culturePattern = Pattern.compile("\\{culture\\}");
|
---|
[31790] | 53 | private String brandLogoUri;
|
---|
[26084] | 54 |
|
---|
[30758] | 55 | /**
|
---|
| 56 | * Constructs a new {@code BingAerialTileSource}.
|
---|
| 57 | */
|
---|
[30855] | 58 | public BingAerialTileSource() {
|
---|
[31122] | 59 | super(new TileSourceInfo("Bing", null, null));
|
---|
[30855] | 60 | }
|
---|
| 61 |
|
---|
[31434] | 62 | /**
|
---|
| 63 | * Constructs a new {@code BingAerialTileSource}.
|
---|
[31439] | 64 | * @param info imagery info
|
---|
[31434] | 65 | */
|
---|
[31122] | 66 | public BingAerialTileSource(TileSourceInfo info) {
|
---|
| 67 | super(info);
|
---|
[27483] | 68 | }
|
---|
[24706] | 69 |
|
---|
[31427] | 70 | protected static class Attribution {
|
---|
[32181] | 71 | private String attributionText;
|
---|
[31434] | 72 | private int minZoom;
|
---|
| 73 | private int maxZoom;
|
---|
| 74 | private Coordinate min;
|
---|
| 75 | private Coordinate max;
|
---|
[24706] | 76 | }
|
---|
| 77 |
|
---|
[26084] | 78 | @Override
|
---|
| 79 | public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
|
---|
[27519] | 80 | // make sure that attribution is loaded. otherwise subdomains is null.
|
---|
[28553] | 81 | if (getAttribution() == null)
|
---|
[28639] | 82 | throw new IOException("Attribution is not loaded yet");
|
---|
[27519] | 83 |
|
---|
[26084] | 84 | int t = (zoom + tilex + tiley) % subdomains.length;
|
---|
| 85 | String subdomain = subdomains[t];
|
---|
[24706] | 86 |
|
---|
[27483] | 87 | String url = imageUrlTemplate;
|
---|
[26084] | 88 | url = subdomainPattern.matcher(url).replaceAll(subdomain);
|
---|
| 89 | url = quadkeyPattern.matcher(url).replaceAll(computeQuadTree(zoom, tilex, tiley));
|
---|
[24706] | 90 |
|
---|
[26084] | 91 | return url;
|
---|
[24706] | 92 | }
|
---|
| 93 |
|
---|
[27507] | 94 | protected URL getAttributionUrl() throws MalformedURLException {
|
---|
[31855] | 95 | return new URL("https://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key="
|
---|
[27507] | 96 | + API_KEY);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | protected List<Attribution> parseAttributionText(InputSource xml) throws IOException {
|
---|
[24706] | 100 | try {
|
---|
[26084] | 101 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
| 102 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
[27507] | 103 | Document document = builder.parse(xml);
|
---|
[26084] | 104 |
|
---|
| 105 | XPathFactory xPathFactory = XPathFactory.newInstance();
|
---|
| 106 | XPath xpath = xPathFactory.newXPath();
|
---|
[32181] | 107 | imageUrlTemplate = xpath.compile("//ImageryMetadata/ImageUrl/text()").evaluate(document).replace(
|
---|
[31855] | 108 | "http://ecn.{subdomain}.tiles.virtualearth.net/",
|
---|
| 109 | "https://ecn.{subdomain}.tiles.virtualearth.net/");
|
---|
[26084] | 110 | imageUrlTemplate = culturePattern.matcher(imageUrlTemplate).replaceAll(Locale.getDefault().toString());
|
---|
[31434] | 111 | imageryZoomMax = Integer.valueOf(xpath.compile("//ImageryMetadata/ZoomMax/text()").evaluate(document));
|
---|
[26084] | 112 |
|
---|
[31429] | 113 | NodeList subdomainTxt = (NodeList) xpath.compile("//ImageryMetadata/ImageUrlSubdomains/string/text()")
|
---|
| 114 | .evaluate(document, XPathConstants.NODESET);
|
---|
[26084] | 115 | subdomains = new String[subdomainTxt.getLength()];
|
---|
[31429] | 116 | for (int i = 0; i < subdomainTxt.getLength(); i++) {
|
---|
[26084] | 117 | subdomains[i] = subdomainTxt.item(i).getNodeValue();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[30758] | 120 | brandLogoUri = xpath.compile("/Response/BrandLogoUri/text()").evaluate(document);
|
---|
| 121 |
|
---|
[26084] | 122 | XPathExpression attributionXpath = xpath.compile("Attribution/text()");
|
---|
| 123 | XPathExpression coverageAreaXpath = xpath.compile("CoverageArea");
|
---|
| 124 | XPathExpression zoomMinXpath = xpath.compile("ZoomMin/text()");
|
---|
| 125 | XPathExpression zoomMaxXpath = xpath.compile("ZoomMax/text()");
|
---|
| 126 | XPathExpression southLatXpath = xpath.compile("BoundingBox/SouthLatitude/text()");
|
---|
| 127 | XPathExpression westLonXpath = xpath.compile("BoundingBox/WestLongitude/text()");
|
---|
| 128 | XPathExpression northLatXpath = xpath.compile("BoundingBox/NorthLatitude/text()");
|
---|
| 129 | XPathExpression eastLonXpath = xpath.compile("BoundingBox/EastLongitude/text()");
|
---|
| 130 |
|
---|
[31429] | 131 | NodeList imageryProviderNodes = (NodeList) xpath.compile("//ImageryMetadata/ImageryProvider")
|
---|
| 132 | .evaluate(document, XPathConstants.NODESET);
|
---|
[32181] | 133 | List<Attribution> attributionsList = new ArrayList<>(imageryProviderNodes.getLength());
|
---|
[26084] | 134 | for (int i = 0; i < imageryProviderNodes.getLength(); i++) {
|
---|
| 135 | Node providerNode = imageryProviderNodes.item(i);
|
---|
| 136 |
|
---|
| 137 | String attribution = attributionXpath.evaluate(providerNode);
|
---|
| 138 |
|
---|
| 139 | NodeList coverageAreaNodes = (NodeList) coverageAreaXpath.evaluate(providerNode, XPathConstants.NODESET);
|
---|
[31429] | 140 | for (int j = 0; j < coverageAreaNodes.getLength(); j++) {
|
---|
[26084] | 141 | Node areaNode = coverageAreaNodes.item(j);
|
---|
| 142 | Attribution attr = new Attribution();
|
---|
[32181] | 143 | attr.attributionText = attribution;
|
---|
[26084] | 144 |
|
---|
| 145 | attr.maxZoom = Integer.parseInt(zoomMaxXpath.evaluate(areaNode));
|
---|
| 146 | attr.minZoom = Integer.parseInt(zoomMinXpath.evaluate(areaNode));
|
---|
| 147 |
|
---|
[31434] | 148 | Double southLat = Double.valueOf(southLatXpath.evaluate(areaNode));
|
---|
| 149 | Double northLat = Double.valueOf(northLatXpath.evaluate(areaNode));
|
---|
| 150 | Double westLon = Double.valueOf(westLonXpath.evaluate(areaNode));
|
---|
| 151 | Double eastLon = Double.valueOf(eastLonXpath.evaluate(areaNode));
|
---|
[26084] | 152 | attr.min = new Coordinate(southLat, westLon);
|
---|
| 153 | attr.max = new Coordinate(northLat, eastLon);
|
---|
| 154 |
|
---|
[32181] | 155 | attributionsList.add(attr);
|
---|
[26084] | 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[32181] | 159 | return attributionsList;
|
---|
[24706] | 160 | } catch (SAXException e) {
|
---|
| 161 | System.err.println("Could not parse Bing aerials attribution metadata.");
|
---|
| 162 | e.printStackTrace();
|
---|
[34085] | 163 | } catch (ParserConfigurationException | XPathExpressionException | NumberFormatException e) {
|
---|
[26084] | 164 | e.printStackTrace();
|
---|
[24706] | 165 | }
|
---|
| 166 | return null;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | @Override
|
---|
| 170 | public int getMaxZoom() {
|
---|
[31429] | 171 | if (imageryZoomMax != null)
|
---|
[26084] | 172 | return imageryZoomMax;
|
---|
| 173 | else
|
---|
| 174 | return 22;
|
---|
[24706] | 175 | }
|
---|
| 176 |
|
---|
[27483] | 177 | @Override
|
---|
[24706] | 178 | public boolean requiresAttribution() {
|
---|
| 179 | return true;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | @Override
|
---|
[26793] | 183 | public String getAttributionLinkURL() {
|
---|
[32181] | 184 | // Terms of Use URL to comply with Bing Terms of Use
|
---|
[26793] | 185 | // (the requirement is that we have such a link at the bottom of the window)
|
---|
[32181] | 186 | return "https://www.microsoft.com/maps/assets/docs/terms.aspx";
|
---|
[26793] | 187 | }
|
---|
| 188 |
|
---|
| 189 | @Override
|
---|
[24706] | 190 | public Image getAttributionImage() {
|
---|
| 191 | try {
|
---|
[34760] | 192 | final URL imageResource = JMapViewer.class.getResource("images/bing_maps.png");
|
---|
[30758] | 193 | if (imageResource != null) {
|
---|
[34760] | 194 | return FeatureAdapter.readImage(imageResource);
|
---|
[30758] | 195 | } else {
|
---|
| 196 | // Some Linux distributions (like Debian) will remove Bing logo from sources, so get it at runtime
|
---|
| 197 | for (int i = 0; i < 5 && getAttribution() == null; i++) {
|
---|
| 198 | // Makes sure attribution is loaded
|
---|
[31432] | 199 | if (JMapViewer.debug) {
|
---|
[31429] | 200 | System.out.println("Bing attribution attempt " + (i+1));
|
---|
| 201 | }
|
---|
[30758] | 202 | }
|
---|
| 203 | if (brandLogoUri != null && !brandLogoUri.isEmpty()) {
|
---|
| 204 | System.out.println("Reading Bing logo from "+brandLogoUri);
|
---|
[34760] | 205 | return FeatureAdapter.readImage(new URL(brandLogoUri));
|
---|
[30758] | 206 | }
|
---|
| 207 | }
|
---|
[24706] | 208 | } catch (IOException e) {
|
---|
[30758] | 209 | System.err.println("Error while retrieving Bing logo: "+e.getMessage());
|
---|
[24706] | 210 | }
|
---|
[30758] | 211 | return null;
|
---|
[24706] | 212 | }
|
---|
| 213 |
|
---|
| 214 | @Override
|
---|
[26793] | 215 | public String getAttributionImageURL() {
|
---|
[34094] | 216 | return "https://opengeodata.org/microsoft-imagery-details";
|
---|
[24706] | 217 | }
|
---|
| 218 |
|
---|
| 219 | @Override
|
---|
[26793] | 220 | public String getTermsOfUseText() {
|
---|
| 221 | return null;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | @Override
|
---|
[24706] | 225 | public String getTermsOfUseURL() {
|
---|
[34094] | 226 | return "https://opengeodata.org/microsoft-imagery-details";
|
---|
[24706] | 227 | }
|
---|
| 228 |
|
---|
[27507] | 229 | protected Callable<List<Attribution>> getAttributionLoaderCallable() {
|
---|
| 230 | return new Callable<List<Attribution>>() {
|
---|
| 231 |
|
---|
| 232 | @Override
|
---|
| 233 | public List<Attribution> call() throws Exception {
|
---|
| 234 | int waitTimeSec = 1;
|
---|
| 235 | while (true) {
|
---|
| 236 | try {
|
---|
| 237 | InputSource xml = new InputSource(getAttributionUrl().openStream());
|
---|
| 238 | List<Attribution> r = parseAttributionText(xml);
|
---|
| 239 | System.out.println("Successfully loaded Bing attribution data.");
|
---|
| 240 | return r;
|
---|
| 241 | } catch (IOException ex) {
|
---|
| 242 | System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.");
|
---|
[33081] | 243 | Thread.sleep(TimeUnit.SECONDS.toMillis(waitTimeSec));
|
---|
[27507] | 244 | waitTimeSec *= 2;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | };
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[27519] | 251 | protected List<Attribution> getAttribution() {
|
---|
[27483] | 252 | if (attributions == null) {
|
---|
[27519] | 253 | // see http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
|
---|
| 254 | synchronized (BingAerialTileSource.class) {
|
---|
| 255 | if (attributions == null) {
|
---|
[31539] | 256 | final FutureTask<List<Attribution>> loader = new FutureTask<>(getAttributionLoaderCallable());
|
---|
| 257 | new Thread(loader, "bing-attribution-loader").start();
|
---|
| 258 | attributions = loader;
|
---|
[27519] | 259 | }
|
---|
| 260 | }
|
---|
[27483] | 261 | }
|
---|
[24763] | 262 | try {
|
---|
[31610] | 263 | return attributions.get(0, TimeUnit.MILLISECONDS);
|
---|
[27519] | 264 | } catch (TimeoutException ex) {
|
---|
| 265 | System.err.println("Bing: attribution data is not yet loaded.");
|
---|
| 266 | } catch (ExecutionException ex) {
|
---|
| 267 | throw new RuntimeException(ex.getCause());
|
---|
| 268 | } catch (InterruptedException ign) {
|
---|
[31429] | 269 | System.err.println("InterruptedException: " + ign.getMessage());
|
---|
[27519] | 270 | }
|
---|
| 271 | return null;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | @Override
|
---|
[31301] | 275 | public String getAttributionText(int zoom, ICoordinate topLeft, ICoordinate botRight) {
|
---|
[27519] | 276 | try {
|
---|
| 277 | final List<Attribution> data = getAttribution();
|
---|
[28553] | 278 | if (data == null)
|
---|
[24763] | 279 | return "Error loading Bing attribution data";
|
---|
| 280 | StringBuilder a = new StringBuilder();
|
---|
[27483] | 281 | for (Attribution attr : data) {
|
---|
[25326] | 282 | if (zoom <= attr.maxZoom && zoom >= attr.minZoom) {
|
---|
| 283 | if (topLeft.getLon() < attr.max.getLon() && botRight.getLon() > attr.min.getLon()
|
---|
| 284 | && topLeft.getLat() > attr.min.getLat() && botRight.getLat() < attr.max.getLat()) {
|
---|
[32181] | 285 | a.append(attr.attributionText);
|
---|
[31434] | 286 | a.append(' ');
|
---|
[24763] | 287 | }
|
---|
[24706] | 288 | }
|
---|
| 289 | }
|
---|
[24763] | 290 | return a.toString();
|
---|
[32180] | 291 | } catch (RuntimeException e) {
|
---|
[24763] | 292 | e.printStackTrace();
|
---|
[24706] | 293 | }
|
---|
[24763] | 294 | return "Error loading Bing attribution data";
|
---|
[24706] | 295 | }
|
---|
| 296 |
|
---|
[31434] | 297 | private static String computeQuadTree(int zoom, int tilex, int tiley) {
|
---|
[24706] | 298 | StringBuilder k = new StringBuilder();
|
---|
[25326] | 299 | for (int i = zoom; i > 0; i--) {
|
---|
[24706] | 300 | char digit = 48;
|
---|
| 301 | int mask = 1 << (i - 1);
|
---|
| 302 | if ((tilex & mask) != 0) {
|
---|
[33079] | 303 | digit += (char) 1;
|
---|
[24706] | 304 | }
|
---|
| 305 | if ((tiley & mask) != 0) {
|
---|
[33079] | 306 | digit += (char) 2;
|
---|
[24706] | 307 | }
|
---|
| 308 | k.append(digit);
|
---|
| 309 | }
|
---|
| 310 | return k.toString();
|
---|
| 311 | }
|
---|
[26085] | 312 | }
|
---|