[ka-Map-users] GeoCoordinates to Pixel position

Paul Spencer pspencer at dmsolutions.ca
Fri May 12 07:35:03 EDT 2006


this is tricky math :)  The correct pixel position needs to be  
calculated in a number of steps depending on how you want to use the  
pixel position.

The math for taking a geographic position and calculating its pixel  
position is handled starting with addObjectGeo ... you can trace the  
calls from there.

The simple calculation is to take the geographic coordinates and  
multiply by the cellsize (geographic coordinates per pixel).

var px = gx * this.cellSize;
var py = gy * this.cellSize; //or myKaMap.cellSize

now you have to convert from geographic space to browser space  
because in the real world up is positive and in browser space, down  
is positive

py = -1 * py;

now you have to convert to viewport coordinates, which are mirrored  
on x/y to get the image to show up in the viewport:

px = -1*px;
py = -1*py;

now you have to adjust for a variable origin

px = px - this.xOrigin;
py = py - this.yOrigin;  // or maybe it is +?

px and py are now relative to the top, left of the viewport element.   
If you want page-relative coordinates, you need to find the top/left  
of the viewport and add px/py.

I may not have this exactly right, whenever I need to do something  
like this I usually end up spending time in the debugger tracing the  
values to figure out exactly which combination I need :(

The pixel coordinates are going to be different at every zoom scale  
because the origin and cell size changes.

You can get notified of changes in the scale by registering for  



PS not sure why you want to do this.  kaMap already includes  
functions for doing most of this in the other direction.  Is there a  
need for the reverse?

On 11-May-06, at 7:52 PM, Wei Yang, Ng - Wayne wrote:

> Hi Ramesh,
>     I am doing similar stuff and I am having the same problem. (The  
> map slides to anywhere else but the point that I want)A possible  
> solution
> (not tested) is to create a javascript function that kicks in when  
> the zoom level changes. (a few events trigger a zoom level change  
> eg. mouse scroll, icon click, KB +/-)
> The function finds out(a set variable perhaps) if the map is  
> supposed to zoom to a certain geographic coordinate and reinvokes  
> the function(supplying lat/long of course) that moves/slides to it.  
> This idea is based on the observation that while the map is  
> sliding, I am able to interrupt the action by dbl clicking anywhere  
> on the map.
> Cheers,
> Wayne
>> Hi,
>> I am trying to figure out the correct pixel position
>> for a given geo-coordinates in all zoom levels.
>> I looked at the code, and it looks like the following
>> sequence
>> p = kaMap.geoToPix(geox,geoy) newxy =  
>> kaMap.currentTool.adjustPixPosition(p[0]*-1,
>> p[1]*-1)
>> seem to work fine if the zoom level is not changed but
>> only extents are changed.
>> How do I calculate the correct pixel position when
>> zoom level(or scales) change?
>> Thanks
>> Ramesh
> _______________________________________________
> ka-Map-users mailing list
> ka-Map-users at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/ka-map-users

|Paul Spencer                           pspencer at dmsolutions.ca   |
|Applications & Software Development                              |
|DM Solutions Group Inc                 http://www.dmsolutions.ca/|

More information about the ka-Map-users mailing list