# How to convert image pixel to latitude and longitude

We are developing an android application for showing an image with geo referened with .jgw and .prj file as below. we want to identify the geo location coordinate of image coordinate(in pixel) that user touched on the image. How we can use image cordinate to find ts relative latitude and longitude using java in android.

We tried proj4 java library for android but it seems incorrect when we find the distance of two point and we couldn't trace where is the issue.

Below are our .jgw and .prj file

JGW File

0.00000180562999999 0 0 -0.00000180562999999 76.882389 (dummy location due to NDA) 8.572607 (dummy location due to NDA)

PRJ File

GEOGCS["GCS_WGS_1984",DATUM["D_WGS84",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

You have world files (.jgw) and projection definition files (.prj).

World file values:

``````Line 1: A: pixel size in the x-direction in map units/pixel
Line 2: D: rotation about y-axis
Line 3: B: rotation about x-axis
Line 4: E: pixel size in the y-direction in map units, almost always negative
Line 5: C: x-coordinate of the center of the upper left pixel
Line 6: F: y-coordinate of the center of the upper left pixel
``````

You can convert pixel coordinates to map coordinates (longitude, latitude assuming the projection is always WGS84 / EPSG 4326) using some simple maths:

``````mx = C + px * A + py * B
my = F + px * D + py * E
``````

Where mx, my = map coordinates and px, py = pixel coordinates.

In your case, as the rotation terms are zero, the equation becomes:

``````mx = C + px * A
my = F + py * E
``````

Which means x or y coordinate of top left corner + image coordinate * pixel size.

• @Shibu You asked how to get map coordinates from pixel coordinates, so you must already have the pixel coordinates... Feb 15, 2017 at 10:09
• Actually the px and py is coming for our own image. In our .jgw file C F looks like lat and lon of left top conrner. If so how we can get its corresponding C and F values. When we look in to some other example its looks like long integer values(not lat and lon) do you have any idea? Feb 15, 2017 at 10:21
• @Shibu The C and F values are in your .jgw so I'm not sure what you mean. Other examples might be in a different projection, but that doesn't matter if you only have to work with your image. Feb 15, 2017 at 10:50
• Thank you luke. Some example mislead us. This solution work for us Feb 15, 2017 at 11:48
• the last example should not rather be "my = F + py * E" ? Oct 6, 2019 at 11:31

You can use GDAL, for which bindings are available in a variety of languages including Java. The tool gdaltransform will do exactly what you want. You can also use gdallocationinfo. This will give you both the pixel value and its coordinates.

• After googling GDAL, we got below example. gdaltransform -s_srs '+proj=lcc +lat_1=34.03333333333333 +lat_2=35.46666666666667 +lat_0=33.5 +lon_0=-118 + x_0=2000000 +y_0=500000 +ellps=GRS80 +units=m +no_defs' -t_srs epsg:4326. Could you explain how we will get x_0,y_0 from our jgw file? Feb 15, 2017 at 9:47