Skip to main content
2 of 3
Removed tag prefix from question title as per Jeff Atwood style preference
underdark
  • 84.5k
  • 22
  • 233
  • 415

Extract a cutline from VRT file in world coordinates?

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?

christoph
  • 6.4k
  • 1
  • 23
  • 42