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);
}
}
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);
}
}
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;
}
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);
}
}
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;
}
}
Aggregations