Copy projection between GeoTIFFs

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Copy projection between GeoTIFFs

jeskynar


_________________________________________________________________
Get 5 GB of storage with Windows Live Hotmail.
http://windowslive.com/Explore/Hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_5gb_112008
_______________________________________________
Geotiff mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/geotiff
Reply | Threaded
Open this post in threaded view
|

RE: Copy projection between GeoTIFFs

jeskynar

[looks like hotmail threw away my message body!]
 
Is there a way of copying a projection from one GeoTIFF to another without completely rewriting the latter?
 
There seems to be a "-a (append)" option to newer versions of geotifcp but it is not documented and didn't do what I expected.
 
--
 
_________________________________________________________________
Get 5 GB of storage with Windows Live Hotmail.
http://windowslive.com/Explore/Hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_5gb_112008
_______________________________________________
Geotiff mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/geotiff
Reply | Threaded
Open this post in threaded view
|

Re: RE: Copy projection between GeoTIFFs

Frank Warmerdam
jeskynar wrote:
> [looks like hotmail threw away my message body!]
>
> Is there a way of copying a projection from one GeoTIFF to another without
> completely rewriting the latter?
>
> There seems to be a "-a (append)" option to newer versions of geotifcp but
> it is not documented and didn't do what I expected.

Jeskynar,

I believe the -a appends a new image to the existing TIFF file.

The listgeo and geotifcp cannot be used to reset the geotiff information
"in place" as things stand though this can be accomplished using the API
with some care.  GDAL does this for instance.

It would be relatively easy to modify geotifcp to have a "reset geo tags
from metadata file, but do nothing else" option or perhaps write a small
new utility called "applygeo".  However, I'm not likely to do so.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
Geotiff mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/geotiff
Reply | Threaded
Open this post in threaded view
|

RE: Copy projection between GeoTIFFs

jeskynar

[hidden email] wrote:
> The listgeo and geotifcp cannot be used to reset the geotiff information
> "in place" as things stand though this can be accomplished using the API
> with some care. GDAL does this for instance.
 
Do you mean there is a GDAL utility or something in the API?
 
> It would be relatively easy to modify geotifcp to have a "reset geo tags
> from metadata file, but do nothing else" option or perhaps write a small
> new utility called "applygeo". However, I'm not likely to do so.
 
Below is my attempt an applygeo.c, but I haven't used the libgeotiff API
"with some care" because it doesn't appear to do anything!  And if I add
a call to TIFFRewriteDirectory before XTIFFClose it crashes (assertion
tif_flags&0x80000 failed). Any ideas?
 
 
/* applygeo.c */
#include
#include
#include "geotiff.h"
#include "xtiffio.h"
 
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;
 
 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);
 }

 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);
}

int
main(int argc, char *argv[])
{
 char *usage = "usage: %s file.geo file.tiff\n"
  "geo\tfile containing projection (eg. from listgeo)\n"
  "tiff\tTIFF file into which the projection is written\n";
 char *prog;
 char *geofile;
 char *tiffile;
 int rc;
 
 prog = argv[0];
 geofile = argv[1];
 tiffile = argv[2];
 
 if (!geofile || !tiffile)
 {
  fprintf(stderr, usage, prog);
  exit(1);
 }
 
 rc = InstallGeoTIFF(geofile, tiffile);
 if (rc)
 {
  fprintf(stderr, "%s: error %d applying projection from %s into TIFF %s\n", prog, rc, geofile, tiffile);
  exit(2);
 }
 
 return(0);
}
_________________________________________________________________
Windows Live Hotmail now works up to 70% faster.
http://windowslive.com/Explore/Hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_faster_112008
_______________________________________________
Geotiff mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/geotiff
Reply | Threaded
Open this post in threaded view
|

Re: Copy projection between GeoTIFFs

Frank Warmerdam
jeskynar wrote:

> [hidden email] wrote:
>> The listgeo and geotifcp cannot be used to reset the geotiff information
>> "in place" as things stand though this can be accomplished using the API
>> with some care. GDAL does this for instance.
>  
> Do you mean there is a GDAL utility or something in the API?
>  
>> It would be relatively easy to modify geotifcp to have a "reset geo tags
>> from metadata file, but do nothing else" option or perhaps write a small
>> new utility called "applygeo". However, I'm not likely to do so.
>  
> Below is my attempt an applygeo.c, but I haven't used the libgeotiff API
> "with some care" because it doesn't appear to do anything!  And if I add
> a call to TIFFRewriteDirectory before XTIFFClose it crashes (assertion
> tif_flags&0x80000 failed). Any ideas?
Jeskynar,

I added the TIFFRewriteDirectory() and it worked fine for me, but I'm using
libtiff4 (latest from cvs).  What version of libtiff are you using?

I also found I had to make an effort to clear the existing tags if I wanted
to be able to replace existing geotiff tags properly (clearing existing tags
that aren't in the new .geo file).  I have attached my modified version.

If you give me permission to distribute your code under the normal libtiff
license, I'll add applygeo in subversion.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent


/* applygeo.c */
#include <stdlib.h>
#include "geotiff.h"
#include "xtiffio.h"
 
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;
 
    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 goekey directory. (#2546)
    uint16 *panVI = NULL;
    uint16 nKeyCount;

    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);
}

int
main(int argc, char *argv[])
{
    char *usage = "usage: %s file.geo file.tiff\n"
        "geo\tfile containing projection (eg. from listgeo)\n"
        "tiff\tTIFF file into which the projection is written\n";
    char *prog;
    char *geofile;
    char *tiffile;
    int rc;
 
    prog = argv[0];
    geofile = argv[1];
    tiffile = argv[2];
 
    if (!geofile || !tiffile)
    {
        fprintf(stderr, usage, prog);
        exit(1);
    }
 
    rc = InstallGeoTIFF(geofile, tiffile);
    if (rc)
    {
        fprintf(stderr, "%s: error %d applying projection from %s into TIFF %s\n", prog, rc, geofile, tiffile);
        exit(2);
    }
 
    return(0);
}

_______________________________________________
Geotiff mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/geotiff