1

I have two sets of Lat and Long values corresponding to two corners of a rectangle (top left and bottom right). I want to load a image in the rectangle and as the user clicks on the locations on the rectangle/image, it should return a latitude and longitude (from the detected pixel's coordinates). What should be the calculation to convert pixels to lat and long, given the above spot of inputs.

Sourav
  • 129
  • 12
  • Does this do what you want? https://stackoverflow.com/a/4249711/8085668 – WizardCoder Jun 07 '17 at 12:12
  • I assume you are able to get the pixel coordinates from the mouse click event. Then you can treat geographic coordinates in a rather linear way: The total width in pixel is representing the difference between left and right longitute: `lon_pixel = lon_left + click.x / width * (lon_right - lon_left)` – Gerhardh Jun 07 '17 at 12:23
  • Lat/Long is not a linear 2D grid... If your image covers any "substantial" area then you will need to consider the curvature of the planet, and the projection of your image / map – Attie Jun 07 '17 at 12:25
  • my map corresponds to a small region 500m by 500m, so I guess curvature can be ignored in this case – Sourav Jun 07 '17 at 13:31
  • @Sourav It is not the size of the grid that is important, it is _where_. If the location is near the north or south pole, simple linear transformations do not work - unless one is on [bizarro world](https://www.sott.net/image/s11/223384/full/bizarroworld.jpg) ;-). – chux - Reinstate Monica Jun 07 '17 at 13:35
  • What are the types of Lat, Long and the two corners? A quality answer depends on that. – chux - Reinstate Monica Jun 07 '17 at 13:37

2 Answers2

2

You should know what geodetic projection has been used to create that map and how rectangular coordinates correspond to geographic ones.

For small map pieces you can use linear approximation, so coordinates might be calculated with linear interpolation.

Example for x-coordinate and longitude:

Lon = LonLeft + X * (LonRight - LonLeft) / Width 
MBo
  • 77,366
  • 5
  • 53
  • 86
  • void pixels_to_coordinates(int _xlength, int _ylength, double& _latt, double& _longg) { _longg = longg_top_left + (double)_ylength * (longg_bottom_right - longg_top_left)/ (601); _latt = latt_top_left + (double)_xlength * (latt_bottom_right - latt_top_left )/ (341); } – Sourav Jun 09 '17 at 07:34
  • I tried this code nut this shifts too much from the actual positions. A m I doing something wrong? – Sourav Jun 09 '17 at 07:35
  • What is _ylength and _xlength? If they are click coordinates - exchange them (x for longitude) – MBo Jun 09 '17 at 08:37
0

Assuming linear interpolation is good enough for your purposes, you can use following formula:

out = (in - inMin) * (outMax - outMin) / (inMax - inMin) + outMin

where in* are image coordinates and out* are map coordinates.

You have to use formula twice; once for horizontal and once for vertical coordinate.

user694733
  • 15,208
  • 2
  • 42
  • 68