use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.
the class WMSTileOverlay 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) {
// the first converter isn't regarded as a new converter because it's
// always the empty map
boolean isNewConverter = lastConverter != null && !converter.equals(lastConverter);
lastConverter = converter;
if (!converter.supportsBoundingBoxes()) {
if (isNewConverter) {
handleError(Messages.WMSTileOverlay_0 + configuration.getName() + Messages.WMSTileOverlay_1);
}
return null;
}
synchronized (this) {
if (capabilities == null) {
try {
capabilities = WMSUtil.getCapabilities(configuration.getBaseUrl());
} catch (WMSCapabilitiesException e) {
// $NON-NLS-1$
log.error("Error getting WMS capabilities");
}
}
}
if (capabilities != null) {
int mapEpsg = converter.getMapEpsg();
WMSBounds box;
synchronized (this) {
if (capabilities.getSupportedSRS().contains("EPSG:" + mapEpsg)) {
// $NON-NLS-1$
// same SRS supported
} else {
// SRS not supported
if (isNewConverter) {
StringBuilder message = new StringBuilder();
message.append(Messages.WMSTileOverlay_2);
message.append(configuration.getName());
message.append(Messages.WMSTileOverlay_3);
boolean init = true;
for (String srs : capabilities.getSupportedSRS()) {
if (init) {
init = false;
} else {
// $NON-NLS-1$
message.append(", ");
}
message.append(srs);
}
handleError(message.toString());
}
return null;
}
box = WMSUtil.getBoundingBox(capabilities, mapEpsg);
}
String srs = box.getSRS();
if (srs.startsWith("EPSG:")) {
// $NON-NLS-1$
// determine format
String format = null;
Iterator<String> itFormat = supportedFormats.iterator();
synchronized (this) {
while (format == null && itFormat.hasNext()) {
String supp = itFormat.next();
if (capabilities.getFormats().contains(supp)) {
format = supp;
}
}
}
if (format == null) {
// no compatible format
return null;
}
try {
// check if tile lies within the bounding box
int epsg = Integer.parseInt(srs.substring(5));
GeoPosition topLeft = converter.pixelToGeo(new Point(posX, posY), zoom);
GeoPosition bottomRight = converter.pixelToGeo(new Point(posX + width, posY + height), zoom);
// WMS bounding box
BoundingBox wms = new BoundingBox(box.getMinX(), box.getMinY(), -1, box.getMaxX(), box.getMaxY(), 1);
GeoConverter geotools = GeotoolsConverter.getInstance();
GeoPosition bbTopLeft = geotools.convert(topLeft, epsg);
GeoPosition bbBottomRight = geotools.convert(bottomRight, epsg);
double minX = Math.min(bbTopLeft.getX(), bbBottomRight.getX());
double minY = Math.min(bbTopLeft.getY(), bbBottomRight.getY());
double maxX = Math.max(bbTopLeft.getX(), bbBottomRight.getX());
double maxY = Math.max(bbTopLeft.getY(), bbBottomRight.getY());
BoundingBox tile = new BoundingBox(minX, minY, -1, maxX, maxY, 1);
// check if bounding box and tile overlap
if (wms.intersectsOrCovers(tile) || tile.covers(wms)) {
WMSBounds bounds;
if (epsg == mapEpsg) {
bounds = new WMSBounds(srs, minX, minY, maxX, maxY);
} else {
// determine bounds for request
minX = Math.min(topLeft.getX(), bottomRight.getX());
minY = Math.min(topLeft.getY(), bottomRight.getY());
maxX = Math.max(topLeft.getX(), bottomRight.getX());
maxY = Math.max(topLeft.getY(), bottomRight.getY());
// $NON-NLS-1$
bounds = new WMSBounds("EPSG:" + mapEpsg, minX, minY, maxX, maxY);
}
URI uri;
synchronized (this) {
uri = WMSUtil.getMapURI(capabilities, configuration, width, height, bounds, null, format, true);
}
Proxy proxy = ProxyUtil.findProxy(uri);
InputStream in = uri.toURL().openConnection(proxy).getInputStream();
BufferedImage image = GraphicsUtilities.loadCompatibleImage(in);
// apply transparency to the image
BufferedImage result = GraphicsUtilities.createCompatibleTranslucentImage(image.getWidth(), image.getHeight());
Graphics2D g = result.createGraphics();
try {
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f);
g.setComposite(ac);
g.drawImage(image, 0, 0, null);
} finally {
g.dispose();
}
return result;
}
} catch (Throwable e) {
// $NON-NLS-1$
log.warn("Error painting WMS overlay", e);
}
}
}
return null;
}
use of de.fhg.igd.geom.BoundingBox 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 de.fhg.igd.geom.BoundingBox 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;
}
}
use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.
the class SelectableWaypoint method addToRefresher.
/**
* Add a way-point to the refresher
*
* @param refresher the refresher
*/
public void addToRefresher(Refresher refresher) {
if (isPoint()) {
refresher.addPosition(getPosition());
} else {
BoundingBox bb = getBoundingBox();
GeoPosition bottomRight = new GeoPosition(bb.getMaxX(), bb.getMaxY(), COMMON_EPSG);
GeoPosition topLeft = new GeoPosition(bb.getMinX(), bb.getMinY(), COMMON_EPSG);
refresher.addArea(topLeft, bottomRight);
}
}
use of de.fhg.igd.geom.BoundingBox in project hale by halestudio.
the class Node method condenseTree.
/**
* Eliminate l if it has too few entries. Propagate elimination upwards.
* Adjust BoundingBoxes.
*
* @param l the Leaf from which an entry has been deleted
*/
@SuppressWarnings("unchecked")
private void condenseTree(Node<T> l) {
// the list of deleted nodes
List<Node<T>> q = new ArrayList<Node<T>>();
Node<T> n = l;
while (n._parent != null) {
// eliminate under-full node
int minSize = calculateMinSize();
if (n._children.size() < minSize) {
n._parent._children.remove(n);
q.add(n);
} else {
// adjust BoundingBox of n (the element from which an
// entry has been deleted)
n._boundingBox = new BoundingBox();
for (Localizable c : n._children) {
n._boundingBox.add(c.getBoundingBox());
}
}
n = n._parent;
}
// adjust the bounding box of the root
n._boundingBox = new BoundingBox();
for (Localizable c : n._children) {
n._boundingBox.add(c.getBoundingBox());
}
// re-insert orphaned entries
for (Node<T> no : q) {
if (no.isLeaf()) {
// re-insert the leaf's children as usual
for (Localizable c : no.getChildren()) {
_tree.insert((T) c);
}
} else {
// insert the children of all leafs we can
// find in "no"
insertChildrenOfAllLeafs(no, _tree);
}
}
}
Aggregations