Topics

Float precision on writing grid

Betman, Maarten
 

I'm trying to transform xyz data to a raster grid using rasterio

I've loaded the elevation data into a numpy array, the precision of the z values is 2 decimals and the array has dtype float64.

after loading the xyz data and putting elevation data in a grid the array zi has the following properties and example value in the array:

[in]  zi.shape

[out] (3796, 3557)

 

[in]  zi.dtype

[out] dtype('float64')

 

[in]  zi[1000,1000]

[out] -5.27

I'm trying to write the gridded data to an ESRI Ascii file using rasterio and the AAIGrid driver.

import rasterio

from rasterio.transform import from_origin

from fiona.crs import from_epsg

 

transform = from_origin(xmin, ymax, 0.5, 0.5)

 

new_dataset = rasterio.open('test1.asc', 'w', driver='AAIGrid',

                        height = zi.shape[0], width = zi.shape[1],

                        count=1, dtype=str(zi.dtype)

                        crs=from_epsg(32750),

                        transform=transform,

                        nodata = -9999)

 

new_dataset.write(zi, 1)

new_dataset.close()

The z grid is written to the ESRI Ascii file successfully, only the "precision" of the values is now 19 decimals. This makes the files unnecessarily large and slows down programs using the gridded files.

Header ESRI Ascii file and first 2 values:

ncols        3557

nrows        3796

xllcorner    765005.500000000000

yllcorner    9430016.000000000000

cellsize     0.500000000000

NODATA_value -9999

-0.029999999329447746277 -0.029999999329447746277

I've tried changing the datatype to float32 and added argument "precision = 2" to rasterio.open. Is their a way I can get the number of decimals down?

Matthew Perry
 

You can control the precision of the AAIGrid output using the GDAL creation options DECIMAL_PRECISION (or SIGNIFICANT_DIGITS). You can specify these as case-insensitive kwargs like `rasterio.open(..., decimal_precision=3)`