use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class AbstractInstancePainter method createWaypoint.
/**
* Create a way-point for an instance
*
* @param instance the instance
* @param instanceService the instance service
* @return the created way-point or <code>null</code> if
*/
protected InstanceWaypoint createWaypoint(Instance instance, InstanceService instanceService) {
// retrieve instance reference
// ,
InstanceReference ref = instanceService.getReference(instance);
// getDataSet());
BoundingBox bb = null;
List<GeometryProperty<?>> geometries = new ArrayList<GeometryProperty<?>>(DefaultGeometryUtil.getDefaultGeometries(instance));
ListIterator<GeometryProperty<?>> it = geometries.listIterator();
while (it.hasNext()) {
GeometryProperty<?> prop = it.next();
// check if geometry is valid for display in map
CoordinateReferenceSystem crs = (prop.getCRSDefinition() == null) ? (null) : (prop.getCRSDefinition().getCRS());
if (crs == null) {
// no CRS, can't display in map
// remove from list
it.remove();
} else {
Geometry geometry = prop.getGeometry();
// determine geometry bounding box
BoundingBox geometryBB = BoundingBox.compute(geometry);
if (geometryBB == null) {
// no valid bounding box for geometry
it.remove();
} else {
try {
// get converter to way-point CRS
CRSConverter conv = CRSConverter.getConverter(crs, getWaypointCRS());
// convert BB to way-point SRS
geometryBB = conv.convert(geometryBB);
// add to instance bounding box
if (bb == null) {
bb = new BoundingBox(geometryBB);
} else {
bb.add(geometryBB);
}
} catch (Exception e) {
log.error("Error converting instance bounding box to waypoint bounding box", e);
// ignore geometry
it.remove();
}
}
}
}
if (bb == null || geometries.isEmpty()) {
// don't create way-point w/o geometries
return null;
}
// use bounding box center as GEO position
Point3D center = bb.getCenter();
GeoPosition pos = new GeoPosition(center.getX(), center.getY(), GenericWaypoint.COMMON_EPSG);
// buffer bounding box if x or y dimension empty
if (bb.getMinX() == bb.getMaxX()) {
bb.setMinX(bb.getMinX() - BUFFER_VALUE);
bb.setMaxX(bb.getMaxX() + BUFFER_VALUE);
}
if (bb.getMinY() == bb.getMaxY()) {
bb.setMinY(bb.getMinY() - BUFFER_VALUE);
bb.setMaxY(bb.getMaxY() + BUFFER_VALUE);
}
// set dummy z range (otherwise the RTree can't deal correctly with it)
bb.setMinZ(-BUFFER_VALUE);
bb.setMaxZ(BUFFER_VALUE);
String name = findInstanceName(instance);
// create the way-point
// XXX in abstract method?
InstanceWaypoint wp = new InstanceWaypoint(pos, bb, ref, geometries, instance.getDefinition(), name);
// each way-point must have its own marker, as the marker stores the
// marker areas
wp.setMarker(createMarker(wp));
return wp;
}
use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class InstanceMarker method paintLine.
/**
* Paint a line string geometry.
*
* @param geometry the line string
* @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 paintLine(LineString geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
Coordinate[] coordinates = geometry.getCoordinates();
if (coordinates.length <= 0) {
return null;
}
if (coordinates.length == 1) {
// fall back to point drawing
Point point = getGeometryFactory().createPoint(coordinates[0]);
return paintPoint(point, g, crsDefinition, context, converter, zoom, mapCRS, calculateArea);
}
try {
// get CRS converter
CRSConverter conv = CRSConverter.getConverter(crsDefinition.getCRS(), mapCRS);
List<Point2D> mapPoints = new ArrayList<Point2D>(coordinates.length);
for (Coordinate coord : coordinates) {
// manually convert to map CRS
Point3D mapPoint = conv.convert(coord.x, coord.y, 0);
GeoPosition pos = new GeoPosition(mapPoint.getX(), mapPoint.getY(), converter.getMapEpsg());
Point2D point = converter.geoToPixel(pos, zoom);
mapPoints.add(point);
}
if (applyStroke(g, context)) {
for (int i = 0; i < mapPoints.size() - 1; i++) {
// draw each connecting line
Point2D p1 = mapPoints.get(i);
Point2D p2 = mapPoints.get(i + 1);
g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
}
} else {
log.warn("Stroke disabled in style, LineString is not rendered");
}
if (!calculateArea) {
return null;
}
// use a buffer around the line as area
// XXX
java.awt.Polygon[] buffer = createBufferPolygon(mapPoints, 3);
// ok?
if (buffer.length == 0) {
return null;
} else if (buffer.length == 1) {
return new PolygonArea(buffer[0]);
} else {
Collection<Area> areas = new ArrayList<Area>();
for (java.awt.Polygon bufferPoly : buffer) {
areas.add(new PolygonArea(bufferPoly));
}
return new MultiArea(areas);
}
} catch (Exception e) {
log.error("Error painting instance polygon geometry", e);
return null;
}
}
use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class InstanceMarker method paintPoint.
/**
* Paint a point geometry.
*
* @param geometry the point
* @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 point marker area or <code>null</code> if painting failed
*/
protected Area paintPoint(Point geometry, Graphics2D g, CRSDefinition crsDefinition, InstanceWaypoint context, PixelConverter converter, int zoom, CoordinateReferenceSystem mapCRS, boolean calculateArea) {
try {
/*
* Conversion to map pixel coordinates: Though most of the time the
* result will be the origin (0,0), e.g. for way-points representing
* a single point, the coordinates may also be different, e.g. for
* MultiPoint way-points.
*/
// 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);
int x = (int) point.getX();
int y = (int) point.getY();
// fall-back: circle
if (applyFill(g, context)) {
g.fillOval(x - defaultPointSize / 2, y - defaultPointSize / 2, defaultPointSize, defaultPointSize);
}
if (applyStroke(g, context)) {
// TODO respect stroke width?
g.drawOval(x - defaultPointSize / 2 - 1, y - defaultPointSize / 2 - 1, defaultPointSize + 1, defaultPointSize + 1);
}
if (calculateArea) {
return new PolygonArea(new java.awt.Polygon(new int[] { x - defaultPointSize / 2 - 1, x + defaultPointSize / 2 + 1, x + defaultPointSize / 2 + 1, x - defaultPointSize / 2 - 1 }, new int[] { y - defaultPointSize / 2 - 1, y - defaultPointSize / 2 - 1, y + defaultPointSize / 2 + 1, y + defaultPointSize / 2 + 1 }, 4));
} else {
return null;
}
} catch (Exception e) {
log.error("Error painting instance point geometry", e);
return null;
}
}
use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class InstanceMarker method createPolygon.
private java.awt.Polygon createPolygon(Coordinate[] coordinates, CRSConverter geoConverter, PixelConverter pixelConverter, int zoom) throws TransformException, IllegalGeoPositionException {
java.awt.Polygon result = new java.awt.Polygon();
for (Coordinate coord : coordinates) {
// manually convert to map CRS
Point3D mapPoint = geoConverter.convert(coord.x, coord.y, 0);
GeoPosition pos = new GeoPosition(mapPoint.getX(), mapPoint.getY(), pixelConverter.getMapEpsg());
Point2D point = pixelConverter.geoToPixel(pos, zoom);
result.addPoint((int) point.getX(), (int) point.getY());
}
return result;
}
use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class GeoUtil method getPositionForAddress.
/**
* Convert a street address into a position. Uses the Yahoo GeoCoder. You
* must supply your own yahoo id.
*
* @param street Street
* @param city City
* @param state State (must be a US state)
* @throws java.io.IOException if the request fails.
* @return the position of this street address
*/
public static GeoPosition getPositionForAddress(String street, String city, String state) throws IOException {
try {
URL load = new URL("http://api.local.yahoo.com/MapsService/V1/geocode?" + "appid=joshy688" + "&street=" + street.replace(' ', '+') + "&city=" + city.replace(' ', '+') + "&state=" + state.replace(' ', '+'));
// System.out.println("using address: " + load);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(load.openConnection().getInputStream());
XPath xpath = XPathFactory.newInstance().newXPath();
// NodeList str =
// (NodeList)xpath.evaluate("//Result",doc,XPathConstants.NODESET);
Double lat = (Double) xpath.evaluate("//Result/Latitude/text()", doc, XPathConstants.NUMBER);
Double lon = (Double) xpath.evaluate("//Result/Longitude/text()", doc, XPathConstants.NUMBER);
// System.out.println("got address at: " + lat + " " + lon);
return new GeoPosition(lon, lat, GeoPosition.WGS_84_EPSG);
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException("Failed to retrieve location information from the internet: " + e.toString());
}
}
Aggregations