Search in sources :

Example 1 with CRSConverter

use of eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter 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 CRSConverter

use of eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter in project hale by halestudio.

the class InstanceMarker method paintPolygon.

/**
 * Paint a polygon geometry.
 *
 * @param geometry the polygon
 * @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 paintPolygon(Polygon geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
    try {
        // get CRS converter
        CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
        // exterior
        Coordinate[] coordinates = geometry.getExteriorRing().getCoordinates();
        java.awt.Polygon outerPolygon = createPolygon(coordinates, conv, converter, zoom);
        if (geometry.getNumInteriorRing() > 0) {
            // polygon has interior geometries
            java.awt.geom.Area drawArea = new java.awt.geom.Area(outerPolygon);
            // interior
            for (int i = 0; i < geometry.getNumInteriorRing(); i++) {
                LineString interior = geometry.getInteriorRingN(i);
                java.awt.Polygon innerPolygon = createPolygon(interior.getCoordinates(), conv, converter, zoom);
                drawArea.subtract(new java.awt.geom.Area(innerPolygon));
            }
            if (applyFill(g, context)) {
                g.fill(drawArea);
            }
            if (applyStroke(g, context)) {
                g.draw(drawArea);
            }
            if (calculateArea) {
                return new AdvancedPolygonArea(drawArea, outerPolygon);
            }
        } else {
            // visible)
            if (applyFill(g, context)) {
                g.fill(outerPolygon);
            }
            if (applyStroke(g, context)) {
                g.draw(outerPolygon);
            }
            if (calculateArea) {
                return new PolygonArea(outerPolygon);
            }
        }
        // no calculateArea set
        return null;
    } catch (Exception e) {
        log.error("Error painting instance polygon geometry", e);
        return null;
    }
}
Also used : 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) LineString(com.vividsolutions.jts.geom.LineString)

Example 3 with CRSConverter

use of eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter 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)

Example 4 with CRSConverter

use of eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter in project hale by halestudio.

the class AbstractInstancePainter method createWaypoint.

/**
 * Create a way-point for an instance
 *
 * @param instance the instance
 * @param instanceService the instance service
 * @return the created way-point or <code>null</code> if
 */
protected InstanceWaypoint createWaypoint(Instance instance, InstanceService instanceService) {
    // retrieve instance reference
    // ,
    InstanceReference ref = instanceService.getReference(instance);
    // getDataSet());
    BoundingBox bb = null;
    List<GeometryProperty<?>> geometries = new ArrayList<GeometryProperty<?>>(DefaultGeometryUtil.getDefaultGeometries(instance));
    ListIterator<GeometryProperty<?>> it = geometries.listIterator();
    while (it.hasNext()) {
        GeometryProperty<?> prop = it.next();
        // check if geometry is valid for display in map
        CoordinateReferenceSystem crs = (prop.getCRSDefinition() == null) ? (null) : (prop.getCRSDefinition().getCRS());
        if (crs == null) {
            // no CRS, can't display in map
            // remove from list
            it.remove();
        } else {
            Geometry geometry = prop.getGeometry();
            // determine geometry bounding box
            BoundingBox geometryBB = BoundingBox.compute(geometry);
            if (geometryBB == null) {
                // no valid bounding box for geometry
                it.remove();
            } else {
                try {
                    // get converter to way-point CRS
                    CRSConverter conv = CRSConverter.getConverter(crs, getWaypointCRS());
                    // convert BB to way-point SRS
                    geometryBB = conv.convert(geometryBB);
                    // add to instance bounding box
                    if (bb == null) {
                        bb = new BoundingBox(geometryBB);
                    } else {
                        bb.add(geometryBB);
                    }
                } catch (Exception e) {
                    log.error("Error converting instance bounding box to waypoint bounding box", e);
                    // ignore geometry
                    it.remove();
                }
            }
        }
    }
    if (bb == null || geometries.isEmpty()) {
        // don't create way-point w/o geometries
        return null;
    }
    // use bounding box center as GEO position
    Point3D center = bb.getCenter();
    GeoPosition pos = new GeoPosition(center.getX(), center.getY(), GenericWaypoint.COMMON_EPSG);
    // buffer bounding box if x or y dimension empty
    if (bb.getMinX() == bb.getMaxX()) {
        bb.setMinX(bb.getMinX() - BUFFER_VALUE);
        bb.setMaxX(bb.getMaxX() + BUFFER_VALUE);
    }
    if (bb.getMinY() == bb.getMaxY()) {
        bb.setMinY(bb.getMinY() - BUFFER_VALUE);
        bb.setMaxY(bb.getMaxY() + BUFFER_VALUE);
    }
    // set dummy z range (otherwise the RTree can't deal correctly with it)
    bb.setMinZ(-BUFFER_VALUE);
    bb.setMaxZ(BUFFER_VALUE);
    String name = findInstanceName(instance);
    // create the way-point
    // XXX in abstract method?
    InstanceWaypoint wp = new InstanceWaypoint(pos, bb, ref, geometries, instance.getDefinition(), name);
    // each way-point must have its own marker, as the marker stores the
    // marker areas
    wp.setMarker(createMarker(wp));
    return wp;
}
Also used : GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Geometry(com.vividsolutions.jts.geom.Geometry) CRSConverter(eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter) PseudoInstanceReference(eu.esdihumboldt.hale.common.instance.model.impl.PseudoInstanceReference) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference) Point3D(de.fhg.igd.geom.Point3D) BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 5 with CRSConverter

use of eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter 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

CRSConverter (eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter)6 Point (com.vividsolutions.jts.geom.Point)5 SelectableWaypoint (de.fhg.igd.mapviewer.waypoints.SelectableWaypoint)5 GeoPosition (org.jdesktop.swingx.mapviewer.GeoPosition)5 Point3D (de.fhg.igd.geom.Point3D)4 Area (de.fhg.igd.mapviewer.marker.area.Area)4 PolygonArea (de.fhg.igd.mapviewer.marker.area.PolygonArea)4 Point2D (java.awt.geom.Point2D)4 Coordinate (com.vividsolutions.jts.geom.Coordinate)3 MultiArea (de.fhg.igd.mapviewer.marker.area.MultiArea)3 ArrayList (java.util.ArrayList)3 IllegalGeoPositionException (org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)3 TransformException (org.opengis.referencing.operation.TransformException)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)2 LineString (com.vividsolutions.jts.geom.LineString)2 Polygon (com.vividsolutions.jts.geom.Polygon)2 BoundingBox (de.fhg.igd.geom.BoundingBox)2 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)1 BoxArea (de.fhg.igd.mapviewer.marker.area.BoxArea)1