001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io.imagery;
003
004import java.awt.HeadlessException;
005import java.io.BufferedReader;
006import java.io.IOException;
007import java.io.InputStream;
008import java.io.StringReader;
009import java.net.MalformedURLException;
010import java.net.URL;
011import java.net.URLConnection;
012import java.util.ArrayList;
013import java.util.Collection;
014import java.util.Collections;
015import java.util.HashSet;
016import java.util.List;
017import java.util.Locale;
018import java.util.Set;
019import java.util.regex.Pattern;
020
021import javax.imageio.ImageIO;
022import javax.xml.parsers.DocumentBuilder;
023import javax.xml.parsers.DocumentBuilderFactory;
024
025import org.openstreetmap.josm.Main;
026import org.openstreetmap.josm.data.Bounds;
027import org.openstreetmap.josm.data.imagery.ImageryInfo;
028import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
029import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
030import org.openstreetmap.josm.io.UTFInputStreamReader;
031import org.openstreetmap.josm.tools.Predicate;
032import org.openstreetmap.josm.tools.Utils;
033import org.w3c.dom.Document;
034import org.w3c.dom.Element;
035import org.w3c.dom.Node;
036import org.w3c.dom.NodeList;
037import org.xml.sax.EntityResolver;
038import org.xml.sax.InputSource;
039import org.xml.sax.SAXException;
040
041public class WMSImagery {
042
043    public static class WMSGetCapabilitiesException extends Exception {
044        private final String incomingData;
045
046        public WMSGetCapabilitiesException(Throwable cause, String incomingData) {
047            super(cause);
048            this.incomingData = incomingData;
049        }
050
051        public String getIncomingData() {
052            return incomingData;
053        }
054    }
055
056    private List<LayerDetails> layers;
057    private URL serviceUrl;
058    private List<String> formats;
059
060    public List<LayerDetails> getLayers() {
061        return layers;
062    }
063
064    public URL getServiceUrl() {
065        return serviceUrl;
066    }
067
068    public List<String> getFormats() {
069        return Collections.unmodifiableList(formats);
070    }
071
072    public String getPreferredFormats() {
073        return formats.contains("image/jpeg") ? "image/jpeg"
074                : formats.contains("image/png") ? "image/png"
075                : formats.isEmpty() ? null
076                : formats.get(0);
077    }
078
079    String buildRootUrl() {
080        if (serviceUrl == null) {
081            return null;
082        }
083        StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
084        a.append("://").append(serviceUrl.getHost());
085        if (serviceUrl.getPort() != -1) {
086            a.append(':').append(serviceUrl.getPort());
087        }
088        a.append(serviceUrl.getPath()).append('?');
089        if (serviceUrl.getQuery() != null) {
090            a.append(serviceUrl.getQuery());
091            if (!serviceUrl.getQuery().isEmpty() && !serviceUrl.getQuery().endsWith("&")) {
092                a.append('&');
093            }
094        }
095        return a.toString();
096    }
097
098    public String buildGetMapUrl(Collection<LayerDetails> selectedLayers) {
099        return buildGetMapUrl(selectedLayers, "image/jpeg");
100    }
101
102    public String buildGetMapUrl(Collection<LayerDetails> selectedLayers, String format) {
103        return buildRootUrl()
104                + "FORMAT=" + format + (imageFormatHasTransparency(format) ? "&TRANSPARENT=TRUE" : "")
105                + "&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LAYERS="
106                + Utils.join(",", Utils.transform(selectedLayers, new Utils.Function<LayerDetails, String>() {
107            @Override
108            public String apply(LayerDetails x) {
109                return x.ident;
110            }
111        }))
112                + "&STYLES=&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}";
113    }
114
115    public void attemptGetCapabilities(String serviceUrlStr) throws MalformedURLException, IOException, WMSGetCapabilitiesException {
116        URL getCapabilitiesUrl = null;
117        try {
118            if (!Pattern.compile(".*GetCapabilities.*", Pattern.CASE_INSENSITIVE).matcher(serviceUrlStr).matches()) {
119                // If the url doesn't already have GetCapabilities, add it in
120                getCapabilitiesUrl = new URL(serviceUrlStr);
121                final String getCapabilitiesQuery = "VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities";
122                if (getCapabilitiesUrl.getQuery() == null) {
123                    getCapabilitiesUrl = new URL(serviceUrlStr + '?' + getCapabilitiesQuery);
124                } else if (!getCapabilitiesUrl.getQuery().isEmpty() && !getCapabilitiesUrl.getQuery().endsWith("&")) {
125                    getCapabilitiesUrl = new URL(serviceUrlStr + '&' + getCapabilitiesQuery);
126                } else {
127                    getCapabilitiesUrl = new URL(serviceUrlStr + getCapabilitiesQuery);
128                }
129            } else {
130                // Otherwise assume it's a good URL and let the subsequent error
131                // handling systems deal with problems
132                getCapabilitiesUrl = new URL(serviceUrlStr);
133            }
134            serviceUrl = new URL(serviceUrlStr);
135        } catch (HeadlessException e) {
136            return;
137        }
138
139        Main.info("GET " + getCapabilitiesUrl);
140        URLConnection openConnection = Utils.openHttpConnection(getCapabilitiesUrl, false, true);
141        StringBuilder ba = new StringBuilder();
142
143        try (
144            InputStream inputStream = openConnection.getInputStream();
145            BufferedReader br = new BufferedReader(UTFInputStreamReader.create(inputStream))
146        ) {
147            String line;
148            while ((line = br.readLine()) != null) {
149                ba.append(line);
150                ba.append('\n');
151            }
152        }
153        String incomingData = ba.toString();
154        Main.debug("Server response to Capabilities request:");
155        Main.debug(incomingData);
156
157        try {
158            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
159            builderFactory.setValidating(false);
160            builderFactory.setNamespaceAware(true);
161            DocumentBuilder builder = null;
162            builder = builderFactory.newDocumentBuilder();
163            builder.setEntityResolver(new EntityResolver() {
164                @Override
165                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
166                    Main.info("Ignoring DTD " + publicId + ", " + systemId);
167                    return new InputSource(new StringReader(""));
168                }
169            });
170            Document document = null;
171            document = builder.parse(new InputSource(new StringReader(incomingData)));
172
173            // Some WMS service URLs specify a different base URL for their GetMap service
174            Element child = getChild(document.getDocumentElement(), "Capability");
175            child = getChild(child, "Request");
176            child = getChild(child, "GetMap");
177
178            formats = new ArrayList<>(Utils.filter(Utils.transform(getChildren(child, "Format"),
179                    new Utils.Function<Element, String>() {
180                        @Override
181                        public String apply(Element x) {
182                            return x.getTextContent();
183                        }
184                    }),
185                    new Predicate<String>() {
186                        @Override
187                        public boolean evaluate(String format) {
188                            boolean isFormatSupported = isImageFormatSupported(format);
189                            if (!isFormatSupported) {
190                                Main.info("Skipping unsupported image format {0}", format);
191                            }
192                            return isFormatSupported;
193                        }
194                    }
195            ));
196
197            child = getChild(child, "DCPType");
198            child = getChild(child, "HTTP");
199            child = getChild(child, "Get");
200            child = getChild(child, "OnlineResource");
201            if (child != null) {
202                String baseURL = child.getAttribute("xlink:href");
203                if (baseURL != null && !baseURL.equals(serviceUrlStr)) {
204                    Main.info("GetCapabilities specifies a different service URL: " + baseURL);
205                    serviceUrl = new URL(baseURL);
206                }
207            }
208
209            Element capabilityElem = getChild(document.getDocumentElement(), "Capability");
210            List<Element> children = getChildren(capabilityElem, "Layer");
211            layers = parseLayers(children, new HashSet<String>());
212        } catch (Exception e) {
213            throw new WMSGetCapabilitiesException(e, incomingData);
214        }
215    }
216
217    static boolean isImageFormatSupported(final String format) {
218        return ImageIO.getImageReadersByMIMEType(format).hasNext()
219                // handles image/tiff image/tiff8 image/geotiff image/geotiff8
220                || (format.startsWith("image/tiff") || format.startsWith("image/geotiff")) && ImageIO.getImageReadersBySuffix("tiff").hasNext()
221                || format.startsWith("image/png") && ImageIO.getImageReadersBySuffix("png").hasNext()
222                || format.startsWith("image/svg") && ImageIO.getImageReadersBySuffix("svg").hasNext()
223                || format.startsWith("image/bmp") && ImageIO.getImageReadersBySuffix("bmp").hasNext();
224    }
225
226    static boolean imageFormatHasTransparency(final String format) {
227        return format != null && (format.startsWith("image/png") || format.startsWith("image/gif")
228                || format.startsWith("image/svg") || format.startsWith("image/tiff"));
229    }
230
231    public ImageryInfo toImageryInfo(String name, Collection<LayerDetails> selectedLayers) {
232        ImageryInfo i = new ImageryInfo(name, buildGetMapUrl(selectedLayers));
233        if (selectedLayers != null) {
234            Set<String> proj = new HashSet<>();
235            for (WMSImagery.LayerDetails l : selectedLayers) {
236                proj.addAll(l.getProjections());
237            }
238            i.setServerProjections(proj);
239        }
240        return i;
241    }
242
243    private List<LayerDetails> parseLayers(List<Element> children, Set<String> parentCrs) {
244        List<LayerDetails> details = new ArrayList<>(children.size());
245        for (Element element : children) {
246            details.add(parseLayer(element, parentCrs));
247        }
248        return details;
249    }
250
251    private LayerDetails parseLayer(Element element, Set<String> parentCrs) {
252        String name = getChildContent(element, "Title", null, null);
253        String ident = getChildContent(element, "Name", null, null);
254
255        // The set of supported CRS/SRS for this layer
256        Set<String> crsList = new HashSet<>();
257        // ...including this layer's already-parsed parent projections
258        crsList.addAll(parentCrs);
259
260        // Parse the CRS/SRS pulled out of this layer's XML element
261        // I think CRS and SRS are the same at this point
262        List<Element> crsChildren = getChildren(element, "CRS");
263        crsChildren.addAll(getChildren(element, "SRS"));
264        for (Element child : crsChildren) {
265            String crs = (String) getContent(child);
266            if (!crs.isEmpty()) {
267                String upperCase = crs.trim().toUpperCase(Locale.ENGLISH);
268                crsList.add(upperCase);
269            }
270        }
271
272        // Check to see if any of the specified projections are supported by JOSM
273        boolean josmSupportsThisLayer = false;
274        for (String crs : crsList) {
275            josmSupportsThisLayer |= isProjSupported(crs);
276        }
277
278        Bounds bounds = null;
279        Element bboxElem = getChild(element, "EX_GeographicBoundingBox");
280        if (bboxElem != null) {
281            // Attempt to use EX_GeographicBoundingBox for bounding box
282            double left = Double.parseDouble(getChildContent(bboxElem, "westBoundLongitude", null, null));
283            double top = Double.parseDouble(getChildContent(bboxElem, "northBoundLatitude", null, null));
284            double right = Double.parseDouble(getChildContent(bboxElem, "eastBoundLongitude", null, null));
285            double bot = Double.parseDouble(getChildContent(bboxElem, "southBoundLatitude", null, null));
286            bounds = new Bounds(bot, left, top, right);
287        } else {
288            // If that's not available, try LatLonBoundingBox
289            bboxElem = getChild(element, "LatLonBoundingBox");
290            if (bboxElem != null) {
291                double left = Double.parseDouble(bboxElem.getAttribute("minx"));
292                double top = Double.parseDouble(bboxElem.getAttribute("maxy"));
293                double right = Double.parseDouble(bboxElem.getAttribute("maxx"));
294                double bot = Double.parseDouble(bboxElem.getAttribute("miny"));
295                bounds = new Bounds(bot, left, top, right);
296            }
297        }
298
299        List<Element> layerChildren = getChildren(element, "Layer");
300        List<LayerDetails> childLayers = parseLayers(layerChildren, crsList);
301
302        return new LayerDetails(name, ident, crsList, josmSupportsThisLayer, bounds, childLayers);
303    }
304
305    private static boolean isProjSupported(String crs) {
306        for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
307            if (pc.getPreferencesFromCode(crs) != null) return true;
308        }
309        return false;
310    }
311
312    private static String getChildContent(Element parent, String name, String missing, String empty) {
313        Element child = getChild(parent, name);
314        if (child == null)
315            return missing;
316        else {
317            String content = (String) getContent(child);
318            return (!content.isEmpty()) ? content : empty;
319        }
320    }
321
322    private static Object getContent(Element element) {
323        NodeList nl = element.getChildNodes();
324        StringBuilder content = new StringBuilder();
325        for (int i = 0; i < nl.getLength(); i++) {
326            Node node = nl.item(i);
327            switch (node.getNodeType()) {
328                case Node.ELEMENT_NODE:
329                    return node;
330                case Node.CDATA_SECTION_NODE:
331                case Node.TEXT_NODE:
332                    content.append(node.getNodeValue());
333                    break;
334            }
335        }
336        return content.toString().trim();
337    }
338
339    private static List<Element> getChildren(Element parent, String name) {
340        List<Element> retVal = new ArrayList<>();
341        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
342            if (child instanceof Element && name.equals(child.getNodeName())) {
343                retVal.add((Element) child);
344            }
345        }
346        return retVal;
347    }
348
349    private static Element getChild(Element parent, String name) {
350        if (parent == null)
351            return null;
352        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
353            if (child instanceof Element && name.equals(child.getNodeName()))
354                return (Element) child;
355        }
356        return null;
357    }
358
359    public static class LayerDetails {
360
361        public final String name;
362        public final String ident;
363        public final List<LayerDetails> children;
364        public final Bounds bounds;
365        public final Set<String> crsList;
366        public final boolean supported;
367
368        public LayerDetails(String name, String ident, Set<String> crsList,
369                            boolean supportedLayer, Bounds bounds,
370                            List<LayerDetails> childLayers) {
371            this.name = name;
372            this.ident = ident;
373            this.supported = supportedLayer;
374            this.children = childLayers;
375            this.bounds = bounds;
376            this.crsList = crsList;
377        }
378
379        public boolean isSupported() {
380            return this.supported;
381        }
382
383        public Set<String> getProjections() {
384            return crsList;
385        }
386
387        @Override
388        public String toString() {
389            if (this.name == null || this.name.isEmpty())
390                return this.ident;
391            else
392                return this.name;
393        }
394
395    }
396}