00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "geotiffio.h"
00015 #include "geo_tiffp.h"
00016 #include "geo_keyp.h"
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
00043 if (!SortKeys(gt,sortkeys))
00044 {
00045
00046 }
00047
00048
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
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
00064 (gt->gt_methods.set)(gt->gt_tif, GTIFF_GEOKEYDIRECTORY, gt->gt_nshorts, gt->gt_short );
00065
00066
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';
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
00082
00083
00084
00085
00086
00087
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;
00124 }
00125
00126 return 1;
00127 }
00128
00129
00130
00131
00132
00133
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