-
Notifications
You must be signed in to change notification settings - Fork 2
/
geo_set.c
232 lines (203 loc) · 7.15 KB
/
geo_set.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**********************************************************************
*
* geo_set.c -- Public routines for GEOTIFF GeoKey access.
*
* Written By: Niles D. Ritter.
*
* copyright (c) 1995 Niles D. Ritter
*
* Permission granted to use this software, so long as this copyright
* notice accompanies any products derived therefrom.
*
**********************************************************************/
#include "geotiff.h" /* public interface */
#include "geo_tiffp.h" /* external TIFF interface */
#include "geo_keyp.h" /* private interface */
#include <assert.h>
/**
This function writes a geokey_t value to a GeoTIFF file.
@param gtif The geotiff information handle from GTIFNew().
@param keyID The geokey_t name (such as ProjectedCSTypeGeoKey).
This must come from the list of legal geokey_t values
(an enumeration) listed below.
@param val The <b>val</b> argument is a pointer to the
variable into which the value should be read. The type of the variable
varies depending on the geokey_t given. While there is no ready mapping
of geokey_t values onto types, in general code values are of type <i>short</i>,
citations are strings, and everything else is of type <i>double</i>. Note
that pointer's to <i>int</i> should never be passed to GTIFKeyGet() for
integer values as they will be shorts, and the int's may not be properly
initialized (and will be grossly wrong on MSB systems).
@param index Indicates how far into the list of values
for this geokey to offset. Should normally be zero.
@param count Indicates how many values
to read. At this time all keys except for strings have only one value,
so <b>index</b> should be zero, and <b>count</b> should be one.<p>
The <b>key</b> indicates the key name to be written to the
file and should from the geokey_t enumeration
(eg. <tt>ProjectedCSTypeGeoKey</tt>). The full list of possible geokey_t
values can be found in geokeys.inc, or in the online documentation for
GTIFKeyGet().<p>
The <b>type</b> should be one of TYPE_SHORT, TYPE_ASCII, or TYPE_DOUBLE and
will indicate the type of value being passed at the end of the argument
list (the key value). The <b>count</b> should be one except for strings
when it should be the length of the string (or zero to for this to be
computed internally). As a special case a <b>count</b> of -1 can be
used to request an existing key be deleted, in which no value is passed.<p>
The actual value is passed at the end of the argument list, and should be
a short, a double, or a char * value. Note that short and double values
are passed by value rather than as pointers when count is 1, but as pointers
if count is larger than 1.<p>
Note that key values aren't actually flushed to the file until
GTIFWriteKeys() is called. Till then
the new values are just kept with the GTIF structure.<p>
<b>Example:</b><p>
<pre>
GTIFKeySet(gtif, GTRasterTypeGeoKey, TYPE_SHORT, 1,
RasterPixelIsArea);
GTIFKeySet(gtif, GTCitationGeoKey, TYPE_ASCII, 0,
"UTM 11 North / NAD27" );
</pre>
*/
int GTIFKeySet(GTIF *gtif, geokey_t keyID, tagtype_t type, int count,...)
{
va_list ap;
int index = gtif->gt_keyindex[ keyID ];
int newvalues = 0;
GeoKey *key;
char *data = NULL;
char *val = NULL;
pinfo_t sval;
double dval;
va_start(ap, count);
/* pass singleton keys by value */
if (count>1 && type!=TYPE_ASCII)
{
val = va_arg(ap, char*);
}
else if( count == -1 )
{
/* delete the indicated tag */
va_end(ap);
if( index < 1 )
return 0;
if (gtif->gt_keys[index].gk_type == TYPE_ASCII)
{
_GTIFFree (gtif->gt_keys[index].gk_data);
}
while( index < gtif->gt_num_keys )
{
_GTIFmemcpy( gtif->gt_keys + index,
gtif->gt_keys + index + 1,
sizeof(GeoKey) );
gtif->gt_keyindex[gtif->gt_keys[index].gk_key] = index;
index++;
}
gtif->gt_num_keys--;
gtif->gt_nshorts -= sizeof(KeyEntry)/sizeof(pinfo_t);
gtif->gt_keyindex[keyID] = 0;
gtif->gt_flags |= FLAG_FILE_MODIFIED;
return 1;
}
else switch (type)
{
case TYPE_SHORT: sval=(pinfo_t) va_arg(ap, int); val=(char *)&sval; break;
case TYPE_DOUBLE: dval=va_arg(ap, dblparam_t); val=(char *)&dval; break;
case TYPE_ASCII:
val=va_arg(ap, char*);
count = strlen(val) + 1; /* force = string length */
break;
default:
assert( FALSE );
break;
}
va_end(ap);
/* We assume here that there are no multi-valued SHORTS ! */
if (index)
{
/* Key already exists */
key = gtif->gt_keys+index;
if (type!=key->gk_type || count > key->gk_count)
{
/* need to reset data pointer */
key->gk_type = type;
key->gk_count = count;
key->gk_size = _gtiff_size[ type ];
newvalues = 1;
}
}
else
{
/* We need to create the key */
if (gtif->gt_num_keys == MAX_KEYS) return 0;
key = gtif->gt_keys + ++gtif->gt_num_keys;
index = gtif->gt_num_keys;
gtif->gt_keyindex[ keyID ] = index;
key->gk_key = keyID;
key->gk_type = type;
key->gk_count = count;
key->gk_size = _gtiff_size[ type ];
if (gtif->gt_keymin > keyID) gtif->gt_keymin=keyID;
if (gtif->gt_keymax < keyID) gtif->gt_keymax=keyID;
newvalues = 1;
}
if (newvalues)
{
switch (type)
{
case TYPE_SHORT:
if (count > 1) return 0;
data = (char *)&key->gk_data; /* store value *in* data */
break;
case TYPE_DOUBLE:
key->gk_data = (char *)(gtif->gt_double + gtif->gt_ndoubles);
data = key->gk_data;
gtif->gt_ndoubles += count;
break;
case TYPE_ASCII:
break;
default:
va_end(ap);
return 0;
}
gtif->gt_nshorts += sizeof(KeyEntry)/sizeof(pinfo_t);
}
/* this fixes a bug where if a request is made to write a duplicate
key, we must initialize the data to a valid value.
Bryan Wells ([email protected]) */
else /* no new values, but still have something to write */
{
switch (type)
{
case TYPE_SHORT:
if (count > 1) return 0;
data = (char *)&key->gk_data; /* store value *in* data */
break;
case TYPE_DOUBLE:
data = key->gk_data;
break;
case TYPE_ASCII:
break;
default:
return 0;
}
}
switch (type)
{
case TYPE_ASCII:
/* throw away existing data and allocate room for new data */
if (key->gk_data != 0)
{
_GTIFFree(key->gk_data);
}
key->gk_data = (char *)_GTIFcalloc(count);
key->gk_count = count;
data = key->gk_data;
break;
default:
break;
}
_GTIFmemcpy(data, val, count*key->gk_size);
gtif->gt_flags |= FLAG_FILE_MODIFIED;
return 1;
}