Convertire un file di griglia ASCII in GeoTIFF usando Python?


11

Ho un file di formato raster ASCII griglia. Per esempio:

ncols 480
nrows 450
xllcorner 378923
yllcorner 4072345
cellsize 30
nodata_value -32768
43 2 45 7 3 56 2 5 23 65 34 6 32 54 57 34 2 2 54 6 
35 45 65 34 2 6 78 4 2 6 89 3 2 7 45 23 5 8 4 1 62 ...

Come posso convertirlo in TIFF o in qualsiasi altro raster usando Python?


Il software GIS può convertire asci in geotiff. Nessuna codifica necessaria. Uso QGIS. È gratis.
— Saul Sheard,

Risposte:


13

La versione dello pseudo codice:

import gdal
import numpy

create the gdal output file as geotiff
set the no data value
set the geotransform 

numpy.genfromtxt('your file', numpy.int8) #looks like int from you example
reshape your array to the shape you need

write out the array.

Un esempio che ti aiuterà lungo - da qui :

if __name__ == '__main__':
    # Import libs
    import numpy, os
    from osgeo import osr, gdal

    # Set file vars
    output_file = "out.tif"

    # Create gtif
    driver = gdal.GetDriverByName("GTiff")
    dst_ds = driver.Create(output_file, 174, 115, 1, gdal.GDT_Byte )
    raster = numpy.zeros( (174, 115) )

    # top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution
    dst_ds.SetGeoTransform( [ 14.97, 0.11, 0, -34.54, 0, 0.11 ] )

    # set the reference info 
    srs = osr.SpatialReference()
    srs.SetWellKnownGeogCS("WGS84")
    dst_ds.SetProjection( srs.ExportToWkt() )

    # write the band
    dst_ds.GetRasterBand(1).WriteArray(raster)

e i valori 14.97 e -34.54 sono le coordinate dell'angolo in alto a sinistra nei cooridanati del WGS84?
— Slava,


7

Potrebbe essere più semplice fare una copia di creazione, poiché il tuo file è un AAIGrid e GTiff supporta CreateCopy ():

from osgeo import gdal, osr
drv = gdal.GetDriverByName('GTiff')
ds_in = gdal.Open('in.asc')
ds_out = drv.CreateCopy('out.tif', ds_in)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
ds_out.SetProjection(srs.ExportToWkt())
ds_in = None
ds_out = None

Qualsiasi driver che supporti CreateCopy può utilizzarlo.


Se non hai bisogno di usare il pitone, il bananafish è sicuramente giusto.

fantastico, grazie! Il mio file di input .asc viene fornito senza un CRS. C'è un modo per specificare questo CRS (GCS WGS 84) prima di scrivere il raster?
— RutgerH,

usa SetProjection e una stringa. È possibile ottenere la stringa da osr. Vedi risposta modifica.
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.