简体   繁体   中英

How to write coordinate to a TIF file using libgeotiff

I'm trying to create a tif file using the GeoTiff library. I managed to write all the data into a normal tif file using the standard TIF function of the library, but I can't figure out how to use the functions to add the coordinates of the corners.

I've looked for example on the internet and got to the point of being able to add information relative to the geolocalization of the image : Model, Raster, Angular unit, etc. But I can't see any keys to add the corners information that I must get when dumping tag info with "listgeo" : Corners Coordinate

...
GTIFKeySet(gtif, GeogCitationGeoKey, TYPE_ASCII, 7, "WGS 84");
GTIFKeySet(gtif, GeogAngularUnitsGeoKey, TYPE_SHORT, 1, 9102);
GTIFKeySet(gtif, GeogSemiMajorAxisGeoKey, TYPE_DOUBLE, 1, 6378137.0);
GTIFKeySet(gtif, GeogInvFlatteningGeoKey, TYPE_DOUBLE, 1, 298.257223563);
...

Could someone indicate where I can find documentation on how to write those coordinate, or which key/function I need to use to do that, if it's possible ?

Cheers.

This would probably be best suited as comment but due to it's size I'll post it as an answer. My GIS knowledge is somewhat rusted but let's take a look at how listgeo dumps the corner information:

static void GTIFPrintCorners( GTIF *gtif, GTIFDefn *defn, FILE * fp_out,
                              int xsize, int ysize, int inv_flag, int dec_flag )

{
    printf( "\nCorner Coordinates:\n" );
    if( !GTIFReportACorner( gtif, defn, fp_out,
                            "Upper Left", 0.0, 0.0, inv_flag, dec_flag ) )
    {
        printf( " ... unable to transform points between pixel/line and PCS space\n" );
        return;
    }

    // Ommited ...
}

Looking at GTIFReportACorner it seems it converts image coordinates (0, 0) to it's corresponding geographic coordinates using GTIFImageToPCS instead of looking directly at some tag.

My bet is that GTIFImageToPCS will do it's work fine as long as you add other information such as projection, datum and probably the location of the image center (tie points?).

Include :

#include <tiffio.h>
#include <geotiff/xtiffio.h>
#include <geotiff/geotiffio.h>

Using

struct TiePoint{
    double rasterX;
    double rasterY;
    double longitude;
    double latitude;
};

and

std::vector<TiePoint> tiePoints;

Open the file with:

TIFF* tiff = XTIFFOpen("filename.tiff", "w");
GTIF* geotiff = GTIFNew( tiff);

And write the points like this:

int length = 6*tiePoints.size();
double data33922[length];
for(int index=0; index<tiePoints.size(); index++){
    data33922[index*6 + 0] = tiePoints.at(index).rasterX;
    data33922[index*6 + 1] = tiePoints.at(index).rasterY;
    data33922[index*6 + 2] = 0;
    data33922[index*6 + 3] = tiePoints.at(index).longitude;
    data33922[index*6 + 4] = tiePoints.at(index).latitude;
    data33922[index*6 + 5] = 0;
}
TIFFSetField(tiff,TIFFTAG_GEOTIEPOINTS,length,data33922);

Finish up with:

GTIFWriteKeys(gtif);
GTIFFree(gtif);
TIFFClose(file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM