What are the corresponding fields to set in libtiff or libgeotiff, given a minimal ESRI ASCII file?
Even if you are planning not to use GDAL in your application (more on that later), nothing prevents you from generating a geotiff from the ESRI ASCII GRID (gdal_translate -of "GTiff" in.asc out.tif
), and inspect the TIFF tags of the generated file (these are the necessary tags to generate a geotiff from the given grid).
AsTiffTagViewer gives the following output (similar to TiffTags utility output):
TagCode (Count DataType): Value // my comments
ImageWidth (1 Short): 5 // ncols
ImageLength (1 Short): 4 // nrows
BitsPerSample (1 Short): 32
Compression (1 Short): Uncompressed
Photometric (1 Short): MinIsBlack
StripOffsets (1 Long): 260
SamplesPerPixel (1 Short): 1
RowsPerStrip (1 Short): 4 //nrows
StripByteCounts (1 Long): 80
PlanarConfig (1 Short): Contig
SampleFormat (1 Short): 3
33550 (3 Double):
33922 (6 Double):
42113 (6 ASCII): -9999 // NODATA_value
As you can see there are 11 standard Tiff tags and 3 non standard tags (but we know the Data Types and more importantly the dimensions of that last 3 tags, 3
,6
,6
). Let's confirm we have 2 GeoTiff tags and one non standard GDAL-specific tag.
Libgeotiff C library is distributed with listgeo
utility for dumping GeoTIFF metadata. The output:
Geotiff_Information:
Version: 1
Key_Revision: 1.0
Tagged_Information:
ModelTiepointTag (2,3):
0 0 0
0 40 0
ModelPixelScaleTag (1,3):
10 10 0
End_Of_Tags.
Keyed_Information:
End_Of_Keys.
End_Of_Geotiff.
Corner Coordinates:
Upper Left ( 0.000, 40.000)
Lower Left ( 0.000, 0.000)
Upper Right ( 50.000, 40.000)
Lower Right ( 50.000, 0.000)
Center ( 25.000, 20.000)
By the dimensions of the Tagged_Information
we can identify the following 2 tags. Also, since the grid is regular (equal X and Y spacing, and there are no skewed grid lines) we can establish the following formulas:
33550
tag:
ModelPixelScaleTag = [ cellsize , cellsize , 0 ]
33922
tag:
ModelTiepointTag = [ 0 , 0 , 0 , UpperLeftCorner_X , UpperLeftCorner_Y , 0]
where
UpperLeftCorner_X = xllcorner
UpperLeftCorner_Y = yllcorner + cellsize * nrows
That leaves the last tag 42113
. The geotiff format does not have a standard tag for a nodata value. GDAL stores band nodata value in the non standard TIFFTAG_GDAL_NODATA ASCII tag (code 42113).
Finally, as an example, we can write a function that relates the header of the grid (ncols, nrows, cellsize, xllcorner, yllcorner
) with the Tiff tags, using Libgeotiff C library:
void SetUpTIFFDirectory(TIFF *tif)
{
double tiepoints[6];
double pixscale[3];
double upperLeftCorner_X, upperLeftCorner_Y;
upperLeftCorner_X = xllcorner;
upperLeftCorner_Y = yllcorner + (cellsize*nrows);
tiepoint[0] = 0.0;
tiepoint[1] = 0.0;
tiepoint[2] = 0.0;
tiepoint[3] = upperLeftCorner_X;
tiepoint[4] = upperLeftCorner_Y;
tiepoint[5] = 0.0;
pixscale[0] = cellsize;
pixscale[1] = cellsize;
pixscale[2] = 0.0;
TIFFSetField(tif,TIFFTAG_IMAGEWIDTH, ncols);
TIFFSetField(tif,TIFFTAG_IMAGELENGTH, nrows);
TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE, 32);
TIFFSetField(tif,TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(tif,TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif,TIFFTAG_STRIPOFFSETS, 260L);
TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP, nrows;
TIFFSetField(tif,TIFFTAG_STRIPBYTECOUNTS, 80L);
TIFFSetField(tif,TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT, 3;
TIFFSetField(tif,GTIFF_TIEPOINTS, 6,tiepoints);
TIFFSetField(tif,GTIFF_PIXELSCALE, 3,pixscale);
}
Note: When you say you can't use GDAL, there's an In Memory Raster format that can probably be used as a temporary placeholder for you raster while you are adding mesh samples:
https://www.gdal.org/frmt_mem.html