Search in sources :

Example 1 with IllegalGeoPositionException

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

the class LinearBoundsConverter method geoToPixel.

/**
 * @see PixelConverter#geoToPixel(GeoPosition, int)
 */
@Override
public Point2D geoToPixel(GeoPosition pos, final int zoom) throws IllegalGeoPositionException {
    // convert to desired epsg code
    pos = geoConverter.convert(pos, epsg);
    // check position - in bounds?
    if (pos.getX() < minX || pos.getX() > minX + xRange || pos.getY() < minY || pos.getY() > minY + yRange)
        // $NON-NLS-1$
        throw new IllegalGeoPositionException("GeoPosition out of bounds: " + pos);
    // calculate range fractions
    double xFrac = ((pos.getX() - minX) / xRange);
    double yFrac = ((pos.getY() - minY) / yRange);
    // reverse fractions if needed
    if (reverseX)
        xFrac = 1.0 - xFrac;
    if (reverseY)
        yFrac = 1.0 - yFrac;
    // swap fractions if needed
    if (swapAxes) {
        double tmpFrac = xFrac;
        xFrac = yFrac;
        yFrac = tmpFrac;
    }
    // calculate pixels
    try {
        double x = xFrac * tileProvider.getMapWidthInTiles(zoom) * tileProvider.getTileWidth(zoom);
        double y = yFrac * tileProvider.getMapHeightInTiles(zoom) * tileProvider.getTileHeight(zoom);
        return new Point2D.Double(x, y);
    } catch (Exception e) {
        // $NON-NLS-1$
        throw new IllegalGeoPositionException("Invalid zoom level", e);
    }
}
Also used : IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)

Example 2 with IllegalGeoPositionException

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

the class ExtendedMapKit method saveState.

/**
 * @see IPersistable#saveState(IMemento)
 */
@Override
public void saveState(IMemento memento) {
    GeoPosition min = getMainMap().convertPointToGeoPosition(new Point((int) Math.round(getMainMap().getWidth() * 0.1), (int) Math.round(getMainMap().getHeight() * 0.1)));
    GeoPosition max = getMainMap().convertPointToGeoPosition(new Point((int) Math.round(getMainMap().getWidth() * 0.9), (int) Math.round(getMainMap().getHeight() * 0.9)));
    // GeoPosition min = getMainMap().convertPointToGeoPosition(new Point(0,
    // 0));
    // GeoPosition max = getMainMap().convertPointToGeoPosition(new
    // Point(getMainMap().getWidth(), getMainMap().getHeight()));
    int epsg = min.getEpsgCode();
    try {
        if (epsg != max.getEpsgCode())
            max = GeotoolsConverter.getInstance().convert(max, epsg);
        memento.putFloat(MEMENTO_KEY_MIN_X, (float) min.getX());
        memento.putFloat(MEMENTO_KEY_MIN_Y, (float) min.getY());
        memento.putFloat(MEMENTO_KEY_MAX_X, (float) max.getX());
        memento.putFloat(MEMENTO_KEY_MAX_Y, (float) max.getY());
        memento.putInteger(MEMENTO_KEY_EPSG, epsg);
    } catch (IllegalGeoPositionException e) {
        // $NON-NLS-1$
        log.error("Error saving map state: Could not convert GeoPosition", e);
    }
}
Also used : GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) Point(java.awt.Point) Point(java.awt.Point)

Example 3 with IllegalGeoPositionException

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

the class AreaCalc method checkEPSG.

/**
 * Checks if GeoPosition are in a metric system and if not convert them if
 * necessary.
 *
 * @param pos List of {@link GeoPosition}
 *
 * @return List of {@link GeoPosition}
 */
public List<GeoPosition> checkEPSG(List<GeoPosition> pos) {
    // list is empty
    if (pos.size() == 0) {
        return pos;
    }
    // 
    int epsg = pos.get(0).getEpsgCode();
    // Worldmercator
    int FALLBACK_EPSG = 3395;
    // WGS84
    FALLBACK_EPSG = 4326;
    try {
        CoordinateSystem cs = CRS.decode("EPSG:" + epsg).getCoordinateSystem();
        for (int i = 0; epsg != FALLBACK_EPSG && i < cs.getDimension(); i++) {
            CoordinateSystemAxis axis = cs.getAxis(i);
            try {
                Unit<Length> unit = axis.getUnit().asType(Length.class);
                if (!unit.toString().equals("m")) {
                    // $NON-NLS-1$
                    // not metric
                    epsg = FALLBACK_EPSG;
                }
            } catch (ClassCastException e) {
                // no length unit
                epsg = FALLBACK_EPSG;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // convert all coordinates
    try {
        GeotoolsConverter g = (GeotoolsConverter) GeotoolsConverter.getInstance();
        pos = g.convertAll(pos, epsg);
    } catch (IllegalGeoPositionException e1) {
        e1.printStackTrace();
    }
    return pos;
}
Also used : GeotoolsConverter(org.jdesktop.swingx.mapviewer.GeotoolsConverter) Length(javax.measure.quantity.Length) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) CoordinateSystemAxis(org.opengis.referencing.cs.CoordinateSystemAxis) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)

Example 4 with IllegalGeoPositionException

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

the class CustomWaypointPainter method findWaypoints.

/**
 * Find the way-points in a given polygon
 *
 * @param poly the polygon
 * @return the way-points in the polygon area
 */
public Set<W> findWaypoints(final Polygon poly) {
    Rectangle viewPort = getMapKit().getMainMap().getViewportBounds();
    final int zoom = getMapKit().getMainMap().getZoom();
    final PixelConverter converter = getMapKit().getMainMap().getTileFactory().getTileProvider().getConverter();
    de.fhg.igd.geom.Point2D[] points = new de.fhg.igd.geom.Point2D[poly.npoints];
    // create a metamodel polygon
    for (int i = 0; i < poly.npoints; i++) {
        int worldX = viewPort.x + poly.xpoints[i];
        int worldY = viewPort.y + poly.ypoints[i];
        // convert to geo position
        GeoPosition pos = converter.pixelToGeo(new Point(worldX, worldY), zoom);
        // convert to common CRS
        try {
            pos = GeotoolsConverter.getInstance().convert(pos, SelectableWaypoint.COMMON_EPSG);
        } catch (IllegalGeoPositionException e) {
            // $NON-NLS-1$
            log.warn("Error converting polygon point for query");
            return new HashSet<W>();
        }
        points[i] = new de.fhg.igd.geom.Point2D(pos.getX(), pos.getY());
    }
    final de.fhg.igd.geom.shape.Polygon verifyPolygon = new de.fhg.igd.geom.shape.Polygon(points);
    // we need a 3D search bounding box for the R-Tree
    BoundingBox searchBox = verifyPolygon.getBoundingBox();
    searchBox.setMinZ(-2.0);
    searchBox.setMaxZ(2.0);
    poly.translate(viewPort.x, viewPort.y);
    try {
        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();
                    poly.translate(-dx, -dy);
                    try {
                        Area area = wp.getMarker().getArea(zoom);
                        if (area != null) {
                            return area.containedIn(poly);
                        } else {
                            // not be selected
                            return false;
                        }
                    } finally {
                        poly.translate(dx, dy);
                    }
                } catch (IllegalGeoPositionException e) {
                    log.warn("Could not convert waypoint position to pixel", e);
                    // fall back to simple method
                    return verifyPolygon.contains(wp.getBoundingBox().toExtent());
                }
            }
        });
        if (wps == null) {
            return new HashSet<W>();
        } else {
            return wps;
        }
    } finally {
        poly.translate(-viewPort.x, -viewPort.y);
    }
}
Also used : Rectangle(java.awt.Rectangle) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) 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) Polygon(java.awt.Polygon) HashSet(java.util.HashSet)

Example 5 with IllegalGeoPositionException

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

the class CustomWaypointPainter method repaintTile.

/**
 * @see AbstractTileOverlayPainter#repaintTile(int, int, int, int,
 *      PixelConverter, int)
 */
@Override
public BufferedImage repaintTile(int posX, int posY, int width, int height, PixelConverter converter, int zoom) {
    if (renderer == null) {
        return null;
    }
    int overlap = getMaxOverlap();
    // overlap pixel coordinates
    Point topLeftPixel = new Point(Math.max(posX - overlap, 0), Math.max(posY - overlap, 0));
    // TODO
    Point bottomRightPixel = new Point(posX + width + overlap, posY + height + overlap);
    // check
    // against
    // map
    // size
    // overlap geo positions
    GeoPosition topLeft = converter.pixelToGeo(topLeftPixel, zoom);
    GeoPosition bottomRight = converter.pixelToGeo(bottomRightPixel, zoom);
    // overlap geo positions in RTree CRS
    try {
        BoundingBox tileBounds = createSearchBB(topLeft, bottomRight);
        synchronized (waypoints) {
            Set<W> candidates = waypoints.query(tileBounds, matchTileVerifier);
            if (candidates != null) {
                // sort way-points
                List<W> sorted = new ArrayList<W>(candidates);
                Collections.sort(sorted, paintFirstComparator);
                BufferedImage image = createImage(width, height);
                Graphics2D gfx = image.createGraphics();
                configureGraphics(gfx);
                try {
                    // for each way-point within these bounds
                    for (W w : sorted) {
                        processWaypoint(w, posX, posY, width, height, converter, zoom, gfx);
                    }
                /*
						 * DEBUG String test = getClass().getSimpleName() +
						 * " - x=" + posX + ", y=" + posY + ": " +
						 * candidates.size() + " WPs"; gfx.setColor(Color.BLUE);
						 * gfx.drawString(test, 4, height - 4);
						 * 
						 * gfx.drawString("minX: " + tileBounds.getMinX(), 4,
						 * height - 84); gfx.drawString("maxX: " +
						 * tileBounds.getMaxX(), 4, height - 64);
						 * gfx.drawString("minY: " + tileBounds.getMinY(), 4,
						 * height - 44); gfx.drawString("maxY: " +
						 * tileBounds.getMaxY(), 4, height - 24);
						 * 
						 * gfx.drawRect(0, 0, width - 1, height - 1);
						 */
                } finally {
                    gfx.dispose();
                }
                return image;
            } else {
                return null;
            }
        }
    } catch (IllegalGeoPositionException e) {
        // $NON-NLS-1$
        log.warn("Error painting waypoint tile: " + e.getMessage());
        return null;
    }
}
Also used : BoundingBox(de.fhg.igd.geom.BoundingBox) ArrayList(java.util.ArrayList) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

IllegalGeoPositionException (org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)13 GeoPosition (org.jdesktop.swingx.mapviewer.GeoPosition)8 Point (java.awt.Point)7 BoundingBox (de.fhg.igd.geom.BoundingBox)6 Point2D (java.awt.geom.Point2D)6 Rectangle (java.awt.Rectangle)4 Area (de.fhg.igd.mapviewer.marker.area.Area)3 HashSet (java.util.HashSet)3 PixelConverter (org.jdesktop.swingx.mapviewer.PixelConverter)3 ArrayList (java.util.ArrayList)2 SelectableWaypoint (de.fhg.igd.mapviewer.waypoints.SelectableWaypoint)1 Dimension (java.awt.Dimension)1 Graphics2D (java.awt.Graphics2D)1 Polygon (java.awt.Polygon)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 Length (javax.measure.quantity.Length)1 GeotoolsConverter (org.jdesktop.swingx.mapviewer.GeotoolsConverter)1 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)1 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)1