use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class GeoUtil method getPosition.
/**
* convert an on screen pixel coordinate and a zoom level to a geo position
*
* @param pixelCoordinate the pixel coordinate
* @param zoom the zoom level
* @param info the tile factory info
*
* @return the corresponding geo position in WGS84
*/
public static GeoPosition getPosition(Point2D pixelCoordinate, int zoom, TileFactoryInfo info) {
// p(" --bitmap to latlon : " + coord + " " + zoom);
double wx = pixelCoordinate.getX();
double wy = pixelCoordinate.getY();
// this reverses getBitmapCoordinates
double flon = (wx - info.getMapCenterInPixelsAtZoom(zoom).getX()) / info.getLongitudeDegreeWidthInPixels(zoom);
double e1 = (wy - info.getMapCenterInPixelsAtZoom(zoom).getY()) / (-1 * info.getLongitudeRadianWidthInPixels(zoom));
double e2 = (2 * Math.atan(Math.exp(e1)) - Math.PI / 2) / (Math.PI / 180.0);
double flat = e2;
GeoPosition wc = new GeoPosition(flon, flat, GeoPosition.WGS_84_EPSG);
return wc;
}
use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class WMSUtil method getBoundingBox.
/**
* Get the preferred bounding box. Tries to convert the bounding box if not
* found with the preferred SRS
*
* @param capabilities the WMS capabilities
* @param preferredEpsg the preferred EPSG code
* @return the preferred bounding box, an available bounding box or
* <code>null</code> if none is available
*/
public static WMSBounds getBoundingBox(WMSCapabilities capabilities, int preferredEpsg) {
WMSBounds bounds = getPreferredBoundingBox(capabilities, preferredEpsg);
String srs = "EPSG:" + preferredEpsg;
if (bounds.getSRS().equals(srs)) {
// matches preferred SRS
return bounds;
} else {
try {
// try to convert the bounding box to the preferred SRS
int boxEpsg = Integer.parseInt(bounds.getSRS().substring(5));
GeoPosition topLeft = new GeoPosition(bounds.getMinX(), bounds.getMinY(), boxEpsg);
GeoPosition bottomRight = new GeoPosition(bounds.getMaxX(), bounds.getMaxY(), boxEpsg);
topLeft = GeotoolsConverter.getInstance().convert(topLeft, preferredEpsg);
bottomRight = GeotoolsConverter.getInstance().convert(bottomRight, preferredEpsg);
return new WMSBounds(srs, Math.min(topLeft.getX(), bottomRight.getX()), Math.min(topLeft.getY(), bottomRight.getY()), Math.max(topLeft.getX(), bottomRight.getX()), Math.max(topLeft.getY(), bottomRight.getY()));
} catch (Exception e) {
// fall back to bounds
return bounds;
}
}
}
use of org.jdesktop.swingx.mapviewer.GeoPosition 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 org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class BasicMapKit method generateBoundingRect.
private Rectangle2D generateBoundingRect(double minX, double minY, double maxX, double maxY, int epsg, int zoom) throws IllegalGeoPositionException {
java.awt.geom.Point2D p1 = getMainMap().getTileFactory().getTileProvider().getConverter().geoToPixel(new GeoPosition(minX, minY, epsg), zoom);
java.awt.geom.Point2D p2 = getMainMap().getTileFactory().getTileProvider().getConverter().geoToPixel(new GeoPosition(maxX, maxY, epsg), zoom);
return new Rectangle2D.Double((p1.getX() < p2.getX()) ? (p1.getX()) : (p2.getX()), (p1.getY() < p2.getY()) ? (p1.getY()) : (p2.getY()), Math.abs(p2.getX() - p1.getX()), Math.abs(p2.getY() - p1.getY()));
}
use of org.jdesktop.swingx.mapviewer.GeoPosition in project hale by halestudio.
the class ExtendedMapKit method restoreState.
/**
* @see IPersistableEditor#restoreState(IMemento)
*/
@Override
public void restoreState(IMemento memento) {
if (memento == null)
return;
Integer oldEpsg = memento.getInteger(MEMENTO_KEY_EPSG);
if (oldEpsg != null) {
GeoPosition p1 = new GeoPosition(memento.getFloat(MEMENTO_KEY_MIN_X), memento.getFloat(MEMENTO_KEY_MIN_Y), oldEpsg);
GeoPosition p2 = new GeoPosition(memento.getFloat(MEMENTO_KEY_MAX_X), memento.getFloat(MEMENTO_KEY_MAX_Y), oldEpsg);
Set<GeoPosition> positions = new HashSet<GeoPosition>();
positions.add(p1);
positions.add(p2);
zoomToPositions(positions);
}
}
Aggregations