[gdal-dev] writing XYZ file

Even Rouault even.rouault at mines-paris.org
Sat Nov 9 11:02:23 PST 2013


Le samedi 09 novembre 2013 10:46:20, denis cohen a écrit :
> Hello,
> 
> I am a new user of gdal.
> I used gdal_translate to convert a netcdf file to xyz:
> gdal_translate -of XYZ file.nc file.xyz
> 
> However, the xyz file starts with the upperleft coordinate.
> Is it possible to have it write the xyz file starting with the lower left
> coordinate?

Not directly although it wouldn't be complicated to add an option to do that.

You can try the following GDAL Python script that will create an intermediary 
TIFF image that has both inverted georeferencing and imagery in a consistant 
way.

python invert.py file.nc file_reserved.tif
gdal_translate -of XYZ file_reversed.tif file.xyz


----- Begin of script invert.py ------

# -*- coding: utf-8 -*-
import sys
from osgeo import gdal

src_ds = gdal.Open(sys.argv[1])
xsize = src_ds.RasterXSize
ysize = src_ds.RasterYSize
bandcount = src_ds.RasterCount
out_drv = gdal.GetDriverByName('GTiff')
out_ds = out_drv.Create(sys.argv[2], xsize, ysize, bandcount)
out_ds.SetProjection(src_ds.GetProjectionRef())
src_gt = src_ds.GetGeoTransform()
out_gt = [ src_gt[i] for i in range(6) ]
out_gt[3] = src_gt[3] + ysize * src_gt[5]
out_gt[5] = -src_gt[5]
out_ds.SetGeoTransform(out_gt)
for j in range(src_ds.RasterYSize):
    data = src_ds.ReadRaster(0, ysize - 1 - j, xsize, 1)
    out_ds.WriteRaster(0, j, xsize, 1, data)
out_ds = None
src_ds = None

----- End of script ------

Even

-- 
Geospatial professional services
http://even.rouault.free.fr/services.html


More information about the gdal-dev mailing list