void set_geotif_header(char *tiff_fname, char *utm_zone, float xoff, float yoff, float scale) { // open tiff file TIFF *tif = XTIFFOpen(tiff_fname, "r+"); if (!tif) fail("failed in XTIFFOpen\n"); GTIF *gtif = GTIFNew(tif); if (!gtif) fail("failed in GTIFNew\n"); // write TIFF tags double pixsize[3] = {scale, scale, 0.0}; TIFFSetField(tif, GTIFF_PIXELSCALE, 3, pixsize); double tiepoint[6] = {0.0, 0.0, 0.0, xoff, yoff, 0.0}; TIFFSetField(tif, GTIFF_TIEPOINTS, 6, tiepoint); // write GEOTIFF keys int utm_ind = get_utm_zone_index_for_geotiff(utm_zone); GTIFKeySet(gtif, ProjectedCSTypeGeoKey, TYPE_SHORT, 1, utm_ind); GTIFWriteKeys(gtif); // free and close GTIFFree(gtif); XTIFFClose(tif); }
int main() { char *fname = "newgeo.tif"; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ tif=XTIFFOpen(fname,"w"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { printf("failed in GTIFNew\n"); goto failure; } SetUpTIFFDirectory(tif); SetUpGeoKeys(gtif); WriteImage(tif); GTIFWriteKeys(gtif); GTIFFree(gtif); XTIFFClose(tif); return 0; failure: printf("failure in makegeo\n"); if (tif) TIFFClose(tif); if (gtif) GTIFFree(gtif); return -1; }
static void CopyGeoTIFF(TIFF * in, TIFF *out) { GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ double *d_list = NULL; int16 d_list_count; /* read definition from source file. */ gtif = GTIFNew(in); if (!gtif) return; if (TIFFGetField(in, GTIFF_TIEPOINTS, &d_list_count, &d_list)) TIFFSetField(out, GTIFF_TIEPOINTS, d_list_count, d_list); if (TIFFGetField(in, GTIFF_PIXELSCALE, &d_list_count, &d_list)) TIFFSetField(out, GTIFF_PIXELSCALE, d_list_count, d_list); if (TIFFGetField(in, GTIFF_TRANSMATRIX, &d_list_count, &d_list)) TIFFSetField(out, GTIFF_TRANSMATRIX, d_list_count, d_list); /* Here we violate the GTIF abstraction to retarget on another file. We should just have a function for copying tags from one GTIF object to another. */ gtif->gt_tif = out; gtif->gt_flags |= FLAG_FILE_MODIFIED; /* Install keys and tags */ GTIFWriteKeys(gtif); GTIFFree(gtif); return; }
static void InstallGeoTIFF(TIFF *out) { GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ FILE *fd; gtif = GTIFNew(out); if (!gtif) { printf("failed in GTIFNew\n"); return; } if( geofile ) { /* Install keys and tags */ fd = fopen(geofile,"r"); if( fd == NULL ) { perror( geofile ); exit( -1 ); } if (!GTIFImport(gtif,0,fd)) { fprintf(stderr,"Failure in GTIFImport\n"); exit (-1); } fclose(fd); } else if( proj4_string ) { if( !GTIFSetFromProj4(gtif,proj4_string) ) { fprintf(stderr,"Failure in GTIFSetFromProj4\n"); exit (-1); } } GTIFWriteKeys(gtif); GTIFFree(gtif); return; }
bool TiffDetails::addGeoKeys(TIFF* pOut, int width, int height, const SessionItem *pItem) { if ((pOut == NULL) || (width == 0) || (height == 0)) { return false; } const View* pInputView = dynamic_cast<const View*>(pItem); if (pInputView == NULL) { return false; } RasterElement* pGeoreferencedRaster = NULL; // First raster element we find with georeferencing information const ProductView* pView = dynamic_cast<const ProductView*>(pInputView); if (pView != NULL) { AnnotationLayer* pAnno = pView->getLayoutLayer(); if (pAnno == NULL) { return false; } /* NOTE: If we find more than one SpatialDataView with a georeferenced RasterElement, we will only provide geo-data for the FIRST one - because two views could theoretically screw up the geo-data if one is in Australia and the other in Canada. */ // get all the view objects std::list<GraphicObject*> objs; pAnno->getObjects(VIEW_OBJECT, objs); // for every object, find the data set with a geocoord matrix for (std::list<GraphicObject*>::iterator it = objs.begin(); it != objs.end(); ++it) { GraphicObject* pObj = *it; if (pObj != NULL) { SpatialDataView* pSpView = dynamic_cast<SpatialDataView*>(pObj->getObjectView()); if (pSpView != NULL) { LayerList* pLayerList = pSpView->getLayerList(); if (pLayerList != NULL) { RasterElement* pRaster = pLayerList->getPrimaryRasterElement(); if (pRaster != NULL && pRaster->isGeoreferenced()) { pGeoreferencedRaster = pRaster; break; } } } } } } const SpatialDataView* pSpView = dynamic_cast<const SpatialDataView*>(pInputView); if (pSpView != NULL) { LayerList* pLayerList = pSpView->getLayerList(); if (pLayerList != NULL) { RasterElement* pRaster = pLayerList->getPrimaryRasterElement(); if (pRaster != NULL && pRaster->isGeoreferenced()) { pGeoreferencedRaster = pRaster; } } } if (pGeoreferencedRaster == NULL) { return false; } GTIF* pGtif = GTIFNew(pOut); if (pGtif == NULL) { return false; } LocationType lowerLeft; LocationType upperLeft; LocationType upperRight; LocationType lowerRight; pInputView->getVisibleCorners(lowerLeft, upperLeft, upperRight, lowerRight); LocationType latLong; //get the lat/long's (0,0) latLong = pGeoreferencedRaster->convertPixelToGeocoord(upperLeft); double ll1y = latLong.mY; //delta long double ll1x = latLong.mX; //delta lat latLong = pGeoreferencedRaster->convertPixelToGeocoord(upperRight); double ll2y = latLong.mY; //long double ll2x = latLong.mX; //lat latLong = pGeoreferencedRaster->convertPixelToGeocoord(lowerLeft); double ll3y = latLong.mY; //long double ll3x = latLong.mX; //lat //compute transformation Matrix values //added slight modification, must divide by magnitude double a = (ll2y - ll1y) / width; double b = (ll3y - ll1y) / height; double d = (ll1y); double e = (ll2x - ll1x) / width; double f = (ll3x - ll1x) / height; double h = (ll1x); double tMatrix[16] = { a, b, 0.0, d, e, f, 0.0, h, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; TIFFSetField(pOut, GTIFF_TRANSMATRIX, 16, tMatrix); GTIFKeySet(pGtif, GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea); GTIFKeySet(pGtif, GTModelTypeGeoKey, TYPE_SHORT, 1, ModelGeographic); GTIFKeySet(pGtif, GeographicTypeGeoKey, TYPE_SHORT, 1, GCS_WGS_84); // Here we violate the GTIF abstraction to retarget on another file. // We should just have a function for copying tags from one GTIF object // to another. pGtif->gt_tif = pOut; pGtif->gt_flags |= FLAG_FILE_MODIFIED; // Install keys and tags GTIFWriteKeys(pGtif); GTIFFree(pGtif); return true; }
static int InstallGeoTIFF(const char *geofile, const char *tiffile) { TIFF *tif = (TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ FILE *fp; uint16 *panVI = NULL; uint16 nKeyCount; tif = XTIFFOpen(tiffile, "r+"); if (!tif) { perror(tiffile); fprintf(stderr, "Cannot open TIFF file %s (does not exist or not a valid TIFF file)\n", tiffile); return(-1); } /* If we have existing geokeys, try to wipe them by writing a dummy geokey directory. (#2546) */ if( TIFFGetField( tif, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI ) ) { uint16 anGKVersionInfo[4] = { 1, 1, 0, 0 }; double adfDummyDoubleParams[1] = { 0.0 }; TIFFSetField( tif, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo ); TIFFSetField( tif, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams ); TIFFSetField( tif, TIFFTAG_GEOASCIIPARAMS, "" ); } gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr, "Internal error (GTIFNew)\n"); return(-2); } /* Read GeoTIFF projection information from geofile */ fp = fopen(geofile, "r"); if( fp == NULL ) { perror( geofile ); fprintf(stderr, "Cannot open projection definition file %s\n", geofile); return(-3); } if (!GTIFImport(gtif, 0, fp)) { fprintf(stderr,"Projection definition file is not valid (%s)\n", geofile); return(-4); } fclose(fp); /* Install GeoTIFF keys into the TIFF file */ GTIFWriteKeys(gtif); /* Clean up */ GTIFFree(gtif); TIFFRewriteDirectory(tif); XTIFFClose(tif); return(0); }