From Gerris

Revision as of 17:56, 24 April 2011; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

xyz2kdt is a command-line utility used to create the Kd-tree-indexed terrain databases used as input for the objects of the Terrain module. A summary of the command-line syntax is given by

% xyz2kdt -h
Usage: xyz2kdt [OPTION] BASENAME

Converts the x, y and z coordinates on standard input to a
2D-tree-indexed database suitable for use with the
terrain module of Gerris.

  -p N  --pagesize=N  sets the pagesize in bytes (default is 4096)
  -v    --verbose     display progress bar
  -h    --help        display this help and exit

Report bugs to

The format of the data on standard input should look like

3.501 5.634 -2
4.601 7.6778 3.456

where the first field is the value of the x-coordinate, the second field the y-coordinate and the last field the z-coordinate. xyz2kdt will stop at the first line which does not fit this format. You may want to check (e.g. using the --verbose option) that the number of points processed matches what you expect. Note that the database does not enforce any other convention.

Example 1: building a global terrain topography database using the ETOPO2 dataset

The ETOPO2 dataset contains topographic information for the entire surface of the Earth (both above and below sea level) at a nominal resolution of two arc-minutes (~4 km).

The first step is to get the raw data e.g.

% wget
% unzip

This is a binary file with a format described in the ETOPO2v2c_i2_LSB.hdr file

% cat ETOPO2v2c_i2_LSB.hdr
NCOLS 10800
NROWS 5400
XLLCORNER -180.000000
YLLCORNER -90.000000
CELLSIZE 0.0333333333333333333
MIN_VALUE -10791.0
MAX_VALUE 8440.0

We need to convert this binary file to a text file. We also want to use the database together with a cartographic projection defined using the Map module. By definition this means that our x-, y- and z-coordinates need to be the east-positive longitude, north-positive latitude and elevation in metres. We can easily get these coordinates in a text format suitable for input into xyz2kdt using the following C code:

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <arpa/inet.h>
/* check that this matches ETOPO2v2c_i2_LSB.hdr */
#define NCOLS 10800
#define NROWS 5400
#define XLLCORNER -180.000000
#define YLLCORNER -90.000000
#define CELLSIZE 0.0333333333333333333
#define NODATA_VALUE 999999
#define MIN_VALUE -10791.0
#define MAX_VALUE 8440.0
int main (int argc, char * argv[])
double lat, lon;
int16_t v;
int i, j;
for (j = 0; j < NROWS; j++) {
for (i = 0; i < NCOLS; i++) {
assert (fread (&v, sizeof (int16_t), 1, stdin));
assert (v >= MIN_VALUE && v <= MAX_VALUE);
printf ("%.8f %.8f %d\n", lon + CELLSIZE/2., - (lat + CELLSIZE/2.), v);
fprintf (stderr, "\rRow %d/%d ", j + 1, NROWS);
fputc ('\n', stderr);
return 0;

Just copy and paste this code into a file called e.g. etopo2xyz.c and compile using

% cc etopo2xyz.c -o etopo2xyz

The following command will then read the binary ETOPO2 file, convert it to the appropriate text format and generate the final terrain database

% ./etopo2xyz < ETOPO2v2c_i2_LSB.bin | xyz2kdt -v etopo2

If everything went well you should end up (~ one hour later) with three large files

% ls etopo2*
etopo2.kdt etopo2.pts etopo2.sum

which together define the terrain database.

Note also that when using several databases simultaneously within GfsRefineTerrain, you need to choose consistent conventions for all the databases (for example a common geodetic system e.g. WGS84). The terrain databases do not know anything about projection systems and it is up to you to enforce your preferred conventions.

Example 2: building a global terrain dataset using SRTM data

awk '
n = 0;
if ($1 == "ncols")
ncols = $2;
else if ($1 == "nrows")
nrows = $2;
else if ($1 == "xllcorner")
x = xllcorner = $2;
else if ($1 == "yllcorner")
yllcorner = $2;
else if ($1 == "cellsize") {
cellsize = $2;
y = yllcorner + cellsize*(nrows - 1);
else if ($1 == "NODATA_value")
NODATA_value = $2;
for (i = 1; i <= NF; i++) {
if ($i != NODATA_value && $i > 0.)
printf ("%f %f %f\n", x,y,$i);
x += cellsize;
if (n >= ncols) {
# print "";
y -= cellsize;
x = xllcorner;
n = 0;

for i in `seq 1 1 72`; do
for j in `seq 1 1 24`; do
ii=`echo $i | awk '{printf ("%02d", $1)}'`
jj=`echo $j | awk '{printf ("%02d", $1)}'`
rm -f srtm_$ii\_$ srtm_$ii\_$jj.asc
if wget -q$ii\_$; then
echo srtm_$ii\_$ > /dev/stderr
unzip -q -o srtm_$ii\_$ srtm_$ii\_$jj.asc
asc2xyz < srtm_$ii\_$jj.asc
rm -f srtm_$ii\_$ srtm_$ii\_$jj.asc
done | xyz2kdt -v srtm

See also

Karamea flood tutorial

Personal tools