use of org.jdesktop.swingx.mapviewer.PixelConverter in project hale by halestudio.
the class CustomWaypointPainter method findWaypoints.
/**
* Find the way-points in a given rectangular area
*
* @param rect the area
* @return the way-points in the area
*/
public Set<W> findWaypoints(Rectangle rect) {
Rectangle viewPort = getMapKit().getMainMap().getViewportBounds();
final Rectangle worldRect = new Rectangle(viewPort.x + rect.x, viewPort.y + rect.y, rect.width, rect.height);
final int zoom = getMapKit().getMainMap().getZoom();
final PixelConverter converter = getMapKit().getMainMap().getTileFactory().getTileProvider().getConverter();
final GeoPosition topLeft = converter.pixelToGeo(new Point(worldRect.x, worldRect.y), zoom);
final GeoPosition bottomRight = converter.pixelToGeo((new Point(worldRect.x + worldRect.width, worldRect.y + worldRect.height)), zoom);
return findWaypoints(topLeft, bottomRight, worldRect, converter, zoom);
}
use of org.jdesktop.swingx.mapviewer.PixelConverter 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.PixelConverter in project hale by halestudio.
the class CustomWaypointPainter method processWaypoint.
private void processWaypoint(W w, int minX, int minY, int width, int height, PixelConverter converter, int zoom, Graphics2D g) {
try {
Point2D point = converter.geoToPixel(w.getPosition(), zoom);
int x = (int) (point.getX() - minX);
int y = (int) (point.getY() - minY);
PixelConverter converterWrapper = new TranslationPixelConverterDecorator(converter, (int) point.getX(), (int) point.getY());
g.translate(x, y);
Rectangle gBounds = new Rectangle(minX - (int) point.getX(), minY - (int) point.getY(), width, height);
renderer.paintWaypoint(g, converterWrapper, zoom, w, gBounds);
g.translate(-x, -y);
} catch (IllegalGeoPositionException e) {
// waypoint not in map bounds or position invalid
// log.warn("Error painting waypoint", e);
}
}
use of org.jdesktop.swingx.mapviewer.PixelConverter 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;
}
}
use of org.jdesktop.swingx.mapviewer.PixelConverter in project hale by halestudio.
the class HoverMapTip method showTip.
private void showTip(final MouseEvent e, final String text) {
synchronized (HoverMapTip.this) {
PixelConverter converter = getMap().getTileFactory().getTileProvider().getConverter();
int zoom = getMap().getZoom();
Rectangle viewPort = getMap().getViewportBounds();
GeoPosition pos = converter.pixelToGeo(new Point(viewPort.x + e.getX(), viewPort.y + e.getY()), zoom);
setTipText(text, pos);
if (closeTimer != null) {
closeTimer.cancel(true);
}
closeTimer = scheduleService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
hideTip();
}
}, 5 * HOVER_DELAY, 1000, TimeUnit.MILLISECONDS);
}
}
Aggregations