Search code examples
meshgdalgeotifflibtifflibtiff.net

What are the corresponding fields to set in libtiff or libgeotiff, given a minimal ESRI ASCII file?


I have an ESRI ASCII file of the form:

ncols         5 
nrows         4 
xllcorner     0 
yllcorner     0 
cellsize      10 
NODATA_value  -9999 
25.4 26.1 27 28.6 27.7
25 26 26.4 27.9 27.4
25.1 25.8 26.8 28.6 27.6
27.5 28 27.7 30.6 28.3

And I need to use libtiff.net ( or its C++ equivalent, libtiff, or libgeotiff, or GDAL, or any other C# or C++ libraries) to convert it to a geotiff file.

But I simply don't know what are the fields to set, there are a lot of fields such as TIFFTAG_IMAGEWIDTH, TIFFTAG_SAMPLESPERPIXEL or TIFFTAG_BITSPERSAMPLE which I don't know whether they are relevant or not.

Given an ESRI ASCII file as per above, how to use the libtiff.net or libtiff library to create a geotiff file?

Why am I doing this?

In my application, I have a mesh, and I would have to convert the mesh to a raster file. I want to create such a geotiff raster file directly from the mesh sampling, without creating an intermediate ASCII file first because the intermediate file size is very big compared to the final geotiff output file.

The only approach I can think of is to directly manipulate the geotiff file using libtiff.net while I am obtaining the grid points on the mesh.

How to do this, or is there a better approach?


Solution

  • Actually this task can be easily done by using GDAL library ( or its .Net equivalent, GDAL.Net). There is even an example here in Python:

    ncols         174
    nrows         115
    xllcorner     14.97
    yllcorner     -34.54
    cellsize      0.11
    

    And the Python script:

    if __name__ == '__main__':
    # Import libs
    import numpy, os
    from osgeo import osr, gdal
    
    # Set file vars
    output_file = "out.tif"
    
    # Create gtif
    driver = gdal.GetDriverByName("GTiff")
    dst_ds = driver.Create(output_file, 174, 115, 1, gdal.GDT_Byte )
    raster = numpy.zeros( (174, 115) )
    
    # top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution
    dst_ds.SetGeoTransform( [ 14.97, 0.11, 0, -34.54, 0, 0.11 ] )
    
    # set the reference info 
    srs = osr.SpatialReference()
    srs.SetWellKnownGeogCS("WGS84")
    dst_ds.SetProjection( srs.ExportToWkt() )
    
    # write the band
    dst_ds.GetRasterBand(1).WriteArray(raster)
    

    Converting the code to .Net should be trivial enough.