[Geotiff] How do I copy all of the header information of a GeoTIFF
file?
Brad Colbert
bcolbert at rscusa.com
Sun Sep 10 13:39:01 EDT 2006
Hi Everyone,
I have a small program that uses specified GeoTIFFs as templates to
create new GeoTIFF files. The problem I'm having is that it appears
that I'm not properly copying all of the header information of the
template files. Specifically the datum and other CS parameters. Below
are cut-and-paste outputs of gdalinfo on, first, the template file and
second my newly created file:
Template:
Driver: GTiff/GeoTIFF
Size is 8050, 8050
Coordinate System is:
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.2572235630016,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]]
...
New File:
Driver: GTiff/GeoTIFF
Size is 8050, 8050
Coordinate System is:
GEOGCS["WGS 84",
DATUM["unknown",
SPHEROID["unnamed",8.749902587848476e-321,0,
AUTHORITY["EPSG","12048"]],
AUTHORITY["EPSG","24208"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]]
I guess my question is if anyone out there in GeoTIFF maillist land
knows of a more accurate way to do what I'm doing?
Thanks,
Brad
P.S.
For reference, I've included a snippet of my code that copies the header
info:
////////////////////////////////////////////////////////////////////////
//////
//
bool
GeoImage :: CopyImageInformation (TIFF *tiffin, GTIF* gtifin,
TIFF *tiffout, GTIF* gtifout)
{
// Get all of the information
int width, height;
float xres, yres;
short orientation;
// Set the attributes of this file
TIFFGetField(tiffin, TIFFTAG_IMAGEWIDTH, &width); //
get the width of the lImage
TIFFGetField(tiffin, TIFFTAG_IMAGELENGTH, &height); //
get the height of the lImage
TIFFGetField(tiffin, TIFFTAG_ORIENTATION, &orientation); //
get the origin of the lImage.
TIFFGetField(tiffin, TIFFTAG_XRESOLUTION, &xres); //
get the x resolution.
TIFFGetField(tiffin, TIFFTAG_YRESOLUTION, &yres); //
get the y resolution.
// Geospecific Tags
short tiepointsize, pixscalesize;
double* tiepoints;//[6];
double* pixscale;//[3];
TIFFGetField(tiffin, TIFFTAG_GEOTIEPOINTS, &tiepointsize,
&tiepoints);
TIFFGetField(tiffin, TIFFTAG_GEOPIXELSCALE, &pixscalesize,
&pixscale);
// Get the geospecific keys
// 6.2.1 GeoTIFF Configuration Keys
geocode_t modelTypeCode, rasterModelType;
GTIFKeyGet(gtifin, GTModelTypeGeoKey, &modelTypeCode, 0, 1); /* Model
Type Codes */
GTIFKeyGet(gtifin, GTRasterTypeGeoKey, &rasterModelType, 0, 1); /*
Raster Type Codes */
//6.2.2 Geographic CS Parameter Keys
geocode_t geoCDTypeCode, geodeticDatumCode,
primeMeridiumCode, linearUnitCode, linearUnitValue,
angularUnitCode, angularUnitValue, ellipsoideCode,
semiMajorAxis, semiMinorAxis, geoInvertFlattening,
angularUnitsCode, primeMeridianLongitude;
GTIFKeyGet(gtifin, GeographicTypeGeoKey, &geoCDTypeCode, 0, 1); /*
Geographic CS Type Codes */
GTIFKeyGet(gtifin, GeogGeodeticDatumGeoKey, &geodeticDatumCode, 0, 1);
/* Geodetic Datum Codes */
GTIFKeyGet(gtifin, GeogPrimeMeridianGeoKey, &primeMeridiumCode, 0, 1);
/* Prime Meridian Codes */
GTIFKeyGet(gtifin, GeogLinearUnitsGeoKey, &linearUnitCode, 0, 1); /*
Linear Units Codes */
GTIFKeyGet(gtifin, GeogLinearUnitSizeGeoKey, &linearUnitValue, 0, 1);
/* Linear Units (meters) */
GTIFKeyGet(gtifin, GeogAngularUnitsGeoKey, &angularUnitCode, 0, 1); /*
Angular Units Codes */
GTIFKeyGet(gtifin, GeogAngularUnitSizeGeoKey, &angularUnitValue, 0,
1); /* Angular Units (radians) */
GTIFKeyGet(gtifin, GeogEllipsoidGeoKey, &ellipsoideCode, 0, 1); /*
Ellipsoid Codes */
GTIFKeyGet(gtifin, GeogSemiMajorAxisGeoKey, &semiMajorAxis, 0, 1); /*
Geographic Semi Major Axis (GeogLinearUnits) */
GTIFKeyGet(gtifin, GeogSemiMinorAxisGeoKey, &semiMinorAxis, 0, 1); /*
Geographic Semi Minor Axis (GeogLinearUnits) */
GTIFKeyGet(gtifin, GeogInvFlatteningGeoKey, &geoInvertFlattening, 0,
1); /* Geographic Invert Flattening (ratio) */
GTIFKeyGet(gtifin, GeogAzimuthUnitsGeoKey, &angularUnitsCode, 0, 1);
/* Angular Units Codes */
GTIFKeyGet(gtifin, GeogPrimeMeridianLongGeoKey,
&primeMeridianLongitude, 0, 1); /* Prime Meridian Longitude
(GeogAngularUnit) */
//6.2.3 Projected CS Parameter Keys
geocode_t projCSSystemCode, ProjCode, projCoordTransfCode,
linearUnitsCode,
linearUnitSize,projStdParallel1,projStdParallel2,projNatOriginLong,
projNatOriginLat, projFalseEasting, projFalseNorthing,
projFalseOriginLong,
projFalseOriginLat,projFalseOriginEasting,projFalseOriginNorthing,
projCenterLong,projCenterLat,projCenterEasting,projCenterNorthing,
projScaleAtNatOrigin, projScaleAtCenter, projAzimuthAngle,
projStraightVertPoleLong;
GTIFKeyGet(gtifin, ProjectedCSTypeGeoKey, &projCSSystemCode, 0, 1); /*
Projected CS Type Codes */
GTIFKeyGet(gtifin, ProjectionGeoKey, &ProjCode, 0, 1); /* Projection
Code */
GTIFKeyGet(gtifin, ProjCoordTransGeoKey, &projCoordTransfCode, 0, 1);
/* Coordinate Transformation Codes */
GTIFKeyGet(gtifin, ProjLinearUnitsGeoKey, &linearUnitsCode, 0, 1); /*
Linear Units Codes */
GTIFKeyGet(gtifin, ProjLinearUnitSizeGeoKey, &linearUnitSize, 0, 1);
/* Linear Units Size (meters) */
GTIFKeyGet(gtifin, ProjStdParallel1GeoKey, &projStdParallel1, 0, 1);
/* ProjStdParallel1 (GeogAngularUnit) */
GTIFKeyGet(gtifin, ProjStdParallel2GeoKey, &projStdParallel2, 0, 1);
/* ProjStdParallel2 (GeogAngularUnit) */
GTIFKeyGet(gtifin, ProjNatOriginLongGeoKey, &projNatOriginLong, 0, 1);
/* ProjNatOriginLong (GeogAngularUnit) */
GTIFKeyGet(gtifin, ProjNatOriginLatGeoKey, &projNatOriginLat, 0, 1);
/* ProjNatOriginLat (GeogAngularUnit) */
GTIFKeyGet(gtifin, ProjFalseEastingGeoKey, &projFalseEasting, 0, 1);
/* ProjFalseEasting (ProjLinearUnits) */
GTIFKeyGet(gtifin, ProjFalseNorthingGeoKey, &projFalseNorthing, 0, 1);
/* ProjFalseNorthing (ProjLinearUnits) */
GTIFKeyGet(gtifin, ProjFalseOriginLongGeoKey, &projFalseOriginLong, 0,
1); /* ProjFalseOriginLong (GeogAngularUnit ) */
GTIFKeyGet(gtifin, ProjFalseOriginLatGeoKey, &projFalseOriginLat, 0,
1); /* ProjFalseOriginLat (GeogAngularUnit ) */
GTIFKeyGet(gtifin, ProjFalseOriginEastingGeoKey,
&projFalseOriginEasting, 0, 1); /* ProjFalseOriginEasting
(ProjLinearUnits ) */
GTIFKeyGet(gtifin, ProjFalseOriginNorthingGeoKey,
&projFalseOriginNorthing, 0, 1); /* ProjFalseOriginNorthing
(ProjLinearUnits ) */
GTIFKeyGet(gtifin, ProjCenterLongGeoKey, &projCenterLong, 0, 1); /*
ProjCenterLong (GeogAngularUnit ) */
GTIFKeyGet(gtifin, ProjCenterLatGeoKey, &projCenterLat, 0, 1); /*
ProjCenterLat (GeogAngularUnit ) */
GTIFKeyGet(gtifin, ProjCenterEastingGeoKey, &projCenterEasting, 0, 1);
/* ProjCenterEasting (ProjLinearUnits ) */
GTIFKeyGet(gtifin, ProjCenterNorthingGeoKey, &projCenterNorthing, 0,
1); /* ProjCenterNorthing (ProjLinearUnits ) */
GTIFKeyGet(gtifin, ProjScaleAtNatOriginGeoKey, &projScaleAtNatOrigin,
0, 1); /* ProjScaleAtNatOrigin (ratio) */
GTIFKeyGet(gtifin, ProjScaleAtCenterGeoKey, &projScaleAtCenter, 0, 1);
/* ProjScaleAtCenter (ratio ) */
GTIFKeyGet(gtifin, ProjAzimuthAngleGeoKey, &projAzimuthAngle, 0, 1);
/* ProjAzimuthAngle (GeogAzimuthUnit ) */
GTIFKeyGet(gtifin, ProjStraightVertPoleLongGeoKey,
&projStraightVertPoleLong, 0, 1); /* ProjStraightVertPoleLong
(GeogAzimuthUnit ) */
//6.2.4 Vertical CS Keys
geocode_t verticalCSType, verticalDatum, verticalUnits;
GTIFKeyGet(gtifin, VerticalCSTypeGeoKey, &verticalCSType, 0, 1); /*
Vertical CS Type Codes */
GTIFKeyGet(gtifin, VerticalDatumGeoKey, &verticalDatum, 0, 1); /*
Vertical CS Datum Codes */
GTIFKeyGet(gtifin, VerticalUnitsGeoKey, &verticalUnits, 0, 1); /*
Linear Units Codes */
// Write this objects attributes
//Allocate(width, height);
_width = width;
_height = height;
TIFFSetField(tiffout, TIFFTAG_IMAGEWIDTH, width); //
set the width of the lImage
TIFFSetField(tiffout, TIFFTAG_IMAGELENGTH, height); //
set the height of the lImage
switch (_writeMode)
{
case GeoImage::Float:
TIFFSetField(tiffout, TIFFTAG_SAMPLESPERPIXEL, 1); // set number
of channels per pixel
TIFFSetField(tiffout, TIFFTAG_BITSPERSAMPLE,
sizeof(Image::FLOAT_I_pixel)*8); // set the size of the channels
TIFFSetField(tiffout, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
break;
case GeoImage::UChar:
TIFFSetField(tiffout, TIFFTAG_SAMPLESPERPIXEL, 1); // set number
of channels per pixel
TIFFSetField(tiffout, TIFFTAG_BITSPERSAMPLE,
sizeof(Image::UCHAR_I_pixel)*8); // set the size of the channels
TIFFSetField(tiffout, TIFFTAG_ROWSPERSTRIP, height);
break;
};
TIFFSetField(tiffout, TIFFTAG_ORIENTATION, orientation);
// set the origin of the lImage.
TIFFSetField(tiffout, TIFFTAG_PLANARCONFIG, 1);
// Some other essential fields to set
TIFFSetField(tiffout, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
//TIFFSetField(tiffout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tiffout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tiffin, TIFFTAG_XRESOLUTION, xres); //
get the x resolution.
TIFFSetField(tiffin, TIFFTAG_YRESOLUTION, yres); //
get the y resolution.
// Geospecific Tags
Top(tiepoints[4]);
Left(tiepoints[3]);
Bottom(tiepoints[4]-(pixscale[1] * double(height)));
Right(tiepoints[3]+(pixscale[0] * double(width)));
TIFFSetField(tiffout, TIFFTAG_GEOTIEPOINTS, tiepointsize, tiepoints);
TIFFSetField(tiffout, TIFFTAG_GEOPIXELSCALE, pixscalesize,
pixscale);
// Set the geotiff attributes
GTIFKeySet(gtifout, GTModelTypeGeoKey, TYPE_SHORT, 1, modelTypeCode);
/* Model Type Codes */
GTIFKeySet(gtifout, GTRasterTypeGeoKey, TYPE_SHORT, 1,
rasterModelType); /* Raster Type Codes */
//6.2.2 Geographic CS Parameter Keys
GTIFKeySet(gtifout, GeographicTypeGeoKey, TYPE_SHORT, 1,
geoCDTypeCode); /* Geographic CS Type Codes */
GTIFKeySet(gtifout, GeogGeodeticDatumGeoKey, TYPE_SHORT, 1,
geodeticDatumCode); /* Geodetic Datum Codes */
GTIFKeySet(gtifout, GeogPrimeMeridianGeoKey, TYPE_SHORT, 1,
primeMeridiumCode); /* Prime Meridian Codes */
GTIFKeySet(gtifout, GeogLinearUnitsGeoKey, TYPE_SHORT, 1,
linearUnitCode); /* Linear Units Codes */
GTIFKeySet(gtifout, GeogLinearUnitSizeGeoKey, TYPE_SHORT, 1,
linearUnitValue); /* Linear Units (meters) */
GTIFKeySet(gtifout, GeogAngularUnitsGeoKey, TYPE_SHORT, 1,
angularUnitCode); /* Angular Units Codes */
GTIFKeySet(gtifout, GeogAngularUnitSizeGeoKey, TYPE_SHORT, 1,
angularUnitValue); /* Angular Units (radians) */
GTIFKeySet(gtifout, GeogEllipsoidGeoKey, TYPE_SHORT, 1,
ellipsoideCode); /* Ellipsoid Codes */
GTIFKeySet(gtifout, GeogSemiMajorAxisGeoKey, TYPE_SHORT, 1,
semiMajorAxis); /* Geographic Semi Major Axis (GeogLinearUnits) */
GTIFKeySet(gtifout, GeogSemiMinorAxisGeoKey, TYPE_SHORT, 1,
semiMinorAxis); /* Geographic Semi Minor Axis (GeogLinearUnits) */
GTIFKeySet(gtifout, GeogInvFlatteningGeoKey, TYPE_SHORT, 1,
geoInvertFlattening); /* Geographic Invert Flattening (ratio) */
GTIFKeySet(gtifout, GeogAzimuthUnitsGeoKey, TYPE_SHORT, 1,
angularUnitsCode); /* Angular Units Codes */
GTIFKeySet(gtifout, GeogPrimeMeridianLongGeoKey, TYPE_SHORT, 1,
primeMeridianLongitude); /* Prime Meridian Longitude (GeogAngularUnit)
*/
//6.2.3 Projected CS Parameter Keys
GTIFKeySet(gtifout, ProjectedCSTypeGeoKey, TYPE_SHORT, 1,
projCSSystemCode); /* Projected CS Type Codes */
GTIFKeySet(gtifout, ProjectionGeoKey, TYPE_SHORT, 1, ProjCode); /*
Projection Code */
GTIFKeySet(gtifout, ProjCoordTransGeoKey, TYPE_SHORT, 1,
projCoordTransfCode); /* Coordinate Transformation Codes */
GTIFKeySet(gtifout, ProjLinearUnitsGeoKey, TYPE_SHORT, 1,
linearUnitsCode); /* Linear Units Codes */
GTIFKeySet(gtifout, ProjLinearUnitSizeGeoKey, TYPE_SHORT, 1,
linearUnitSize); /* Linear Units Size (meters) */
GTIFKeySet(gtifout, ProjStdParallel1GeoKey, TYPE_SHORT, 1,
projStdParallel1); /* ProjStdParallel1 (GeogAngularUnit) */
GTIFKeySet(gtifout, ProjStdParallel2GeoKey, TYPE_SHORT, 1,
projStdParallel2); /* ProjStdParallel2 (GeogAngularUnit) */
GTIFKeySet(gtifout, ProjNatOriginLongGeoKey, TYPE_SHORT, 1,
projNatOriginLong); /* ProjNatOriginLong (GeogAngularUnit) */
GTIFKeySet(gtifout, ProjNatOriginLatGeoKey, TYPE_SHORT, 1,
projNatOriginLat); /* ProjNatOriginLat (GeogAngularUnit) */
GTIFKeySet(gtifout, ProjFalseEastingGeoKey, TYPE_SHORT, 1,
projFalseEasting); /* ProjFalseEasting (ProjLinearUnits) */
GTIFKeySet(gtifout, ProjFalseNorthingGeoKey, TYPE_SHORT, 1,
projFalseNorthing); /* ProjFalseNorthing (ProjLinearUnits) */
GTIFKeySet(gtifout, ProjFalseOriginLongGeoKey, TYPE_SHORT, 1,
projFalseOriginLong); /* ProjFalseOriginLong (GeogAngularUnit ) */
GTIFKeySet(gtifout, ProjFalseOriginLatGeoKey, TYPE_SHORT, 1,
projFalseOriginLat); /* ProjFalseOriginLat (GeogAngularUnit ) */
GTIFKeySet(gtifout, ProjFalseOriginEastingGeoKey, TYPE_SHORT, 1,
projFalseOriginEasting); /* ProjFalseOriginEasting (ProjLinearUnits ) */
GTIFKeySet(gtifout, ProjFalseOriginNorthingGeoKey, TYPE_SHORT, 1,
projFalseOriginNorthing); /* ProjFalseOriginNorthing (ProjLinearUnits )
*/
GTIFKeySet(gtifout, ProjCenterLongGeoKey, TYPE_SHORT, 1,
projCenterLong); /* ProjCenterLong (GeogAngularUnit ) */
GTIFKeySet(gtifout, ProjCenterLatGeoKey, TYPE_SHORT, 1,
projCenterLat); /* ProjCenterLat (GeogAngularUnit ) */
GTIFKeySet(gtifout, ProjCenterEastingGeoKey, TYPE_SHORT, 1,
projCenterEasting); /* ProjCenterEasting (ProjLinearUnits ) */
GTIFKeySet(gtifout, ProjCenterNorthingGeoKey, TYPE_SHORT, 1,
projCenterNorthing); /* ProjCenterNorthing (ProjLinearUnits ) */
GTIFKeySet(gtifout, ProjScaleAtNatOriginGeoKey, TYPE_SHORT, 1,
projScaleAtNatOrigin); /* ProjScaleAtNatOrigin (ratio) */
GTIFKeySet(gtifout, ProjScaleAtCenterGeoKey, TYPE_SHORT, 1,
projScaleAtCenter); /* ProjScaleAtCenter (ratio ) */
GTIFKeySet(gtifout, ProjAzimuthAngleGeoKey, TYPE_SHORT, 1,
projAzimuthAngle); /* ProjAzimuthAngle (GeogAzimuthUnit ) */
GTIFKeySet(gtifout, ProjStraightVertPoleLongGeoKey, TYPE_SHORT, 1,
projStraightVertPoleLong); /* ProjStraightVertPoleLong (GeogAzimuthUnit
) */
//6.2.4 Vertical CS Keys
GTIFKeySet(gtifout, VerticalCSTypeGeoKey, TYPE_SHORT, 1,
verticalCSType); /* Vertical CS Type Codes */
GTIFKeySet(gtifout, VerticalDatumGeoKey, TYPE_SHORT, 1,
verticalDatum); /* Vertical CS Datum Codes */
GTIFKeySet(gtifout, VerticalUnitsGeoKey, TYPE_SHORT, 1,
verticalUnits); /* Linear Units Codes */
//
GTIFKeySet(gtifout, GTCitationGeoKey, TYPE_ASCII, 0,
"Light Irradiance Map");
GTIFKeySet(gtifout, GeogCitationGeoKey, TYPE_ASCII, 0,
"WGS84 Ellipsoid Used.");
return true;
}
---
Renaissance Sciences Corporation
O/M: 480 290-3997
F: 425 675-8044
More information about the Geotiff
mailing list