Reputation: 509
I have a picture of a size (1280, 1792, 4) and it's a weather forecast map. It's said that the left top corner corresponds to geographical coordinates latitude: 55.78, longitude: -135.0 and bottom right is latitude: 21.94, longitude: -67.5. Given a geographical points on this map for example (latitude: 35.78, longitude: -100.0) I would like to know what are the pixel coordinates on the original picture.
I tried plotting one spot on it and it went ok, but I need to know what are the original pixel coordinates on the original image...
fig, ax = plt.subplots()
weather_coordinates = json.load(open('weather.json'))
ax.scatter(-70.935242, 40.730610, edgecolors='red', linewidths=2, zorder=2)
ax.imshow(
mpimg.imread('weather.png'),
extent=(
weather_coordinates.get('top_left').get('longitude'),
weather_coordinates.get('bottom_right').get('longitude'),
weather_coordinates.get('bottom_right').get('latitude'),
weather_coordinates.get('top_left').get('latitude')),
zorder=1)
plt.show()
Upvotes: 0
Views: 1219
Reputation: 432
You can use numpy's interp() function to achieve this. It takes the value you want to transform in the 1st argument, and then the input range, and output range as arrays, so interp(100, [0, 200], [500, 1000]) will return 750. Since you need both y and x pixel values, you need to call it twice like so:
from numpy import interp
# the range of the latitudes and longitudes
latRange = [21.94, 55.78] # flipped from descending to ascending
lonRange = [-135.0, -67.5]
# the range of y and x pixels
yRange = [0, 1280]
xRange = [0, 1792]
lat = 35.78
lon = -100.0
yPixel = yRange[1] - interp(lat, latRange, yRange) # flipped again
xPixel = interp(lon, lonRange, xRange)
Note that, because the function only works for increasing ranges, we have to flip the latitude range [55.78, 21.94] to [21.94, 55.78], and then subtract the yPixel from the max latitude to get a correct answer.
Upvotes: 1