Import XYZ

From GRASS-Wiki
Revision as of 09:01, 12 May 2009 by Neteler (talk | contribs) (improved)
Jump to navigation Jump to search

Import XYZ DEM data

There are a couple of ways to import regular DEM (elevation) data in XYZ ASCII format:


Import with r.in.xyz

Example: We want to import a XYZ ASCII file "VTL2733.XYZ". First we look at the structure of the file:

 head -n 4 VTL2733.XYZ
 617000.0 148000.0 157.92
 617025.0 148000.0 157.82
 617050.0 148000.0 157.70
 617075.0 148000.0 157.60
 ...

It indicates a 25m raster cell size. To import, we run a few commands:

# First scan the map extent in human readable way (reduce white space on the fly, read from standard-input):
cat VTL2733.XYZ| r.in.xyz -s in=- fs=space out=test
Range:     min         max
x: 617000.000000 619250.000000
y: 148000.000000 151000.000000
z:  149.160000  162.150000

# set region, get from scanning the file again, now in machine-readable form (eval and -g flag):
eval `cat VTL2733.XYZ | r.in.xyz -s -g in=- fs=space out=test`
g.region n=$n s=$s w=$w e=$e res=25 -p

# enlarge region by half raster cell resolution to store values in cell-center:
g.region n=n+12.5 s=s-12.5 w=w-12.5 e=e+12.5 -p

# verify that the number of rows in the ASCII file corresponds to the number of cells in enlarged region
wc -l VTL2733.XYZ

# finally import as raster map:
cat VTL2733.XYZ | r.in.xyz in=- fs=space out=vtl2733

Now you can analyse the imported elevation map or export into a reasonable raster format (e.g., GeoTIFF) with r.out.gdal.

Import multiple files in one step

Just amend above procedure to use wildcards. Change in above example all occurencies of

 cat VTL2733.XYZ

to

 cat *.XYZ

and use a more reasonable output name of course. That's all to import even thousands of files (tiled DEM) easily.

Hint if there are multiple white spaces as separator

Sometimes one comes across ASCII tables formatted in unfortunate ways, e.g. like this:

 617000.0 148000.0  157.92
 617025.0 148000.0  157.82
 617050.0 148000.0  157.70
 617075.0 148000.0  157.60
 ...

Above mentioned GRASS commands refuse to import directly this file since they require single (and not replicated) field separators.

Solution: On Unix-based systems, you can get rid of this on the fly and import the XYZ ASCII file to a raster map combining some text tools and GRASS commands. Just amend above procedure to use "tr".

Change

cat VTL2733.XYZ | r.in.xyz ...

to

cat VTL2733.XYZ | tr -s ' ' ' ' | r.in.xyz ...

Then proceed as above.

See also