1

I have downloaded a map file which I'm using as a texture on a plane. The file is a proper map file with projection in a prj-file and a tfw-file. The prj file contains:

GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.017453292519943295]]

And the tfw-file contains:

      0.00000000000000 
      0.00000000000000 
      -0.03333333333333 
      -179.98333333333333 
      89.98333333333333 

The original file is 10800x5400 pixels, and I've scaled it down to 2048x1024 pixels. So, how do I calculate the long/lat pair if someone clicks on point [712, 133] on my scaled down image?

Bonus things I'm wondering about:

  • The image will be rescaled several times, but will keep the aspect ratio, is that a problem?
  • How do I do this in reverse? IE if I want to find the x,y pair from a long/lat pair.

I've spent hours trying to read in random places about GIS, maps and whatnot but it seems to be a very steep learning curve and I'd be happy to take a shortcut as in the whole it's a fairly minor part of what I'm trying to do. Pseudo code, c#, some other language doesn't really matter, but I'm surprised how hard it has proven (for me at least) to find such hands on advice.

Sorry about the cross post, asked here originally but based on the comment I figured it might make more sense to ask here

  • That 3rd value in the tfw file is the cell size in coordinate reference system units. When you resize the image, that value (often there are two--one for width and one for height) has to change. – mkennedy Feb 5 at 20:56
  • The first value is the X-size of a pixel and should not be 0. – Ian Turton Feb 6 at 9:12
  • Thanks for the info (can't @ both), the map is from naturalearthdata.com/downloads/50m-natural-earth-1/… so I guess they've put in things wrong in the tfw file. Luckily I don't seems to need that in this case. Maps are clearly a scary place, and I'm glad I only had to use a linear function to get my values :) – Jon Feb 6 at 11:02
1

Judging from the world file, I assume your image represents the whole world (from -180° to 180° longitude and 90° to -90° latitude), then if you know your resized image dimensions (let's notate them imgsize_x,imgsize_y), you can calculate the long/lat position of a pixel center like this:

longitude = ((pixel_x - 0.5) * 360 / imgsize_x) - 180
latitude = 90 - ((pixel_y - 0.5) * 180 / imgsize_y)

Calculating pixel x,y from long/lat is:

pixel_x = ((longitude + 180) * imgsize_x) / 360 + 1
pixel_y = ((90 - latitude) * imgsize_y / 180) + 1

Taking the integer part of the result will tell you in which pixel the long/lat lies.

Note that this works because your image in is a geographic crs (GCS_WGS_1984). If your image were in some other projected crs like Mercator, Miller, etc. you would need to transform from projected to geographic coordinates in the process.

  • Thanks a bunch works perfect! Just out of curiosity, is there any easy to understand resource with just "if your map image is of this type, here's how you go about it"? Like the above and for different projections? Everything I've found is assuming you either know a lot about maps, or that you are about to learn everything. – Jon Feb 6 at 6:34
  • 1
    Sadly, it's not always that simple. Your image was in an unprojected crs, basically equivalent to an equirectangular projection, where the lat/long graticule is represented as a 2D grid of perfect squares. This makes the conversion from pixel coordinates very straightforward, since the conversion boils down to a simple matter of scaling/translating appropriately. With other projections though, the math can be much more involved, and specific software or libraries are usually used to deal with those transformations. – FSimardGIS Feb 7 at 2:29
  • I see, and the world of maps is truly a scary place. Really happy that I was lucky with my map. Again, thanks! – Jon Feb 8 at 9:04
  • pixel_x = ((longitude + 180) * imgsize_x) / 360 + 1 @FSimardGIS what is +1 here for? Thanks – SERG Mar 5 at 8:22
  • @SERG I had assumed that the pixels were numbered starting at 1, while the mathematical coordinate frame starts at 0, so to make sure the pixel number was computed correctly when we extract the integer part, I added 1. However, if the first pixel row and column are numbered 0, which is very common, then it is not necessary to add 1, and we should add 0.5 instead of subtracting 0.5 in the pixel to lat/lon part. – FSimardGIS Mar 11 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.