use of de.fhg.igd.mapviewer.marker.area.Area 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.mapviewer.marker.area.Area in project hale by halestudio.
the class StyledInstanceMarker method getArea.
/**
* Returns the area of the drawn point.
*
* @param point the point
* @param style the Style2D object
* @param shape the Light Shape
* @return the area, which should equal the space of the drawn object
*/
private Area getArea(Point2D point, Style2D style, LiteShape2 shape) {
// if it is a mark style
if (style instanceof MarkStyle2D) {
PathIterator citer = getPathIterator(shape);
float[] coords = new float[2];
MarkStyle2D ms2d = (MarkStyle2D) style;
Shape transformedShape;
while (!(citer.isDone())) {
citer.currentSegment(coords);
transformedShape = ms2d.getTransformedShape(coords[0], coords[1]);
if (transformedShape != null) {
java.awt.geom.Area areatemp = new java.awt.geom.Area(transformedShape);
Rectangle rec = areatemp.getBounds();
AdvancedBoxArea area = new AdvancedBoxArea(areatemp, rec.x, rec.y, rec.x + rec.width, rec.y + rec.height);
return area;
}
}
} else // if it is an external graphic style
if (style instanceof GraphicStyle2D) {
GraphicStyle2D gs2d = (GraphicStyle2D) style;
int minX = (int) point.getX() - gs2d.getImage().getWidth() / 2;
int minY = (int) point.getY() - gs2d.getImage().getHeight() / 2;
int maxX = (int) point.getX() + gs2d.getImage().getWidth() / 2;
int maxY = (int) point.getX() + gs2d.getImage().getHeight() / 2;
BoxArea area = new BoxArea(minX, minY, maxX, maxY);
return area;
}
return null;
}
use of de.fhg.igd.mapviewer.marker.area.Area in project hale by halestudio.
the class StyledInstanceMarker method paintPoint.
/**
* @see InstanceMarker#paintPoint(Point, Graphics2D, CRSDefinition,
* InstanceWaypoint, PixelConverter, int, CoordinateReferenceSystem,
* boolean)
*/
@Override
protected Area paintPoint(Point geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
initStyle(context);
Area area = null;
try {
if (pointSymbolizer == null || (SLD.mark(pointSymbolizer) == null && pointSymbolizer.getGraphic().graphicalSymbols().isEmpty())) {
// generic
return super.paintFallback(g, context, converter, zoom, null, calculateArea);
}
// get CRS converter
CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
// manually convert to map CRS
Point3D mapPoint = conv.convert(geometry.getX(), geometry.getY(), 0);
GeoPosition pos = new GeoPosition(mapPoint.getX(), mapPoint.getY(), converter.getMapEpsg());
// determine pixel coordinates
Point2D point = converter.geoToPixel(pos, zoom);
Coordinate coordinate = new Coordinate(point.getX(), point.getY());
Point newPoint = geometry.getFactory().createPoint(coordinate);
// create a LiteShape and instantiate the Painter and the
// StyleFactory
LiteShape2 lites = new LiteShape2(newPoint, null, null, false);
StyledShapePainter ssp = new StyledShapePainter();
SLDStyleFactory styleFactory = new SLDStyleFactory();
Range<Double> range = new Range<Double>(Double.class, 0.5, 1.5);
PointSymbolizer pointS;
// is the Waypoint selected?
if (context.isSelected()) {
// switch to the SelectionSymbolizer
pointS = getSelectionSymbolizer(pointSymbolizer);
} else
// use the specific PointSymbolizer
pointS = pointSymbolizer;
// Create the Style2D object for painting with the use of a
// DummyFeature wich extends SimpleFeatures
// because Geotools can only work with that
DummyFeature dummy = new DummyFeature();
Style2D style2d = styleFactory.createStyle(dummy, pointS, range);
// create the area object of the painted image for further use
area = getArea(point, style2d, lites);
// actually paint
ssp.paint(g, lites, style2d, 1);
// graphic)
if (context.isSelected() && style2d instanceof GraphicStyle2D) {
GraphicStyle2D gs2d = (GraphicStyle2D) style2d;
// get minX and minY for the drawn rectangle arround the image
int minX = (int) point.getX() - gs2d.getImage().getWidth() / 2;
int minY = (int) point.getY() - gs2d.getImage().getHeight() / 2;
// apply the specification of the selection rectangle
applyFill(g, context);
applyStroke(g, context);
// draw the selection rectangle
g.drawRect(minX - 1, minY - 1, gs2d.getImage().getWidth() + 1, gs2d.getImage().getHeight() + 1);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return area;
}
use of de.fhg.igd.mapviewer.marker.area.Area in project hale by halestudio.
the class InstanceMarker method paintGeometry.
/**
* Paint a geometry.
*
* @param g the graphics to paint on
* @param crsDefinition the CRS definition associated with the geometry
* @param geometry the geometry
* @param context the context
* @param converter the pixel converter
* @param zoom the zoom level
* @param singleGeometry if this is the only geometry associated to the
* marker
* @param gBounds the graphics bounds
* @param mapCRS the map coordinate reference system
* @param calculateArea if the area representing the marker should be
* calculated, if <code>false</code> is given here the return
* value is ignored and should be <code>null</code>
* @return the area the geometry occupies (in pixel coordinates), or
* <code>null</code> if nothing has been painted
*/
protected Area paintGeometry(Graphics2D g, CRSDefinition crsDefinition, Geometry geometry, InstanceWaypoint context, PixelConverter converter, int zoom, boolean singleGeometry, Rectangle gBounds, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
if (geometry instanceof GeometryCollection) {
// paint each geometry in a geometry collection
List<Area> areas = (calculateArea) ? (new ArrayList<Area>()) : (null);
GeometryCollection collection = (GeometryCollection) geometry;
for (int i = 0; i < collection.getNumGeometries(); i++) {
Geometry geom = collection.getGeometryN(i);
Area geomArea = paintGeometry(g, crsDefinition, geom, context, converter, zoom, singleGeometry && collection.getNumGeometries() == 1, gBounds, mapCRS, calculateArea);
if (areas != null && geomArea != null) {
areas.add(geomArea);
}
}
if (areas == null || areas.isEmpty()) {
return null;
} else {
return new MultiArea(areas);
}
}
// if it is the only geometry the check that was already made is OK
if (!calculateArea && !singleGeometry) {
// we can safely return null inside this method, as no area has to
// be calculated
// determine bounding box
BoundingBox geometryBB;
synchronized (geometryMapBBs) {
// retrieve cached bounding box
geometryBB = geometryMapBBs.get(geometry);
if (geometryBB == null) {
// if none available, try to calculate BB
BoundingBox calcBB = BoundingBox.compute(geometry);
if (calcBB != null && calcBB.checkIntegrity()) {
try {
// get CRS converter
CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
// manually convert to map CRS
geometryBB = conv.convert(calcBB);
// put BB in cache
geometryMapBBs.put(geometry, geometryBB);
} catch (Throwable e) {
log.error("Error checking geometry bounding box", e);
return null;
}
}
}
}
if (geometryBB != null) {
try {
GeoPosition minCorner = new GeoPosition(geometryBB.getMinX(), geometryBB.getMinY(), converter.getMapEpsg());
GeoPosition maxCorner = new GeoPosition(geometryBB.getMaxX(), geometryBB.getMaxY(), converter.getMapEpsg());
// determine pixel coordinates
Point2D minPixels = converter.geoToPixel(minCorner, zoom);
Point2D maxPixels = converter.geoToPixel(maxCorner, zoom);
// geometry pixel bounding box
int minX = Math.min((int) minPixels.getX(), (int) maxPixels.getX());
int minY = Math.min((int) minPixels.getY(), (int) maxPixels.getY());
int maxX = Math.max((int) minPixels.getX(), (int) maxPixels.getX());
int maxY = Math.max((int) minPixels.getY(), (int) maxPixels.getY());
// add overlap
minX -= GEOMETRY_PIXEL_BB_OVERLAP;
minY -= GEOMETRY_PIXEL_BB_OVERLAP;
maxX += GEOMETRY_PIXEL_BB_OVERLAP;
maxY += GEOMETRY_PIXEL_BB_OVERLAP;
// create bounding box
Rectangle geometryPixelBB = new Rectangle(minX, minY, maxX - minX, maxY - minY);
if (!gBounds.intersects(geometryPixelBB) && !gBounds.contains(geometryPixelBB)) {
// geometry does not lie in tile
return null;
}
} catch (Throwable e) {
log.error("Error checking geometry bounding box", e);
return null;
}
} else {
// empty or invalid bounding box
return null;
}
}
if (geometry instanceof Point) {
return paintPoint((Point) geometry, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
}
if (geometry instanceof Polygon) {
return paintPolygon((Polygon) geometry, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
}
if (geometry instanceof LineString) {
return paintLine((LineString) geometry, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
}
return null;
}
use of de.fhg.igd.mapviewer.marker.area.Area in project hale by halestudio.
the class InstanceMarker method paintPolygon.
/**
* Paint a polygon geometry.
*
* @param geometry the polygon
* @param g the graphics object to paint on
* @param crsDefinition the CRS definition associated to the geometry
* @param context the context
* @param converter the pixel converter
* @param zoom the zoom level
* @param mapCRS the map coordinate reference system
* @param calculateArea if the area representing the marker should be
* calculated, if <code>false</code> is given here the return
* value is ignored and should be <code>null</code>
* @return the polygon area or <code>null</code> if painting failed
*/
protected Area paintPolygon(Polygon geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
try {
// get CRS converter
CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
// exterior
Coordinate[] coordinates = geometry.getExteriorRing().getCoordinates();
java.awt.Polygon outerPolygon = createPolygon(coordinates, conv, converter, zoom);
if (geometry.getNumInteriorRing() > 0) {
// polygon has interior geometries
java.awt.geom.Area drawArea = new java.awt.geom.Area(outerPolygon);
// interior
for (int i = 0; i < geometry.getNumInteriorRing(); i++) {
LineString interior = geometry.getInteriorRingN(i);
java.awt.Polygon innerPolygon = createPolygon(interior.getCoordinates(), conv, converter, zoom);
drawArea.subtract(new java.awt.geom.Area(innerPolygon));
}
if (applyFill(g, context)) {
g.fill(drawArea);
}
if (applyStroke(g, context)) {
g.draw(drawArea);
}
if (calculateArea) {
return new AdvancedPolygonArea(drawArea, outerPolygon);
}
} else {
// visible)
if (applyFill(g, context)) {
g.fill(outerPolygon);
}
if (applyStroke(g, context)) {
g.draw(outerPolygon);
}
if (calculateArea) {
return new PolygonArea(outerPolygon);
}
}
// no calculateArea set
return null;
} catch (Exception e) {
log.error("Error painting instance polygon geometry", e);
return null;
}
}
Aggregations