Search in sources :

Example 31 with GeoPosition

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

the class AreaCalc method shoelaceFormula.

/**
 * Gauss' Area Formula
 *
 * @param positions polygon
 *
 * @return the base of the polygon
 */
private double shoelaceFormula(List<GeoPosition> positions) {
    double result = 0;
    // int epsg = positions.get(0).getEpsgCode();
    ArrayList<Double> listY = new ArrayList<Double>();
    ArrayList<Double> listX = new ArrayList<Double>();
    for (GeoPosition v : positions) {
        listY.add(v.getY());
        listX.add(v.getX());
    }
    for (int i = 0; i < listX.size(); i++) {
        result = result + listX.get(i) * (listY.get(getFirst(i, listY)) - listY.get(getSecond(i, listY)));
    }
    result = result / 2;
    // 
    double reduction = 0.0;
    return Math.abs(result - reduction);
}
Also used : ArrayList(java.util.ArrayList) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition)

Example 32 with GeoPosition

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

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

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