Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have taken a geotiff image from MODIS and i want to get a pixel (x,y) from that source image based on the longitude and latitude. I need a formula that how to calculate this without projection, any help would be appriciate.

What I have tried:

math - Convert longitude and latitude coordinates to image of a map pixels X and Y coordinates Java - Stack Overflow[^]
Posted
Updated 10-Sep-19 4:43am
v3

1 solution

You need the image dimension in pixels (width and height), a lat/lon reference point at a specified pixel position (e.g. lat0 and lon0 at x = y = 0), and the lat/lon degress covered by the image (e.g. by having another reference point at max. x and y).

First step is calculating the scaling factors in pixels per degree:
C++
double scale_x = width / lon_range;
double scale_y = height / lat_range;


Now you can get the pixel positions:
C++
double x = (lon - lon0) * scale_x;
double y = (lat - lat0) * scaly_y;


Finally convert them to integers with rounding:
C++
int xp = static_cast<int>((x > 0) ? x + 0.5 : x - 0.5);
int yp = static_cast<int>((y > 0) ? y + 0.5 : y - 0.5);</int></int>


Note: This is just from scratch (had not tested it).
 
Share this answer
 
Comments
Sukerbek 17-Nov-16 22:10pm    
thank you so much for answering But result was wrong
when i used the this parameters
width = 4023;
height = 4096;
lon0 = 103.21922302246094;
lat0 = 58.177616119384766;
lat = 37.552063;
lon = 125.518799;
lon_range = 360;
lat_range = 180;
Result
xp = 245;
yp = -454;
However result must be
xp = arround-> 2300 something; or bigger than 2000
yp = arround-> 2300 something; or bigger than 2000
Jochen Arndt 18-Nov-16 2:50am    
The lat/lon ranges must be the width and height of the image in degrees ("degrees covered by the image"), not the full lat/lon ranges of 180/360.
Sukerbek 21-Nov-16 21:39pm    
Okay thank you so much i have solved my problem by GDAL library
Member 14586399 11-Sep-19 2:19am    
@Sukerbek, how did you solve it using goal, I am trying to achieve the same thing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900