[Jprogramming] Useful geographic algorithm

Simon Ampleman sa at infodev.ca
Fri Mar 24 05:52:28 HKT 2006


Hello,

Here are some very wonderful functions to convert any latitude-longitude 
in the world to a pixel coordinate.

I developped these J formulas by playing around with encrypted and 
hidden javascript stuff from google map...

There is 17 zoom available...

The smallest world is on a single tile 256 * 256 pixels (zoom 0)....
The largest world is on 131072 tiles wide by 131072 tiles high... which 
is 33 554 432 pixels * 33 554 432 pixels (zoom 17)

Using these formulas, any pixels in your screen becomes 
geo-referenced.... and you can plot gps data directly on manually drawed 
images or georeferenced image....

sin =: 1&o.
PixByZoom =: 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 
262144 524288 1048576 2097152 4194304 8388608 16777216
PixByZoomLNG =: 
0.7111111111111111,1.4222222222222222,2.8444444444444445,5.688888888888889,11.377777777777778,22.755555555555556,45.51111111111111,91.02222222222222,182.04444444444445,364.0888888888889,728.1777777777778,1456.3555555555556,2912.711111111111,5825.422222222222,11650.844444444444,23301.68888888889,46603.37777777778,93206.75555555556
PixByZoomLAT =: 
40.74366543152521,81.48733086305041,162.97466172610083,325.94932345220166,651.8986469044033,1303.7972938088066,2607.5945876176133,5215.189175235227,10430.378350470453,20860.756700940906,41721.51340188181,83443.02680376362,166886.05360752725,333772.1072150545,667544.214430109,1335088.428860218,2670176.857720436,5340353.715440872
TilesPerZoom =: 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 
32768 65536 131072

PIXtoLL =: 3 : 0
    'zoom x y' =. y.
    zv =. zoom pick PixByZoom
    lon =. (x - zv) % (zoom pick PixByZoomLNG)
    lat =. (y - zv) % -(zoom pick PixByZoomLAT)
    lat =. 0.017453292519943295 %~ (+: arctan ^ lat) - 1.5707963267948965
    lat,lon  
)

LLtoPIX =: 3 : 0
    'zoom lat lon' =. y.
    zv =. zoom pick PixByZoom
    X =. <.zv + lon * (zoom pick PixByZoomLNG)
    Y =. 0.9999 <. _0.9999 >. (sin 0.017453292519943295 * lat)
    Y =. <.zv + 0.5 * (^. (1+Y) % (1-Y)) * -(zoom pick PixByZoomLAT)
    X,Y
)

Test =: 3 : 0
    NB. Quebec at Zoom 0 is at pixel 77,90 on (256*256)
    'X Y' =: LLtoPIX 0,46.99,-71.34855

    NB. Quebec at Zoom 17 is at pixel 10127049,11803340
   'X Y' =: LLtoPIX 17,46.99,-71.34855
)

Have a good day!

Simon



More information about the Programming mailing list