Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a grid, which has flipped X and Y coordinates (X is represented by Y values and Y is represented by X values). This results in grid being positioned in completely wrong place.

How can I flip the coordinates without loss of quality and doing by GDAL. Is it possible? If not, is there any other Linux library, that can achieve that?

Thank you,

Matej

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If the rasters are square (such as tiles), with equal numbers of rows and columns, and the format is writable, this is easy to do using a Python loop with the GDAL library:

from osgeo import gdal

fname = '/path/to/tile.tif'
ds = gdal.Open(fname, gdal.GA_Update)
assert ds.RasterXSize == ds.RasterYSize:

for iBand in range(ds.RasterCount):
    band = ds.GetRasterBand(iBand + 1)
    ar = band.ReadAsArray()
    band.WriteArray(ar.T)  # .T means transpose 2D array
ds = band = None  # save, close

However for non-square rasters, a more complicated approach is required to recreate a new raster:

  • The raster driver would need to support gdal.Create() (check driver = ds.GetDriver(); driver.GetMetadataItem('DCAP_CREATE'))
  • Create() requires the same pixel type, and may require several creation options
  • Several data source objects would need to be copied over, such as the projection (ds.GetProjection())
  • There is potentially more metadata for each band, such as band.GetNoDataValue()
  • The geotransform (ds.GetGeoTransform()) might need to be redefined, as it references the X and Y coordinate to the upper left corner. If the transpose axis is not the upper left corner, then it would need to be calculated.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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