Search in sources :

Example 1 with WMSBounds

use of de.fhg.igd.mapviewer.server.wms.capabilities.WMSBounds in project hale by halestudio.

the class WMSTileOverlay method repaintTile.

/**
 * @see AbstractTileOverlayPainter#repaintTile(int, int, int, int,
 *      PixelConverter, int)
 */
@Override
public BufferedImage repaintTile(int posX, int posY, int width, int height, PixelConverter converter, int zoom) {
    // the first converter isn't regarded as a new converter because it's
    // always the empty map
    boolean isNewConverter = lastConverter != null && !converter.equals(lastConverter);
    lastConverter = converter;
    if (!converter.supportsBoundingBoxes()) {
        if (isNewConverter) {
            handleError(Messages.WMSTileOverlay_0 + configuration.getName() + Messages.WMSTileOverlay_1);
        }
        return null;
    }
    synchronized (this) {
        if (capabilities == null) {
            try {
                capabilities = WMSUtil.getCapabilities(configuration.getBaseUrl());
            } catch (WMSCapabilitiesException e) {
                // $NON-NLS-1$
                log.error("Error getting WMS capabilities");
            }
        }
    }
    if (capabilities != null) {
        int mapEpsg = converter.getMapEpsg();
        WMSBounds box;
        synchronized (this) {
            if (capabilities.getSupportedSRS().contains("EPSG:" + mapEpsg)) {
            // $NON-NLS-1$
            // same SRS supported
            } else {
                // SRS not supported
                if (isNewConverter) {
                    StringBuilder message = new StringBuilder();
                    message.append(Messages.WMSTileOverlay_2);
                    message.append(configuration.getName());
                    message.append(Messages.WMSTileOverlay_3);
                    boolean init = true;
                    for (String srs : capabilities.getSupportedSRS()) {
                        if (init) {
                            init = false;
                        } else {
                            // $NON-NLS-1$
                            message.append(", ");
                        }
                        message.append(srs);
                    }
                    handleError(message.toString());
                }
                return null;
            }
            box = WMSUtil.getBoundingBox(capabilities, mapEpsg);
        }
        String srs = box.getSRS();
        if (srs.startsWith("EPSG:")) {
            // $NON-NLS-1$
            // determine format
            String format = null;
            Iterator<String> itFormat = supportedFormats.iterator();
            synchronized (this) {
                while (format == null && itFormat.hasNext()) {
                    String supp = itFormat.next();
                    if (capabilities.getFormats().contains(supp)) {
                        format = supp;
                    }
                }
            }
            if (format == null) {
                // no compatible format
                return null;
            }
            try {
                // check if tile lies within the bounding box
                int epsg = Integer.parseInt(srs.substring(5));
                GeoPosition topLeft = converter.pixelToGeo(new Point(posX, posY), zoom);
                GeoPosition bottomRight = converter.pixelToGeo(new Point(posX + width, posY + height), zoom);
                // WMS bounding box
                BoundingBox wms = new BoundingBox(box.getMinX(), box.getMinY(), -1, box.getMaxX(), box.getMaxY(), 1);
                GeoConverter geotools = GeotoolsConverter.getInstance();
                GeoPosition bbTopLeft = geotools.convert(topLeft, epsg);
                GeoPosition bbBottomRight = geotools.convert(bottomRight, epsg);
                double minX = Math.min(bbTopLeft.getX(), bbBottomRight.getX());
                double minY = Math.min(bbTopLeft.getY(), bbBottomRight.getY());
                double maxX = Math.max(bbTopLeft.getX(), bbBottomRight.getX());
                double maxY = Math.max(bbTopLeft.getY(), bbBottomRight.getY());
                BoundingBox tile = new BoundingBox(minX, minY, -1, maxX, maxY, 1);
                // check if bounding box and tile overlap
                if (wms.intersectsOrCovers(tile) || tile.covers(wms)) {
                    WMSBounds bounds;
                    if (epsg == mapEpsg) {
                        bounds = new WMSBounds(srs, minX, minY, maxX, maxY);
                    } else {
                        // determine bounds for request
                        minX = Math.min(topLeft.getX(), bottomRight.getX());
                        minY = Math.min(topLeft.getY(), bottomRight.getY());
                        maxX = Math.max(topLeft.getX(), bottomRight.getX());
                        maxY = Math.max(topLeft.getY(), bottomRight.getY());
                        // $NON-NLS-1$
                        bounds = new WMSBounds("EPSG:" + mapEpsg, minX, minY, maxX, maxY);
                    }
                    URI uri;
                    synchronized (this) {
                        uri = WMSUtil.getMapURI(capabilities, configuration, width, height, bounds, null, format, true);
                    }
                    Proxy proxy = ProxyUtil.findProxy(uri);
                    InputStream in = uri.toURL().openConnection(proxy).getInputStream();
                    BufferedImage image = GraphicsUtilities.loadCompatibleImage(in);
                    // apply transparency to the image
                    BufferedImage result = GraphicsUtilities.createCompatibleTranslucentImage(image.getWidth(), image.getHeight());
                    Graphics2D g = result.createGraphics();
                    try {
                        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f);
                        g.setComposite(ac);
                        g.drawImage(image, 0, 0, null);
                    } finally {
                        g.dispose();
                    }
                    return result;
                }
            } catch (Throwable e) {
                // $NON-NLS-1$
                log.warn("Error painting WMS overlay", e);
            }
        }
    }
    return null;
}
Also used : WMSBounds(de.fhg.igd.mapviewer.server.wms.capabilities.WMSBounds) InputStream(java.io.InputStream) AlphaComposite(java.awt.AlphaComposite) Point(java.awt.Point) URI(java.net.URI) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) WMSCapabilitiesException(de.fhg.igd.mapviewer.server.wms.capabilities.WMSCapabilitiesException) Graphics2D(java.awt.Graphics2D) Proxy(java.net.Proxy) BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) GeoConverter(org.jdesktop.swingx.mapviewer.GeoConverter)

Aggregations

BoundingBox (de.fhg.igd.geom.BoundingBox)1 WMSBounds (de.fhg.igd.mapviewer.server.wms.capabilities.WMSBounds)1 WMSCapabilitiesException (de.fhg.igd.mapviewer.server.wms.capabilities.WMSCapabilitiesException)1 AlphaComposite (java.awt.AlphaComposite)1 Graphics2D (java.awt.Graphics2D)1 Point (java.awt.Point)1 BufferedImage (java.awt.image.BufferedImage)1 InputStream (java.io.InputStream)1 Proxy (java.net.Proxy)1 URI (java.net.URI)1 GeoConverter (org.jdesktop.swingx.mapviewer.GeoConverter)1 GeoPosition (org.jdesktop.swingx.mapviewer.GeoPosition)1