I'm trying to extract the world coordinates of a cutline (clipping polygon) in a GDAL VRT file. Unfortunately, the cutline is stored in image coordinates (pixels), and i could not find the right method to transform it into world coordinates.
The following python functions didn't help:
def Pixel2world(geoMatrix, x, y):
ulX = geoMatrix[0]
ulY = geoMatrix[3]
xDist = geoMatrix[1]
yDist = geoMatrix[5]
coorX = (ulX + (x * xDist))
coorY = (ulY + (y * yDist))
return (coorX, coorY)
def Pixel2coord(geoMatrix, x, y):
"""Returns global coordinates from pixel x, y coords"""
xoff, a, b, yoff, d, e = geoMatrix
xp = a * x + b * y + xoff
yp = d * x + e * y + yoff
return(xp, yp)
raster=r'virtual_raster.vrt'
ds=gdal.Open(raster)
gt=ds.GetGeoTransform()
print Pixel2coord(gt,1000,1000) #=> Returns wrong coordinates
print Pixel2world(gt,1000,1000) #=> Returns wrong coordinates
BTW: due to a coordinate transformation inside the VRT from EPSG:31255 to EPSG:3857, the resulting image is shifted and rotated. And (geoMatrix[0],geoMatrix[3]) does contain the upper left corner of the cutted image and not the uncutted one.
Has anyone successfully extracted a VRT cutline in world coordinates?