1

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?

1

The image/raster coordinates of a point x,y (world coordinates) are

gt = (263104.72544800001, 10.002079999999999, 0.0, 155223.647811, 0.0, -10.002079999999999)
x,y  = (263220.5,155110.6)
rasterx = (x - gt[0]) / gt[1]
rastery = (y - gt[3]) / gt[5]
print rasterx,rastery
11.5750475901 11.3024301945

This allows to extract the value of the pixel at coordinate x, y (a = number of band 1,2, 3)

layer.GetRasterBand(a).ReadAsArray(int(rasterx),int(rastery), 1, 1)

Therefore, if you have the image coordinates (rasterx, rastery)

x = rasterx * gt[1] + gt[0]
y = rastery * gt[5] + gt[3]
print x, y
263220.5 155110.6
  • Maybe that's correct for images/rasters that are not clipped. But for my VRT cutline it doesn't work. (gt[0],gt[3]) is not the origin of the original image, it's the origin of the clipped image. And the cutline seems to have the original image coordinates. – christoph Feb 11 '16 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.