Information on transforming ESRI ASCII grids to X,Y,Z format

 There are many problems distributing large raster files. First, there is the large size of the files; second, the different data formats used by the most common proprietary and free software; third, the binary representation of numbers ( Endianness ) changes depending on the hardware platform; and finally, there is this issue of convenience of the maintainer's research group.

I have settled on ESRI, Inc.'s ASCII Grid format because it (1) stores data in a reasonably compact raster format when compressed with Zip, (2) is widely supported and easy to convert with a small script if necessary, (3) is an ASCII format so it is both human readable and hardware independent; and (4) is easy to import and export from my GIS software (ArcGIS).

To import ASCII Grids into common GIS packages (please email me with instructions if your package also supports the format):

  • ArcGIS: Use ArcTools's Import ASCII to GRID function
  • ArcView: use the import ASCII Grid function (May need Spatial Analyst)
  • GRASS: Use the ' r.in.arc ' function.

For those software packages that do not natively support ESRI ASCII Grids, a full description of the data format follows. Scroll down to the bottom for a Python script that converts these grids to XYZ triplets.

ESRI ASCII Grid Format

(Copied from the ArcWorkstation 8.3 Help File)

The ASCII file must consist of header information containing a set of keywords, followed by cell values in row-major order. The file format is:

<NCOLS xxx>
<NROWS xxx>
<XLLCENTER xxx | XLLCORNER xxx>
<YLLCENTER xxx | YLLCORNER xxx>
<CELLSIZE xxx>
{NODATA_VALUE xxx}
row 1
row 2
.
.
.
row n

where xxx is a number, and the keyword nodata_value is optional and defaults to -9999. Row 1 of the data is at the top of the grid, row 2 is just under row 1 and so on.

For example:

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 ...

The nodata_value is the value in the ASCII file to be assigned to those cells whose true value is unknown. In the grid they will be assigned the keyword NODATA.

Cell values should be delimited by spaces. No carriage returns are necessary at the end of each row in the grid. The number of columns in the header is used to determine when a new row begins.

The number of cell values must be equal to the number of rows times the number of columns, or an error will be returned.

Conversion to XYZ triplets

I have written two Python scripts to convert ASCII Grids to xyz triplets for those people who need them:

  1. grd2xyz.py - This is a simple script that will convert 1 grid at a time. I recommend this version for those uncomfortable with scripts in general.

    Download the script to the same directory as the ASCII grids, modify the input and output filenames (including full paths) in a text editor and run the script by double clicking on the icon (in Windows) to extract the xyz data. Warning: this will more than triple the size of the file on disk and can take several minutes to run on the larger grids I provide.

  2. grd2xyz2.py - This is a full CLI program that writes results to stdout (by default). You can change the deliminator with the -d switch and get a simple help printout with -h.

IMPORTANT: Both scripts require Python! Download and install a free copy of Python from www.python.org. Also, My scripts do not handle the XLLCENTER format. So, adjust your header accordingly.