Search in sources :

Example 16 with BoundingBox

use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.

the class BoundingBoxMarker method paintMarker.

/**
 * @see AbstractMarker#paintMarker(Graphics2D, Object, PixelConverter, int,
 *      Rectangle, boolean)
 */
@Override
protected Area paintMarker(Graphics2D g, T context, PixelConverter converter, int zoom, Rectangle gBounds, boolean calculateArea) {
    if (context.isPoint()) {
        return paintFallback(g, context, converter, zoom, gBounds, calculateArea);
    }
    BoundingBox bb = context.getBoundingBox();
    int code = SelectableWaypoint.COMMON_EPSG;
    GeoPosition pos1 = new GeoPosition(bb.getMinX(), bb.getMinY(), code);
    GeoPosition pos2 = new GeoPosition(bb.getMaxX(), bb.getMaxY(), code);
    try {
        Point2D p1 = converter.geoToPixel(pos1, zoom);
        Point2D p2 = converter.geoToPixel(pos2, zoom);
        int minX = (int) Math.min(p1.getX(), p2.getX());
        int minY = (int) Math.min(p1.getY(), p2.getY());
        int maxX = (int) Math.max(p1.getX(), p2.getX());
        int maxY = (int) Math.max(p1.getY(), p2.getY());
        int width = maxX - minX;
        int height = maxY - minY;
        // decide whether it is to small to paint
        if (isToSmall(width, height, zoom)) {
            return paintFallback(g, context, converter, zoom, gBounds, calculateArea);
        }
        return doPaintMarker(g, context, converter, zoom, minX, minY, maxX, maxY, gBounds, calculateArea);
    } catch (IllegalGeoPositionException e) {
        // use fallback marker instead
        return paintFallback(g, context, converter, zoom, gBounds, calculateArea);
    }
}
Also used : Point2D(java.awt.geom.Point2D) BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) SelectableWaypoint(de.fhg.igd.mapviewer.waypoints.SelectableWaypoint)

Example 17 with BoundingBox

use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.

the class CustomWaypointPainter method findWaypoint.

/**
 * Find a way-point at a given position
 *
 * @param point the position
 * @return the way-point
 */
public W findWaypoint(Point point) {
    Rectangle viewPort = getMapKit().getMainMap().getViewportBounds();
    // the overlap is the reason why
    final int overlap = getMaxOverlap();
    // the point is used instead of
    // a GeoPosition
    final int x = viewPort.x + point.x;
    final int y = viewPort.y + point.y;
    final int zoom = getMapKit().getMainMap().getZoom();
    final PixelConverter converter = getMapKit().getMainMap().getTileFactory().getTileProvider().getConverter();
    final Dimension mapSize = TileProviderUtils.getMapSize(getMapKit().getMainMap().getTileFactory().getTileProvider(), zoom);
    final int width = mapSize.width * getMapKit().getMainMap().getTileFactory().getTileProvider().getTileWidth(zoom);
    final int height = mapSize.height * getMapKit().getMainMap().getTileFactory().getTileProvider().getTileHeight(zoom);
    final GeoPosition topLeft = converter.pixelToGeo(new Point(Math.max(x - overlap, 0), Math.max(y - overlap, 0)), zoom);
    final GeoPosition bottomRight = converter.pixelToGeo(new Point(Math.min(x + overlap, width), Math.min(y + overlap, height)), zoom);
    BoundingBox searchBox;
    try {
        searchBox = createSearchBB(topLeft, bottomRight);
        Set<W> wps = waypoints.query(searchBox, new Verifier<W, BoundingBox>() {

            @Override
            public boolean verify(W wp, BoundingBox box) {
                try {
                    Point2D wpPixel = converter.geoToPixel(wp.getPosition(), zoom);
                    int relX = x - (int) wpPixel.getX();
                    int relY = y - (int) wpPixel.getY();
                    Area area = wp.getMarker().getArea(zoom);
                    if (area != null && area.contains(relX, relY)) {
                        // match
                        return true;
                    }
                } catch (IllegalGeoPositionException e) {
                    // $NON-NLS-1$
                    log.debug("Error converting waypoint position", e);
                }
                return false;
            }
        });
        if (wps == null || wps.isEmpty()) {
            return null;
        } else {
            if (wps.size() == 1) {
                return wps.iterator().next();
            } else {
                List<W> sorted = new ArrayList<W>(wps);
                Collections.sort(sorted, new Comparator<W>() {

                    @Override
                    public int compare(W o1, W o2) {
                        double a1 = o1.getMarker().getArea(zoom).getArea();
                        double a2 = o2.getMarker().getArea(zoom).getArea();
                        // compare size
                        if (a1 < a2) {
                            return -1;
                        } else if (a2 < a1) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                });
                return sorted.get(0);
            }
        }
    } catch (IllegalGeoPositionException e) {
        return null;
    }
}
Also used : Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) Dimension(java.awt.Dimension) Point(java.awt.Point) Point(java.awt.Point) Area(de.fhg.igd.mapviewer.marker.area.Area) Point2D(java.awt.geom.Point2D) BoundingBox(de.fhg.igd.geom.BoundingBox) PixelConverter(org.jdesktop.swingx.mapviewer.PixelConverter) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition)

Example 18 with BoundingBox

use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.

the class CustomWaypointPainter method findWaypoints.

/**
 * Find way-points in a rectangular area defined by the given
 * {@link GeoPosition}s
 *
 * @param topLeft the top left position
 * @param bottomRight the bottom right position
 * @param worldRect the bounding box in world pixel coordinates
 * @param converter the pixel converter
 * @param zoom the zoom level
 *
 * @return the way-points in the area
 */
public Set<W> findWaypoints(GeoPosition topLeft, GeoPosition bottomRight, final Rectangle worldRect, final PixelConverter converter, final int zoom) {
    BoundingBox searchBox;
    try {
        searchBox = createSearchBB(topLeft, bottomRight);
        final BoundingBox verifyBox = searchBox;
        Set<W> wps = waypoints.query(searchBox, new Verifier<W, BoundingBox>() {

            @Override
            public boolean verify(W wp, BoundingBox second) {
                try {
                    Point2D wpPixel = converter.geoToPixel(wp.getPosition(), zoom);
                    int dx = (int) wpPixel.getX();
                    int dy = (int) wpPixel.getY();
                    worldRect.translate(-dx, -dy);
                    try {
                        Area area = wp.getMarker().getArea(zoom);
                        if (area != null) {
                            return area.containedIn(worldRect);
                        } else {
                            // not be selected
                            return false;
                        }
                    } finally {
                        worldRect.translate(dx, dy);
                    }
                } catch (IllegalGeoPositionException e) {
                    log.warn("Could not convert waypoint position to pixel", e);
                    // fall back to simple method
                    return verifyBox.covers(wp.getBoundingBox());
                }
            }
        });
        if (wps == null) {
            return new HashSet<W>();
        } else {
            return wps;
        }
    } catch (IllegalGeoPositionException e) {
        return new HashSet<W>();
    }
}
Also used : Area(de.fhg.igd.mapviewer.marker.area.Area) Point2D(java.awt.geom.Point2D) BoundingBox(de.fhg.igd.geom.BoundingBox) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) HashSet(java.util.HashSet)

Example 19 with BoundingBox

use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.

the class SelectableWaypoint method getBoundingBox.

/**
 * @see Localizable#getBoundingBox()
 */
@Override
public BoundingBox getBoundingBox() {
    if (box == null) {
        GeoPosition pos;
        try {
            pos = GeotoolsConverter.getInstance().convert(getPosition(), COMMON_EPSG);
            box = new BoundingBox(pos.getX() - POSITION_EXPAND, pos.getY() - POSITION_EXPAND, 0.0, pos.getX() + POSITION_EXPAND, pos.getY() + POSITION_EXPAND, 1.0);
        } catch (IllegalGeoPositionException e) {
            // $NON-NLS-1$
            log.warn("Error creating bounding box for waypoint");
        }
    }
    return box;
}
Also used : BoundingBox(de.fhg.igd.geom.BoundingBox) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)

Example 20 with BoundingBox

use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.

the class SpatialIndexInstanceProcessor method process.

/**
 * @see eu.esdihumboldt.hale.common.instance.processing.InstanceProcessor#process(eu.esdihumboldt.hale.common.instance.model.Instance,
 *      eu.esdihumboldt.hale.common.instance.model.InstanceReference)
 */
@Override
public void process(Instance instance, InstanceReference reference) {
    SpatialIndexService<Localizable, Localizable> index = getSpatialIndexService();
    final GeometryFinder finder = new GeometryFinder(null);
    InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
    traverser.traverse(instance, finder);
    final List<Geometry> geometries = new ArrayList<>();
    for (GeometryProperty<?> property : finder.getGeometries()) {
        Geometry g = property.getGeometry();
        for (int i = 0; i < g.getNumGeometries(); i++) {
            geometries.add(g.getGeometryN(i));
        }
    }
    final BoundingBox boundingBox = new BoundingBox();
    for (Geometry geometry : geometries) {
        boundingBox.add(BoundingBox.compute(geometry));
    }
    if (boundingBox.checkIntegrity()) {
        TypedInstanceReference typedRef = new TypedInstanceReference(reference, instance.getDefinition());
        index.insert(new LocalizableInstanceReference(typedRef, boundingBox));
    }
}
Also used : LocalizableInstanceReference(eu.esdihumboldt.hale.common.instance.index.LocalizableInstanceReference) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) ArrayList(java.util.ArrayList) Localizable(de.fhg.igd.geom.Localizable) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Geometry(com.vividsolutions.jts.geom.Geometry) BoundingBox(de.fhg.igd.geom.BoundingBox) TypedInstanceReference(eu.esdihumboldt.hale.common.instance.index.TypedInstanceReference)

Aggregations

BoundingBox (de.fhg.igd.geom.BoundingBox)20 GeoPosition (org.jdesktop.swingx.mapviewer.GeoPosition)11 ArrayList (java.util.ArrayList)8 Localizable (de.fhg.igd.geom.Localizable)6 IllegalGeoPositionException (org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)6 Point2D (java.awt.geom.Point2D)5 Area (de.fhg.igd.mapviewer.marker.area.Area)4 Point (java.awt.Point)4 HashSet (java.util.HashSet)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 Rectangle (java.awt.Rectangle)3 SelectableWaypoint (de.fhg.igd.mapviewer.waypoints.SelectableWaypoint)2 InstanceReference (eu.esdihumboldt.hale.common.instance.model.InstanceReference)2 AbstractInstancePainter (eu.esdihumboldt.hale.ui.views.styledmap.painter.AbstractInstancePainter)2 CRSConverter (eu.esdihumboldt.hale.ui.views.styledmap.util.CRSConverter)2 Graphics2D (java.awt.Graphics2D)2 BufferedImage (java.awt.image.BufferedImage)2 PixelConverter (org.jdesktop.swingx.mapviewer.PixelConverter)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)1 LineString (com.vividsolutions.jts.geom.LineString)1