[OpenLayers-Users] openlayers can't show an image layer in the right place starting from a center point
Matteo Galletti
m.galletti at eldes.it
Fri Sep 20 01:21:39 PDT 2013
I have to draw an image over the map as an overlay. The image is, for
example, 400x400 pixels and represents an area of 400x400 kilometers. I'm
using an Image Layer to do this. I have, as reference to place the image in
the map, the lat/lon point of the center where the image should be
positioned. To draw the image layer, I have to create a Bounds object, which
needs the south-west lat/lon point and the north-east lat/lon point.
The idea is this:
starting from the center point I calculate the south-west point going
200km south from center, and then 200km west. This way I get the south-west
lat/lon point.
starting from the center point I calculate the north-east point going
200km north from center, and then 200km east. This way I get the north-east
lat/lon point.
now I have the two points to create the Bounds object needed to create
the image layer.
Everything is fine, but the problem is that the image is not rendered in the
right position. The center of the image does not correspond to the center
point I started the calculation but it's a little shifted like this:
(see fig1.png in attachment)
To calculate the south-west and north-east lat/lon coordinates I use a
library found on Internet called "Geodesy by Mike Gavaghan".
Here the code:
//lat, lon are the coordinates of the center point
//initialize geodesy
GeodeticCalculator geoCalc = new GeodeticCalculator();
Ellipsoid reference = Ellipsoid.WGS84;
GlobalCoordinates coords = new GlobalCoordinates(lat, lon);
//get south-west corner
GlobalCoordinates south =
geoCalc.calculateEndingGlobalCoordinates(reference, coords, 180,
imgMetersHeight/2);
GlobalCoordinates southwest =
geoCalc.calculateEndingGlobalCoordinates(reference, south, 270,
imgMetersWidth/2);
//get north-east corner
GlobalCoordinates north =
geoCalc.calculateEndingGlobalCoordinates(reference, coords, 0,
imgMetersHeight/2);
GlobalCoordinates northeast =
geoCalc.calculateEndingGlobalCoordinates(reference, north, 90,
imgMetersWidth/2);
//create a bound using the calculated coordinates
Bounds bounds = new Bounds(southwest.getLongitude(),
southwest.getLatitude(), northeast.getLongitude(), northeast.getLatitude());
bounds.transform(m_Proj4326, new Projection(m_Map.getProjection()));
//set options of the image
ImageOptions imgOptions = new ImageOptions();
imgOptions.setIsBaseLayer(false);
imgOptions.setTransitionEffect(TransitionEffect.RESIZE);
imgOptions.setAlwaysInRange(true);
imgOptions.setLayerOpacity(0.7);
imgOptions.setProjection("EPSG:900913");
//create the image layer and add to the map
imgLayer = new Image("my name", null, bounds, new Size(400, 400),
imgOptions);
map.addLayer(imgLayer);
My questions:
What am I doing wrong?
Are my calculations of south-west and north-east points correct?
What should I do to draw correctly on the map an image placed and
centered correctly?
Thanks.
Magallo79
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130920/f0026137/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fig1.png
Type: image/png
Size: 14082 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130920/f0026137/attachment-0001.png>
More information about the Users
mailing list