[GIS] Convert x, y position in georeferenced image (with world file) to longitude, latitude

coordinate systemgeoreferencingraster

What steps do I need to do in order to calculate longitude and latitude for a specific pixel in a georeferenced image? For example this image http://www.shadedreliefarchive.com/medit_Mideast_africa_copy.html contains information, .tfw file:

1627.32174969982000 
0.00000000000000 
0.00000000000000 
-1627.32174969982000 
-4957506.34276705000000 
6096922.76188281000000 

and .prj file:

PROJCS["Lambert Azimuthal Equal-Area",
       GEOGCS["GCS_WGS_1984",
              DATUM["D_WGS_1984", SPHEROID["WGS_1984", 6378137, 298.257223563]],
              PRIMEM["Greenwich", 0],
              UNIT["Degree", 0.017453292519943295]],
       PROJECTION["Lambert_Azimuthal_Equal_Area"],
       PARAMETER["false_easting", 0],
       PARAMETER["false_northing", 0],
       PARAMETER["latitude_of_origin", 0],
       PARAMETER["central_meridian", 20],
       PARAMETER["xy_plane_rotation", 0],
       UNIT["Meter", 1]]

I've created this snippet of javascript to compute new x1 and y1 values for specific pixels in the image:

var A = 1627.32174969982000,
    D = 0.00000000000000,
    B = 0.00000000000000,
    E = -1627.32174969982000,
    C = -4957506.34276705000000,
    F = 6096922.76188281000000;

function x1(x, y) { return A*x + B*y + C; }

function y1(x, y) { return D*x + E*y + F; }

What more do I have to do to get longitude and latitude? For example C, F represents the top-left corner of the image, right? Then how do I translate C, F to get longitude and latitude?

I want do do it using javascript only, have looked at http://proj4js.org/ but don't really understand what to do with the values I get…

Best Answer

The .tfw represents the affine transform for the image -- how image pixel coordinates map to coordinates in the space defined by the .prj.

Here's an explanation of the values: http://en.wikipedia.org/wiki/World_file

Getting to geographic coordinates is thus a two-step process:

  • Find the coordinates of the pixel in the projected coordinate system
  • Unproject this coordinate back to the geographic coordinate system to arrive at lat & lon.

The formula for the first step:

X_proj = C + (A * X_img) + (B * Y_img) 
Y_proj = F + (E * Y_img) + (D * X_img)

Note: I'm using the variables A..F from the article I linked, not from your code above! I don't believe they correspond; the Wikipedia article chose labels that don't correspond to line numbers.

Now you have X & Y coordinates in your desired coordinate system. Since you're using Javascript (using proj4js; forgive me if I haven't tested the code below):

var source = new Proj4js.Proj('PROJ4_ARGS_FOR_YOUR_PROJECTION');
var dest = new Proj4js.Proj('EPSG:4326') // geographic coordinates + WGS84 (which matches the ellipsoid used in your .prj)

var p = new Proj4js.Point(X_proj, Y_proj)
Proj4js.transform(source, dest, p)

// p.x & p.y are now lat & lon. (or lon & lat, can't recall coord order)

I used spatialrefrence.org to convert your .prj into arguments for Proj4; you can see it here by clicking on 'proj4': http://spatialreference.org/ref/sr-org/7119/ In general, .prj to proj4 args is a purely mechanical transformation.

Related Question