I've got a function that converts x,y of raster to geographic coordinates in given transformation (where gt
is GetGeoTransform()
from GDAL
):
def p(col, row, gt): # p: pixel coords to map coords
c, a, b, f, d, e = gt
x_geo = a * col + b * row + a * 0.5 + b * 0.5 + c
y_geo = d * col + e * row + d * 0.5 + e * 0.5 + f
return x_geo, y_geo # map coordinates
But how do I reverse it, so it would be geographic coordinates to pixel coordinates? Becasue mathematically it doesn't seem to work correctly:
def p_reverse(col, row, gt):
c, a, b, f, d, e = gt
x_geo = a / col - b / row - a / 0.5 - b / 0.5 - c
y_geo = d / col - e / row - d / 0.5 - e / 0.5 - f
return x_raster, y_raster
What am I doing wrong?