Main Page   Compound List   File List   Compound Members   File Members   Related Pages  

geo_write.c

00001 /**********************************************************************
00002  *
00003  *  geo_write.c  -- Public routines for GEOTIFF GeoKey access.
00004  *
00005  *    Written By: Niles D. Ritter.
00006  *
00007  *  copyright (c) 1995   Niles D. Ritter
00008  *
00009  *  Permission granted to use this software, so long as this copyright
00010  *  notice accompanies any source code derived therefrom.
00011  *
00012  **********************************************************************/
00013 
00014 #include "geotiffio.h"   /* public interface        */
00015 #include "geo_tiffp.h" /* external TIFF interface */
00016 #include "geo_keyp.h"  /* private interface       */
00017 
00018 static int WriteKey(GTIF* gt, KeyEntry* entptr,GeoKey* keyptr);
00019 static int SortKeys(GTIF* gt,int *sortkeys);
00020 
00032 int GTIFWriteKeys(GTIF *gt)
00033 {
00034         int i;
00035         GeoKey *keyptr;
00036         KeyEntry *entptr;
00037         KeyHeader *header;
00038         int sortkeys[MAX_KEYS];
00039         
00040         if (!(gt->gt_flags & FLAG_FILE_MODIFIED)) return 1;
00041         
00042         /*  Sort the Keys into numerical order */
00043         if (!SortKeys(gt,sortkeys))
00044         {
00045                 /* XXX error: a key was not recognized */
00046         }
00047         
00048         /* Set up header of ProjectionInfo tag */
00049         header = (KeyHeader *)gt->gt_short;
00050         header->hdr_num_keys = gt->gt_num_keys;
00051         header->hdr_version  = GvCurrentVersion;
00052         header->hdr_rev_major  = GvCurrentRevision;
00053         header->hdr_rev_minor  = GvCurrentMinorRev;
00054         
00055         /* Set up the rest of SHORT array properly */
00056         keyptr = gt->gt_keys;
00057         entptr = (KeyEntry*)(gt->gt_short + 4);
00058         for (i=0; i< gt->gt_num_keys; i++,entptr++)
00059         {
00060                 if (!WriteKey(gt,entptr,keyptr+sortkeys[i])) return 0;
00061         }       
00062         
00063         /* Write out the Key Directory */
00064         (gt->gt_methods.set)(gt->gt_tif, GTIFF_GEOKEYDIRECTORY, gt->gt_nshorts, gt->gt_short ); 
00065         
00066         /* Write out the params directories */
00067         if (gt->gt_ndoubles)
00068           (gt->gt_methods.set)(gt->gt_tif, GTIFF_DOUBLEPARAMS, gt->gt_ndoubles, gt->gt_double );
00069         if (gt->gt_nascii)
00070         {
00071           gt->gt_ascii[gt->gt_nascii] = '\0'; /* just to be safe */
00072           (gt->gt_methods.set)(gt->gt_tif, GTIFF_ASCIIPARAMS, 0, gt->gt_ascii );
00073         }
00074         
00075         gt->gt_flags &= ~FLAG_FILE_MODIFIED;
00076         return 1;
00077 }
00078 
00079 /**********************************************************************
00080  *
00081  *                        Private Routines
00082  *
00083  **********************************************************************/
00084  
00085 /*
00086  * Given GeoKey, write out the KeyEntry entries, returning 0 if failure.
00087  *  This is the exact complement of ReadKey().
00088  */
00089 
00090 static int WriteKey(GTIF* gt, KeyEntry* entptr,GeoKey* keyptr)
00091 {
00092         int count;
00093         
00094         entptr->ent_key = keyptr->gk_key;
00095         entptr->ent_count = keyptr->gk_count;
00096         count = entptr->ent_count;
00097         
00098         if (count==1 && keyptr->gk_type==TYPE_SHORT)
00099         {
00100                 entptr->ent_location = GTIFF_LOCAL;
00101                 entptr->ent_val_offset = *(pinfo_t*)&keyptr->gk_data;
00102                 return 1;
00103         }
00104                   
00105         switch (keyptr->gk_type)
00106         {
00107                 case TYPE_SHORT:
00108                         entptr->ent_location = GTIFF_GEOKEYDIRECTORY;
00109                         entptr->ent_val_offset = 
00110                            (pinfo_t*)keyptr->gk_data - gt->gt_short;
00111                         break;
00112                 case TYPE_DOUBLE:
00113                         entptr->ent_location = GTIFF_DOUBLEPARAMS;
00114                         entptr->ent_val_offset = 
00115                            (double*)keyptr->gk_data - gt->gt_double;
00116                         break;
00117                 case TYPE_ASCII:
00118                         entptr->ent_location = GTIFF_ASCIIPARAMS;
00119                         entptr->ent_val_offset = 
00120                            (char*)keyptr->gk_data - gt->gt_ascii;
00121                         break;
00122                 default:
00123                         return 0; /* failure */
00124         }
00125         
00126         return 1; /* success */
00127 }
00128 
00129 
00130 /* 
00131  * Numerically sort the GeoKeys.
00132  * We just do a linear search through
00133  * the list and pull out the keys that were set.
00134  */
00135 
00136 static int SortKeys(GTIF* gt,int *sortkeys)
00137 {
00138     int loc;
00139     int nkeys=0;
00140     geokey_t key,kmin,kmax;
00141     int *index = gt->gt_keyindex;
00142         
00143     kmin = (geokey_t) gt->gt_keymin;
00144     kmax = (geokey_t) gt->gt_keymax;
00145     for (key=kmin; key<=kmax; key++)
00146     {
00147         if ( (loc=index[key]) != 0 )
00148         {
00149             sortkeys[nkeys] = loc;
00150             nkeys++;
00151         }
00152     }
00153         
00154     return nkeys==gt->gt_num_keys;
00155 }
00156 

Generated at Sun Mar 4 23:32:44 2001 for libgeotiff by doxygen1.2.3-20001105 written by Dimitri van Heesch, © 1997-2000