Search in sources :

Example 1 with GeoPosition

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

the class BBOXPage method updateMap.

/**
 * Update the map (i.e. set the map server).
 *
 * @param caps the WFS capabilities
 */
private void updateMap(WFSCapabilities caps) {
    CustomTileMapServer tileServer = new CustomTileMapServer();
    // use Stamen Terrain as default map here
    tileServer.setUrlPattern("http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg");
    tileServer.setAttributionText("Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.");
    tileServer.setZoomLevel(16);
    MapServer server = tileServer;
    Set<GeoPosition> positions = null;
    if (caps != null) {
        /*
			 * TODO optimal solution would be using the WMS that serves the
			 * layers corresponding to the feature types
			 */
        // collect BBs from feature types
        Set<QName> types = new HashSet<>(getWizard().getConfiguration().getTypeNames());
        if (types.isEmpty()) {
        // no features will be selected
        } else {
            List<BBox> bbs = new ArrayList<>();
            for (QName type : types) {
                FeatureTypeInfo info = caps.getFeatureTypes().get(type);
                if (info != null && info.getWgs84BBox() != null) {
                    bbs.add(info.getWgs84BBox());
                }
            }
            if (!bbs.isEmpty()) {
                double minX, maxX, minY, maxY;
                Iterator<BBox> it = bbs.iterator();
                BBox bb = it.next();
                minX = bb.getX1();
                minY = bb.getY1();
                maxX = bb.getX2();
                maxY = bb.getY2();
                while (it.hasNext()) {
                    bb = it.next();
                    minX = Math.min(minX, bb.getX1());
                    minY = Math.min(minY, bb.getY1());
                    maxX = Math.max(maxX, bb.getX2());
                    maxY = Math.max(maxY, bb.getY2());
                }
                GeoPosition topLeft = new GeoPosition(minX, maxY, 4326);
                GeoPosition bottomRight = new GeoPosition(maxX, minY, 4326);
                Color back = mapKit.getBackground();
                server = new ClippingMapServer(server, topLeft, bottomRight, new Color(back.getRed(), back.getGreen(), back.getBlue(), 170));
                positions = new HashSet<>();
                positions.add(topLeft);
                positions.add(bottomRight);
            } else {
            // ignore BBs, provide full map
            }
        }
    }
    mapKit.setServer(server, true);
    if (positions != null) {
        try {
            mapKit.zoomToPositions(positions);
        } catch (Exception e) {
        // ignore error
        }
    }
    mapKit.refresh();
}
Also used : QName(javax.xml.namespace.QName) Color(java.awt.Color) ArrayList(java.util.ArrayList) ClippingMapServer(eu.esdihumboldt.hale.io.wfs.ui.getfeature.internal.ClippingMapServer) CustomTileMapServer(de.fhg.igd.mapviewer.server.tiles.CustomTileMapServer) BBox(eu.esdihumboldt.hale.io.wfs.capabilities.BBox) MapServer(de.fhg.igd.mapviewer.server.MapServer) ClippingMapServer(eu.esdihumboldt.hale.io.wfs.ui.getfeature.internal.ClippingMapServer) CustomTileMapServer(de.fhg.igd.mapviewer.server.tiles.CustomTileMapServer) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) FeatureTypeInfo(eu.esdihumboldt.hale.io.wfs.capabilities.FeatureTypeInfo) HashSet(java.util.HashSet)

Example 2 with GeoPosition

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

the class StyledMapUtil method zoomToSelection.

/**
 * Zoom to the selected instances. Does nothing if the selection is empty or
 * contains no {@link Instance}s or {@link InstanceReference}s.
 *
 * @param mapKit the map kit
 * @param selection the selection
 */
public static void zoomToSelection(BasicMapKit mapKit, IStructuredSelection selection) {
    BoundingBox bb = null;
    // determine bounding box for each reference and accumulate it
    for (AbstractInstancePainter painter : mapKit.getTilePainters(AbstractInstancePainter.class)) {
        for (Object element : selection.toList()) {
            InstanceReference ref = getReference(element);
            if (ref != null) {
                InstanceWaypoint wp = painter.findWaypoint(ref);
                if (wp != null) {
                    BoundingBox wpBB = wp.getBoundingBox();
                    if (wpBB.checkIntegrity() && !wpBB.isEmpty()) {
                        if (bb == null) {
                            bb = new BoundingBox(wpBB);
                        } else {
                            bb.add(wpBB);
                        }
                    }
                }
            }
        }
    }
    if (bb != null) {
        Set<GeoPosition> positions = new HashSet<GeoPosition>();
        positions.add(new GeoPosition(bb.getMinX(), bb.getMinY(), SelectableWaypoint.COMMON_EPSG));
        positions.add(new GeoPosition(bb.getMaxX(), bb.getMaxY(), SelectableWaypoint.COMMON_EPSG));
        mapKit.zoomToPositions(positions);
    }
}
Also used : InstanceWaypoint(eu.esdihumboldt.hale.ui.views.styledmap.painter.InstanceWaypoint) AbstractInstancePainter(eu.esdihumboldt.hale.ui.views.styledmap.painter.AbstractInstancePainter) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference) BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) HashSet(java.util.HashSet)

Example 3 with GeoPosition

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

the class StyledMapUtil method zoomToAll.

/**
 * Zoom to all instances available in the map. Does nothing if there are no
 * instances displayed.
 *
 * @param mapKit the map kit
 */
public static void zoomToAll(BasicMapKit mapKit) {
    BoundingBox bb = null;
    // determine bounding box
    for (AbstractInstancePainter painter : mapKit.getTilePainters(AbstractInstancePainter.class)) {
        BoundingBox painterBB = painter.getBoundingBox();
        if (painterBB.checkIntegrity() && !painterBB.isEmpty()) {
            if (bb == null) {
                bb = new BoundingBox(painterBB);
            } else {
                bb.add(painterBB);
            }
        }
    }
    if (bb != null) {
        Set<GeoPosition> positions = new HashSet<GeoPosition>();
        positions.add(new GeoPosition(bb.getMinX(), bb.getMinY(), SelectableWaypoint.COMMON_EPSG));
        positions.add(new GeoPosition(bb.getMaxX(), bb.getMaxY(), SelectableWaypoint.COMMON_EPSG));
        mapKit.zoomToPositions(positions);
    }
}
Also used : AbstractInstancePainter(eu.esdihumboldt.hale.ui.views.styledmap.painter.AbstractInstancePainter) BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) HashSet(java.util.HashSet)

Example 4 with GeoPosition

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

the class InstanceMarker method paintGeometry.

/**
 * Paint a geometry.
 *
 * @param g the graphics to paint on
 * @param crsDefinition the CRS definition associated with the geometry
 * @param geometry the geometry
 * @param context the context
 * @param converter the pixel converter
 * @param zoom the zoom level
 * @param singleGeometry if this is the only geometry associated to the
 *            marker
 * @param gBounds the graphics bounds
 * @param mapCRS the map coordinate reference system
 * @param calculateArea if the area representing the marker should be
 *            calculated, if <code>false</code> is given here the return
 *            value is ignored and should be <code>null</code>
 * @return the area the geometry occupies (in pixel coordinates), or
 *         <code>null</code> if nothing has been painted
 */
protected Area paintGeometry(Graphics2D g, CRSDefinition crsDefinition, Geometry geometry, InstanceWaypoint context, PixelConverter converter, int zoom, boolean singleGeometry, Rectangle gBounds, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
    if (geometry instanceof GeometryCollection) {
        // paint each geometry in a geometry collection
        List<Area> areas = (calculateArea) ? (new ArrayList<Area>()) : (null);
        GeometryCollection collection = (GeometryCollection) geometry;
        for (int i = 0; i < collection.getNumGeometries(); i++) {
            Geometry geom = collection.getGeometryN(i);
            Area geomArea = paintGeometry(g, crsDefinition, geom, context, converter, zoom, singleGeometry && collection.getNumGeometries() == 1, gBounds, mapCRS, calculateArea);
            if (areas != null && geomArea != null) {
                areas.add(geomArea);
            }
        }
        if (areas == null || areas.isEmpty()) {
            return null;
        } else {
            return new MultiArea(areas);
        }
    }
    // if it is the only geometry the check that was already made is OK
    if (!calculateArea && !singleGeometry) {
        // we can safely return null inside this method, as no area has to
        // be calculated
        // determine bounding box
        BoundingBox geometryBB;
        synchronized (geometryMapBBs) {
            // retrieve cached bounding box
            geometryBB = geometryMapBBs.get(geometry);
            if (geometryBB == null) {
                // if none available, try to calculate BB
                BoundingBox calcBB = BoundingBox.compute(geometry);
                if (calcBB != null && calcBB.checkIntegrity()) {
                    try {
                        // get CRS converter
                        CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
                        // manually convert to map CRS
                        geometryBB = conv.convert(calcBB);
                        // put BB in cache
                        geometryMapBBs.put(geometry, geometryBB);
                    } catch (Throwable e) {
                        log.error("Error checking geometry bounding box", e);
                        return null;
                    }
                }
            }
        }
        if (geometryBB != null) {
            try {
                GeoPosition minCorner = new GeoPosition(geometryBB.getMinX(), geometryBB.getMinY(), converter.getMapEpsg());
                GeoPosition maxCorner = new GeoPosition(geometryBB.getMaxX(), geometryBB.getMaxY(), converter.getMapEpsg());
                // determine pixel coordinates
                Point2D minPixels = converter.geoToPixel(minCorner, zoom);
                Point2D maxPixels = converter.geoToPixel(maxCorner, zoom);
                // geometry pixel bounding box
                int minX = Math.min((int) minPixels.getX(), (int) maxPixels.getX());
                int minY = Math.min((int) minPixels.getY(), (int) maxPixels.getY());
                int maxX = Math.max((int) minPixels.getX(), (int) maxPixels.getX());
                int maxY = Math.max((int) minPixels.getY(), (int) maxPixels.getY());
                // add overlap
                minX -= GEOMETRY_PIXEL_BB_OVERLAP;
                minY -= GEOMETRY_PIXEL_BB_OVERLAP;
                maxX += GEOMETRY_PIXEL_BB_OVERLAP;
                maxY += GEOMETRY_PIXEL_BB_OVERLAP;
                // create bounding box
                Rectangle geometryPixelBB = new Rectangle(minX, minY, maxX - minX, maxY - minY);
                if (!gBounds.intersects(geometryPixelBB) && !gBounds.contains(geometryPixelBB)) {
                    // geometry does not lie in tile
                    return null;
                }
            } catch (Throwable e) {
                log.error("Error checking geometry bounding box", e);
                return null;
            }
        } else {
            // empty or invalid bounding box
            return null;
        }
    }
    if (geometry instanceof Point) {
        return paintPoint((Point) geometry, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
    }
    if (geometry instanceof Polygon) {
        return paintPolygon((Polygon) geometry, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
    }
    if (geometry instanceof LineString) {
        return paintLine((LineString) geometry, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) SelectableWaypoint(de.fhg.igd.mapviewer.waypoints.SelectableWaypoint) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) Geometry(com.vividsolutions.jts.geom.Geometry) Area(de.fhg.igd.mapviewer.marker.area.Area) PolygonArea(de.fhg.igd.mapviewer.marker.area.PolygonArea) MultiArea(de.fhg.igd.mapviewer.marker.area.MultiArea) CRSConverter(eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter) Point2D(java.awt.geom.Point2D) LineString(com.vividsolutions.jts.geom.LineString) BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) Polygon(com.vividsolutions.jts.geom.Polygon) MultiArea(de.fhg.igd.mapviewer.marker.area.MultiArea)

Example 5 with GeoPosition

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

the class StyledInstanceMarker method paintPoint.

/**
 * @see InstanceMarker#paintPoint(Point, Graphics2D, CRSDefinition,
 *      InstanceWaypoint, PixelConverter, int, CoordinateReferenceSystem,
 *      boolean)
 */
@Override
protected Area paintPoint(Point geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
    initStyle(context);
    Area area = null;
    try {
        if (pointSymbolizer == null || (SLD.mark(pointSymbolizer) == null && pointSymbolizer.getGraphic().graphicalSymbols().isEmpty())) {
            // generic
            return super.paintFallback(g, context, converter, zoom, null, calculateArea);
        }
        // get CRS converter
        CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
        // manually convert to map CRS
        Point3D mapPoint = conv.convert(geometry.getX(), geometry.getY(), 0);
        GeoPosition pos = new GeoPosition(mapPoint.getX(), mapPoint.getY(), converter.getMapEpsg());
        // determine pixel coordinates
        Point2D point = converter.geoToPixel(pos, zoom);
        Coordinate coordinate = new Coordinate(point.getX(), point.getY());
        Point newPoint = geometry.getFactory().createPoint(coordinate);
        // create a LiteShape and instantiate the Painter and the
        // StyleFactory
        LiteShape2 lites = new LiteShape2(newPoint, null, null, false);
        StyledShapePainter ssp = new StyledShapePainter();
        SLDStyleFactory styleFactory = new SLDStyleFactory();
        Range<Double> range = new Range<Double>(Double.class, 0.5, 1.5);
        PointSymbolizer pointS;
        // is the Waypoint selected?
        if (context.isSelected()) {
            // switch to the SelectionSymbolizer
            pointS = getSelectionSymbolizer(pointSymbolizer);
        } else
            // use the specific PointSymbolizer
            pointS = pointSymbolizer;
        // Create the Style2D object for painting with the use of a
        // DummyFeature wich extends SimpleFeatures
        // because Geotools can only work with that
        DummyFeature dummy = new DummyFeature();
        Style2D style2d = styleFactory.createStyle(dummy, pointS, range);
        // create the area object of the painted image for further use
        area = getArea(point, style2d, lites);
        // actually paint
        ssp.paint(g, lites, style2d, 1);
        // graphic)
        if (context.isSelected() && style2d instanceof GraphicStyle2D) {
            GraphicStyle2D gs2d = (GraphicStyle2D) style2d;
            // get minX and minY for the drawn rectangle arround the image
            int minX = (int) point.getX() - gs2d.getImage().getWidth() / 2;
            int minY = (int) point.getY() - gs2d.getImage().getHeight() / 2;
            // apply the specification of the selection rectangle
            applyFill(g, context);
            applyStroke(g, context);
            // draw the selection rectangle
            g.drawRect(minX - 1, minY - 1, gs2d.getImage().getWidth() + 1, gs2d.getImage().getHeight() + 1);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return area;
}
Also used : PointSymbolizer(org.geotools.styling.PointSymbolizer) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Range(org.geotools.util.Range) SelectableWaypoint(de.fhg.igd.mapviewer.waypoints.SelectableWaypoint) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) GraphicStyle2D(org.geotools.renderer.style.GraphicStyle2D) MarkStyle2D(org.geotools.renderer.style.MarkStyle2D) Style2D(org.geotools.renderer.style.Style2D) BoxArea(de.fhg.igd.mapviewer.marker.area.BoxArea) Area(de.fhg.igd.mapviewer.marker.area.Area) GraphicStyle2D(org.geotools.renderer.style.GraphicStyle2D) CRSConverter(eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter) Point2D(java.awt.geom.Point2D) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point3D(de.fhg.igd.geom.Point3D) SLDStyleFactory(org.geotools.renderer.style.SLDStyleFactory) LiteShape2(org.geotools.geometry.jts.LiteShape2) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) StyledShapePainter(org.geotools.renderer.lite.StyledShapePainter)

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