1

Currently I am writing a crop function for georeferenced TIFF files. I use Free.Ports.LibGeoTiff which seems to work quite well to read the TIFF, also the geo references.

It is also possible to write a copy of the TIFF file to a new file, but I can't find a solution to write the geo references as well. The copy is a non-referenced TIFF.

I use the following code to read the geokeys according to the rare example codes and write them back in the copy file:

foreach (Free.Ports.LibGeoTiff.geokey_t geotag in Enum.GetValues(typeof(Free.Ports.LibGeoTiff.geokey_t)))
    {
        try
        {    
            if (Free.Ports.LibGeoTiff.libgeotiff.GTIFKeyInfo(geotiff, geotag, out Free.Ports.LibGeoTiff.tagtype_t tagtype) > 0)
            {    
                switch (tagtype)
                {
                    case Free.Ports.LibGeoTiff.tagtype_t.TYPE_ASCII:
                        if (Free.Ports.LibGeoTiff.libgeotiff.GTIFKeyGet(geotiff, geotag, out string geovaluestr) > 0) { Free.Ports.LibGeoTiff.libgeotiff.GTIFKeySet(geoouttiff, geotag, geovaluestr); }
                        break;
                    case Free.Ports.LibGeoTiff.tagtype_t.TYPE_DOUBLE:
                        if (Free.Ports.LibGeoTiff.libgeotiff.GTIFKeyGet(geotiff, geotag, out double geovaluedbl, 0) > 0) { Free.Ports.LibGeoTiff.libgeotiff.GTIFKeySet(geoouttiff, geotag, geovaluedbl); }
                        break;
                    case Free.Ports.LibGeoTiff.tagtype_t.TYPE_SHORT:
                        if (Free.Ports.LibGeoTiff.libgeotiff.GTIFKeyGet(geotiff, geotag, out ushort geovaluesht, 0) > 0) { Free.Ports.LibGeoTiff.libgeotiff.GTIFKeySet(geoouttiff, geotag, geovaluesht); }
                        break;
                }    
            }    
        }
        catch (System.Exception)
        {
            Console.WriteLine(geotag.ToString());
        }
    }

    bool saved = Free.Ports.LibGeoTiff.libgeotiff.GTIFWriteKeys(geoouttiff);

Debugging tells me the GTIFKeyGet() will read the data. Also, after setting the values I need to write the values into the file with the function GTIFWriteKeys() which returns true, but it does not actually do that.

Does anyone have a brilliant thought about this (I prefer this library and not look at GDAL)?

New contributor
Anton Huizinga is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

Anton Huizinga is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.