Search in sources :

Example 1 with MultiArea

use of de.fhg.igd.mapviewer.marker.area.MultiArea 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 2 with MultiArea

use of de.fhg.igd.mapviewer.marker.area.MultiArea in project hale by halestudio.

the class InstanceMarker method doPaintMarker.

/**
 * @see BoundingBoxMarker#doPaintMarker(Graphics2D, SelectableWaypoint,
 *      PixelConverter, int, int, int, int, int, Rectangle, boolean)
 */
@Override
protected Area doPaintMarker(Graphics2D g, InstanceWaypoint context, PixelConverter converter, int zoom, int minX, int minY, int maxX, int maxY, Rectangle gBounds, boolean calulateArea) {
    List<Area> areas = (!calulateArea) ? (null) : (new ArrayList<Area>());
    List<GeometryProperty<?>> geometries = context.getGeometries();
    // map CRS
    CoordinateReferenceSystem mapCRS;
    try {
        mapCRS = CRSDecode.getLonLatCRS(converter.getMapEpsg());
    // map (GeoPosition) assumes lon/lat order
    } catch (Throwable e) {
        log.error("Could not decode map CRS", e);
        return null;
    }
    // paint each geometry
    for (GeometryProperty<?> geometry : geometries) {
        Area geometryArea = paintGeometry(g, geometry.getCRSDefinition(), geometry.getGeometry(), context, converter, zoom, geometries.size() == 1, gBounds, mapCRS, calulateArea);
        if (areas != null && geometryArea != null) {
            areas.add(geometryArea);
        }
    }
    if (areas == null) {
        return null;
    }
    if (areas.size() == 1) {
        return areas.get(0);
    } else if (!areas.isEmpty()) {
        return new MultiArea(areas);
    }
    return null;
}
Also used : Area(de.fhg.igd.mapviewer.marker.area.Area) PolygonArea(de.fhg.igd.mapviewer.marker.area.PolygonArea) MultiArea(de.fhg.igd.mapviewer.marker.area.MultiArea) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) ArrayList(java.util.ArrayList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiArea(de.fhg.igd.mapviewer.marker.area.MultiArea)

Example 3 with MultiArea

use of de.fhg.igd.mapviewer.marker.area.MultiArea in project hale by halestudio.

the class InstanceMarker method paintLine.

/**
 * Paint a line string geometry.
 *
 * @param geometry the line string
 * @param g the graphics object to paint on
 * @param crsDefinition the CRS definition associated to the geometry
 * @param context the context
 * @param converter the pixel converter
 * @param zoom the zoom level
 * @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 polygon area or <code>null</code> if painting failed
 */
protected Area paintLine(LineString geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
    Coordinate[] coordinates = geometry.getCoordinates();
    if (coordinates.length <= 0) {
        return null;
    }
    if (coordinates.length == 1) {
        // fall back to point drawing
        Point point = getGeometryFactory().createPoint(coordinates[0]);
        return paintPoint(point, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
    }
    try {
        // get CRS converter
        CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
        List<Point2D> mapPoints = new ArrayList<Point2D>(coordinates.length);
        for (Coordinate coord : coordinates) {
            // manually convert to map CRS
            Point3D mapPoint = conv.convert(coord.x, coord.y, 0);
            GeoPosition pos = new GeoPosition(mapPoint.getX(), mapPoint.getY(), converter.getMapEpsg());
            Point2D point = converter.geoToPixel(pos, zoom);
            mapPoints.add(point);
        }
        if (applyStroke(g, context)) {
            for (int i = 0; i < mapPoints.size() - 1; i++) {
                // draw each connecting line
                Point2D p1 = mapPoints.get(i);
                Point2D p2 = mapPoints.get(i + 1);
                g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
            }
        } else {
            log.warn("Stroke disabled in style, LineString is not rendered");
        }
        if (!calculateArea) {
            return null;
        }
        // use a buffer around the line as area
        // XXX
        java.awt.Polygon[] buffer = createBufferPolygon(mapPoints, 3);
        // ok?
        if (buffer.length == 0) {
            return null;
        } else if (buffer.length == 1) {
            return new PolygonArea(buffer[0]);
        } else {
            Collection<Area> areas = new ArrayList<Area>();
            for (java.awt.Polygon bufferPoly : buffer) {
                areas.add(new PolygonArea(bufferPoly));
            }
            return new MultiArea(areas);
        }
    } catch (Exception e) {
        log.error("Error painting instance polygon geometry", e);
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) SelectableWaypoint(de.fhg.igd.mapviewer.waypoints.SelectableWaypoint) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) TransformException(org.opengis.referencing.operation.TransformException) PolygonArea(de.fhg.igd.mapviewer.marker.area.PolygonArea) 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) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point2D(java.awt.geom.Point2D) Point3D(de.fhg.igd.geom.Point3D) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) Collection(java.util.Collection) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) Polygon(com.vividsolutions.jts.geom.Polygon) MultiArea(de.fhg.igd.mapviewer.marker.area.MultiArea)

Aggregations

Area (de.fhg.igd.mapviewer.marker.area.Area)3 MultiArea (de.fhg.igd.mapviewer.marker.area.MultiArea)3 PolygonArea (de.fhg.igd.mapviewer.marker.area.PolygonArea)3 ArrayList (java.util.ArrayList)3 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)2 Point (com.vividsolutions.jts.geom.Point)2 Polygon (com.vividsolutions.jts.geom.Polygon)2 SelectableWaypoint (de.fhg.igd.mapviewer.waypoints.SelectableWaypoint)2 CRSConverter (eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter)2 Point2D (java.awt.geom.Point2D)2 GeoPosition (org.jdesktop.swingx.mapviewer.GeoPosition)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 LineString (com.vividsolutions.jts.geom.LineString)1 BoundingBox (de.fhg.igd.geom.BoundingBox)1 Point3D (de.fhg.igd.geom.Point3D)1 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)1 Rectangle (java.awt.Rectangle)1 Collection (java.util.Collection)1 IllegalGeoPositionException (org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)1