Search code examples
c#geospatialtiffgeogeotiff

Read pixel values from GeoTIFF in C#


I am trying to read the pixel values from a GeoTiff in C#. The pixels represent elevation data. The GeoTiff is:

  • greyscaled
  • contains float32 values
  • non-tiled.

In Python this does exactly what i want:

tiffArray = numpy.array(tiffImage)
elevation = tiffArray[0,0] # returns: 39.66m

I tried a lot of solutions, mainly using the libTif library, which looks promising. But most expect tiled GeoTIFFs or fail in ways i do not understand (I dont know much about this low-level byte/binary/decoding stuff...). The GDAL library is apparently difficult to integrate into unity, so it is not a good alternative.

The sample GeoTIFF (just 2x2) can be downloaded here: https://isk.geobasis-bb.de/ows/dgm_wcs?VERSION=2.0.1&SERVICE=wcs&REQUEST=GetCoverage&COVERAGEID=bb_dgm&FORMAT=image/tiff&SUBSET=x(389381,389384)&SUBSET=y(5820682,5820685)&SCALEFACTOR=1

Any help is appreciated, thanks in advance!!


Solution

  • I eventually found a library that exactly does the job.

    Not easy to find, so here is a link: https://www.nuget.org/packages/GeoTiffElevationReader

    Use it like this:

    using GeoTiffElevationReader;
    
    string filePath = "..../YourHeightMap.tif";
    GeoTiff elevationTiff = new GeoTiff(filePath);
    float elevation = elevationTiff.GetElevationAtLatLon((int)latitude, (int)longitude);
    

    Gives out the elevation in meters (in this case).

    It even takes the coordinates of the CRS of the GeoTIFF directly.