Search in sources :

Example 6 with GeoPosition

use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.

the class GeoUtil method getPosition.

/**
 * convert an on screen pixel coordinate and a zoom level to a geo position
 *
 * @param pixelCoordinate the pixel coordinate
 * @param zoom the zoom level
 * @param info the tile factory info
 *
 * @return the corresponding geo position in WGS84
 */
public static GeoPosition getPosition(Point2D pixelCoordinate, int zoom, TileFactoryInfo info) {
    // p(" --bitmap to latlon : " + coord + " " + zoom);
    double wx = pixelCoordinate.getX();
    double wy = pixelCoordinate.getY();
    // this reverses getBitmapCoordinates
    double flon = (wx - info.getMapCenterInPixelsAtZoom(zoom).getX()) / info.getLongitudeDegreeWidthInPixels(zoom);
    double e1 = (wy - info.getMapCenterInPixelsAtZoom(zoom).getY()) / (-1 * info.getLongitudeRadianWidthInPixels(zoom));
    double e2 = (2 * Math.atan(Math.exp(e1)) - Math.PI / 2) / (Math.PI / 180.0);
    double flat = e2;
    GeoPosition wc = new GeoPosition(flon, flat, GeoPosition.WGS_84_EPSG);
    return wc;
}
Also used : GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition)

Example 7 with GeoPosition

use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.

the class WMSUtil method getBoundingBox.

/**
 * Get the preferred bounding box. Tries to convert the bounding box if not
 * found with the preferred SRS
 *
 * @param capabilities the WMS capabilities
 * @param preferredEpsg the preferred EPSG code
 * @return the preferred bounding box, an available bounding box or
 *         <code>null</code> if none is available
 */
public static WMSBounds getBoundingBox(WMSCapabilities capabilities, int preferredEpsg) {
    WMSBounds bounds = getPreferredBoundingBox(capabilities, preferredEpsg);
    String srs = "EPSG:" + preferredEpsg;
    if (bounds.getSRS().equals(srs)) {
        // matches preferred SRS
        return bounds;
    } else {
        try {
            // try to convert the bounding box to the preferred SRS
            int boxEpsg = Integer.parseInt(bounds.getSRS().substring(5));
            GeoPosition topLeft = new GeoPosition(bounds.getMinX(), bounds.getMinY(), boxEpsg);
            GeoPosition bottomRight = new GeoPosition(bounds.getMaxX(), bounds.getMaxY(), boxEpsg);
            topLeft = GeotoolsConverter.getInstance().convert(topLeft, preferredEpsg);
            bottomRight = GeotoolsConverter.getInstance().convert(bottomRight, preferredEpsg);
            return new WMSBounds(srs, Math.min(topLeft.getX(), bottomRight.getX()), Math.min(topLeft.getY(), bottomRight.getY()), Math.max(topLeft.getX(), bottomRight.getX()), Math.max(topLeft.getY(), bottomRight.getY()));
        } catch (Exception e) {
            // fall back to bounds
            return bounds;
        }
    }
}
Also used : GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with GeoPosition

use of org.jdesktop.swingx.mapviewer.GeoPosition 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)

Example 9 with GeoPosition

use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.

the class BasicMapKit method generateBoundingRect.

private Rectangle2D generateBoundingRect(double minX, double minY, double maxX, double maxY, int epsg, int zoom) throws IllegalGeoPositionException {
    java.awt.geom.Point2D p1 = getMainMap().getTileFactory().getTileProvider().getConverter().geoToPixel(new GeoPosition(minX, minY, epsg), zoom);
    java.awt.geom.Point2D p2 = getMainMap().getTileFactory().getTileProvider().getConverter().geoToPixel(new GeoPosition(maxX, maxY, epsg), zoom);
    return new Rectangle2D.Double((p1.getX() < p2.getX()) ? (p1.getX()) : (p2.getX()), (p1.getY() < p2.getY()) ? (p1.getY()) : (p2.getY()), Math.abs(p2.getX() - p1.getX()), Math.abs(p2.getY() - p1.getY()));
}
Also used : GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition)

Example 10 with GeoPosition

use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.

the class ExtendedMapKit method restoreState.

/**
 * @see IPersistableEditor#restoreState(IMemento)
 */
@Override
public void restoreState(IMemento memento) {
    if (memento == null)
        return;
    Integer oldEpsg = memento.getInteger(MEMENTO_KEY_EPSG);
    if (oldEpsg != null) {
        GeoPosition p1 = new GeoPosition(memento.getFloat(MEMENTO_KEY_MIN_X), memento.getFloat(MEMENTO_KEY_MIN_Y), oldEpsg);
        GeoPosition p2 = new GeoPosition(memento.getFloat(MEMENTO_KEY_MAX_X), memento.getFloat(MEMENTO_KEY_MAX_Y), oldEpsg);
        Set<GeoPosition> positions = new HashSet<GeoPosition>();
        positions.add(p1);
        positions.add(p2);
        zoomToPositions(positions);
    }
}
Also used : GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) HashSet(java.util.HashSet)

Aggregations

GeoPosition (org.jdesktop.swingx.mapviewer.GeoPosition)33 BoundingBox (de.fhg.igd.geom.BoundingBox)11 Point (java.awt.Point)10 IllegalGeoPositionException (org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)10 ArrayList (java.util.ArrayList)9 Point2D (java.awt.geom.Point2D)8 Point3D (de.fhg.igd.geom.Point3D)6 HashSet (java.util.HashSet)6 Area (de.fhg.igd.mapviewer.marker.area.Area)5 SelectableWaypoint (de.fhg.igd.mapviewer.waypoints.SelectableWaypoint)5 CRSConverter (eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter)5 Rectangle (java.awt.Rectangle)5 Point (com.vividsolutions.jts.geom.Point)4 PixelConverter (org.jdesktop.swingx.mapviewer.PixelConverter)4 Coordinate (com.vividsolutions.jts.geom.Coordinate)3 Polygon (com.vividsolutions.jts.geom.Polygon)3 PolygonArea (de.fhg.igd.mapviewer.marker.area.PolygonArea)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)2 MultiArea (de.fhg.igd.mapviewer.marker.area.MultiArea)2