]>
Commit | Line | Data |
---|---|---|
ad0a8c2d EB |
1 | #include <stdio.h> |
2 | #include <string.h> | |
3 | #include <glib.h> | |
4 | #include <math.h> | |
5 | #include <stdlib.h> | |
6 | ||
7 | #include "dem.h" | |
8 | #include "file.h" | |
9 | ||
10 | #define DEM_BLOCK_SIZE 1024 | |
11 | #define GET_COLUMN(dem,n) ((VikDEMColumn *)g_ptr_array_index( (dem)->columns, (n) )) | |
12 | ||
13 | static gboolean get_double_and_continue ( gchar **buffer, gdouble *tmp, gboolean warn ) | |
14 | { | |
15 | gchar *endptr; | |
16 | *tmp = g_strtod(*buffer, &endptr); | |
17 | if ( endptr == NULL|| endptr == *buffer ) { | |
18 | if ( warn ) | |
19 | g_warning("Invalid DEM"); | |
20 | return FALSE; | |
21 | } | |
22 | *buffer=endptr; | |
23 | return TRUE; | |
24 | } | |
25 | ||
26 | ||
27 | static gboolean get_int_and_continue ( gchar **buffer, gint *tmp, gboolean warn ) | |
28 | { | |
29 | gchar *endptr; | |
30 | *tmp = strtol(*buffer, &endptr, 10); | |
31 | if ( endptr == NULL|| endptr == *buffer ) { | |
32 | if ( warn ) | |
33 | g_warning("Invalid DEM"); | |
34 | return FALSE; | |
35 | } | |
36 | *buffer=endptr; | |
37 | return TRUE; | |
38 | } | |
39 | ||
40 | static gboolean dem_parse_header ( gchar *buffer, VikDEM *dem ) | |
41 | { | |
42 | gdouble val; | |
43 | gint int_val; | |
44 | guint i; | |
45 | gchar *tmp = buffer; | |
46 | ||
47 | /* incomplete header */ | |
48 | if ( strlen(buffer) != 1024 ) | |
49 | return FALSE; | |
50 | ||
51 | /* fix Fortran-style exponentiation 1.0D5 -> 1.0E5 */ | |
52 | while (*tmp) { | |
53 | if ( *tmp=='D') | |
54 | *tmp='E'; | |
55 | tmp++; | |
56 | } | |
57 | ||
58 | /* skip name */ | |
59 | buffer += 149; | |
60 | ||
61 | /* "DEM level code, pattern code, palaimetric reference system code" -- skip */ | |
62 | get_int_and_continue(&buffer, &int_val, TRUE); | |
63 | get_int_and_continue(&buffer, &int_val, TRUE); | |
64 | get_int_and_continue(&buffer, &int_val, TRUE); | |
65 | ||
66 | /* zone */ | |
67 | get_int_and_continue(&buffer, &int_val, TRUE); | |
68 | dem->utm_zone = int_val; | |
69 | /* TODO -- southern or northern hemisphere?! */ | |
70 | dem->utm_letter = 'N'; | |
71 | ||
72 | /* skip numbers 5-19 */ | |
73 | for ( i = 0; i < 15; i++ ) { | |
74 | if ( ! get_double_and_continue(&buffer, &val, FALSE) ) { | |
75 | g_warning ("Invalid DEM header"); | |
76 | return FALSE; | |
77 | } | |
78 | } | |
79 | ||
80 | /* number 20 -- horizontal unit code (utm/ll) */ | |
81 | get_double_and_continue(&buffer, &val, TRUE); | |
82 | dem->horiz_units = val; | |
83 | get_double_and_continue(&buffer, &val, TRUE); | |
84 | dem->vert_units = val; | |
85 | ||
86 | /* TODO: do this for real. these are only for 1:24k and 1:250k USGS */ | |
87 | if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) { | |
88 | dem->east_scale = 10.0; /* meters */ | |
89 | dem->north_scale = 10.0; | |
90 | } else { | |
91 | dem->east_scale = 3.0; /* arcseconds */ | |
92 | dem->north_scale = 3.0; | |
93 | } | |
94 | ||
95 | /* skip next */ | |
96 | get_double_and_continue(&buffer, &val, TRUE); | |
97 | ||
98 | /* now we get the four corner points. record the min and max. */ | |
99 | get_double_and_continue(&buffer, &val, TRUE); | |
100 | dem->min_east = dem->max_east = val; | |
101 | get_double_and_continue(&buffer, &val, TRUE); | |
102 | dem->min_north = dem->max_north = val; | |
103 | ||
104 | for ( i = 0; i < 3; i++ ) { | |
105 | get_double_and_continue(&buffer, &val, TRUE); | |
106 | if ( val < dem->min_east ) dem->min_east = val; | |
107 | if ( val > dem->max_east ) dem->max_east = val; | |
108 | get_double_and_continue(&buffer, &val, TRUE); | |
109 | if ( val < dem->min_north ) dem->min_north = val; | |
110 | if ( val > dem->max_north ) dem->max_north = val; | |
111 | } | |
112 | ||
113 | return TRUE; | |
114 | } | |
115 | ||
116 | static void dem_parse_block_as_cont ( gchar *buffer, VikDEM *dem, gint *cur_column, gint *cur_row ) | |
117 | { | |
118 | gint tmp; | |
119 | while ( *cur_row < GET_COLUMN(dem, *cur_column)->n_points ) { | |
120 | if ( get_int_and_continue(&buffer, &tmp,FALSE) ) | |
121 | GET_COLUMN(dem, *cur_column)->points[*cur_row] = (gint16) tmp; | |
122 | else | |
123 | return; | |
124 | (*cur_row)++; | |
125 | } | |
126 | *cur_row = -1; /* expecting new column */ | |
127 | } | |
128 | ||
129 | static void dem_parse_block_as_header ( gchar *buffer, VikDEM *dem, gint *cur_column, gint *cur_row ) | |
130 | { | |
131 | guint n_rows; | |
132 | gint i; | |
133 | gdouble east_west, south; | |
134 | gdouble tmp; | |
135 | ||
136 | /* 1 x n_rows 1 east_west south x x x DATA */ | |
137 | ||
138 | if ( (!get_double_and_continue(&buffer, &tmp, TRUE)) || tmp != 1 ) { | |
139 | g_warning("Incorrect DEM Class B record: expected 1"); | |
140 | return; | |
141 | } | |
142 | ||
143 | /* don't need this */ | |
144 | if ( !get_double_and_continue(&buffer, &tmp, TRUE ) ) return; | |
145 | ||
146 | /* n_rows */ | |
147 | if ( !get_double_and_continue(&buffer, &tmp, TRUE ) ) | |
148 | return; | |
149 | n_rows = (guint) tmp; | |
150 | ||
151 | if ( (!get_double_and_continue(&buffer, &tmp, TRUE)) || tmp != 1 ) { | |
152 | g_warning("Incorrect DEM Class B record: expected 1"); | |
153 | return; | |
154 | } | |
155 | ||
156 | if ( !get_double_and_continue(&buffer, &east_west, TRUE) ) | |
157 | return; | |
158 | if ( !get_double_and_continue(&buffer, &south, TRUE) ) | |
159 | return; | |
160 | ||
161 | /* next three things we don't need */ | |
162 | if ( !get_double_and_continue(&buffer, &tmp, TRUE)) return; | |
163 | if ( !get_double_and_continue(&buffer, &tmp, TRUE)) return; | |
164 | if ( !get_double_and_continue(&buffer, &tmp, TRUE)) return; | |
165 | ||
166 | ||
167 | dem->n_columns ++; | |
168 | (*cur_column) ++; | |
169 | ||
170 | /* empty spaces for things before that were skipped */ | |
171 | (*cur_row) = (south - dem->min_north) / dem->north_scale; | |
172 | if ( south > dem->max_north || (*cur_row) < 0 ) | |
173 | (*cur_row) = 0; | |
174 | ||
175 | n_rows += *cur_row; | |
176 | ||
177 | g_ptr_array_add ( dem->columns, g_malloc(sizeof(VikDEMColumn)) ); | |
178 | GET_COLUMN(dem,*cur_column)->east_west = east_west; | |
179 | GET_COLUMN(dem,*cur_column)->south = south; | |
180 | GET_COLUMN(dem,*cur_column)->n_points = n_rows; | |
181 | GET_COLUMN(dem,*cur_column)->points = g_malloc(sizeof(gint16)*n_rows); | |
182 | ||
183 | /* no information for things before that */ | |
184 | for ( i = 0; i < (*cur_row); i++ ) | |
185 | GET_COLUMN(dem,*cur_column)->points[i] = VIK_DEM_INVALID_ELEVATION; | |
186 | ||
187 | /* now just continue */ | |
188 | dem_parse_block_as_cont ( buffer, dem, cur_column, cur_row ); | |
189 | ||
190 | ||
191 | } | |
192 | ||
193 | static void dem_parse_block ( gchar *buffer, VikDEM *dem, gint *cur_column, gint *cur_row ) | |
194 | { | |
195 | /* if haven't read anything or have read all items in a columns and are expecting a new column */ | |
196 | if ( *cur_column == -1 || *cur_row == -1 ) { | |
197 | dem_parse_block_as_header(buffer, dem, cur_column, cur_row); | |
198 | } else { | |
199 | dem_parse_block_as_cont(buffer, dem, cur_column, cur_row); | |
200 | } | |
201 | } | |
202 | ||
203 | static VikDEM *vik_dem_read_srtm_hgt(FILE *f, const gchar *basename) | |
204 | { | |
205 | gint16 elev; | |
206 | gint i, j; | |
207 | VikDEM *dem; | |
208 | ||
209 | ||
210 | dem = g_malloc(sizeof(VikDEM)); | |
211 | ||
212 | dem->horiz_units = VIK_DEM_HORIZ_LL_ARCSECONDS; | |
213 | dem->vert_units = VIK_DEM_VERT_DECIMETERS; | |
214 | dem->east_scale = dem->north_scale = 3; | |
215 | ||
216 | /* TODO */ | |
217 | dem->min_north = atoi(basename+1) * 3600; | |
218 | dem->min_east = atoi(basename+4) * 3600; | |
219 | if ( basename[0] == 'S' ) | |
220 | dem->min_north = - dem->min_north; | |
221 | if ( basename[3] == 'W' ) | |
222 | dem->min_east = - dem->min_east; | |
223 | ||
224 | dem->max_north = 3600 + dem->min_north; | |
225 | dem->max_east = 3600 + dem->min_east; | |
226 | ||
227 | dem->columns = g_ptr_array_new(); | |
228 | dem->n_columns = 0; | |
229 | ||
230 | for ( i = 0; i < 1201; i++ ) { | |
231 | dem->n_columns++; | |
232 | g_ptr_array_add ( dem->columns, g_malloc(sizeof(VikDEMColumn)) ); | |
233 | GET_COLUMN(dem,i)->east_west = dem->min_east + 3*i; | |
234 | GET_COLUMN(dem,i)->south = dem->min_north; | |
235 | GET_COLUMN(dem,i)->n_points = 1201; | |
236 | GET_COLUMN(dem,i)->points = g_malloc(sizeof(gint16)*1201); | |
237 | } | |
238 | ||
239 | for ( i = 1200; i >= 0; i-- ) { | |
240 | for ( j = 0; j < 1201; j++ ) { | |
241 | if ( feof(f) ) { | |
242 | g_warning("Incorrect SRTM file: unexpected EOF"); | |
243 | g_print("%d %d\n", i, j); | |
244 | return dem; | |
245 | } | |
246 | fread(&elev, sizeof(elev), 1, f); | |
247 | gchar *x = (gchar *) &elev; | |
248 | gchar tmp; | |
249 | tmp=x[0]; | |
250 | x[0]=x[1]; | |
251 | x[1]=tmp; | |
252 | GET_COLUMN(dem,j)->points[i] = elev; | |
253 | } | |
254 | ||
255 | } | |
256 | ||
257 | return dem; | |
258 | } | |
259 | ||
260 | #define IS_SRTM_HGT(fn) (strlen((fn))==11 && (fn)[7]=='.' && (fn)[8]=='h' && (fn)[9]=='g' && (fn)[10]=='t' && ((fn)[0]=='N' || (fn)[0]=='S') && ((fn)[3]=='E' || (fn)[3]=='W')) | |
261 | ||
262 | VikDEM *vik_dem_new_from_file(const gchar *file) | |
263 | { | |
264 | FILE *f; | |
265 | VikDEM *rv; | |
266 | gchar buffer[DEM_BLOCK_SIZE+1]; | |
267 | ||
268 | /* use to record state for dem_parse_block */ | |
269 | gint cur_column = -1; | |
270 | gint cur_row = -1; | |
271 | const gchar *basename = a_file_basename(file); | |
272 | ||
273 | /* FILE IO */ | |
274 | f = fopen(file, "r"); | |
275 | if ( !f ) | |
276 | return NULL; | |
277 | ||
278 | if ( strlen(basename)==11 && basename[7]=='.' && basename[8]=='h' && basename[9]=='g' && basename[10]=='t' && | |
279 | (basename[0] == 'N' || basename[0] == 'S') && (basename[3] == 'E' || basename[3] =='W')) | |
280 | return vik_dem_read_srtm_hgt(f, basename); | |
281 | ||
282 | /* Create Structure */ | |
283 | rv = g_malloc(sizeof(VikDEM)); | |
284 | ||
285 | /* Header */ | |
286 | buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0'; | |
287 | if ( ! dem_parse_header ( buffer, rv ) ) { | |
288 | g_free ( rv ); | |
289 | return NULL; | |
290 | } | |
291 | /* TODO: actually use header -- i.e. GET # OF COLUMNS EXPECTED */ | |
292 | ||
293 | rv->columns = g_ptr_array_new(); | |
294 | rv->n_columns = 0; | |
295 | ||
296 | /* Column -- Data */ | |
297 | while (! feof(f) ) { | |
298 | gchar *tmp; | |
299 | ||
300 | /* read block */ | |
301 | buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0'; | |
302 | ||
303 | /* Fix Fortran-style exponentiation */ | |
304 | tmp = buffer; | |
305 | while (*tmp) { | |
306 | if ( *tmp=='D') | |
307 | *tmp='E'; | |
308 | tmp++; | |
309 | } | |
310 | ||
311 | dem_parse_block(buffer, rv, &cur_column, &cur_row); | |
312 | } | |
313 | ||
314 | /* TODO - class C records (right now says 'Invalid' and dies) */ | |
315 | ||
316 | fclose(f); | |
317 | ||
318 | /* 24k scale */ | |
319 | if ( rv->horiz_units == VIK_DEM_HORIZ_UTM_METERS && rv->n_columns >= 2 ) | |
320 | rv->north_scale = rv->east_scale = GET_COLUMN(rv, 1)->east_west - GET_COLUMN(rv,0)->east_west; | |
321 | ||
322 | /* FIXME bug in 10m DEM's */ | |
323 | if ( rv->horiz_units == VIK_DEM_HORIZ_UTM_METERS && rv->north_scale == 10 ) { | |
324 | rv->min_east -= 100; | |
325 | rv->min_north += 200; | |
326 | } | |
327 | ||
328 | ||
329 | return rv; | |
330 | } | |
331 | ||
332 | void vik_dem_free ( VikDEM *dem ) | |
333 | { | |
334 | guint i; | |
335 | for ( i = 0; i < dem->n_columns; i++) | |
336 | g_free ( GET_COLUMN(dem, i)->points ); | |
337 | g_ptr_array_free ( dem->columns, TRUE ); | |
338 | g_free ( dem ); | |
339 | } | |
340 | ||
341 | gint16 vik_dem_get_xy ( VikDEM *dem, guint col, guint row ) | |
342 | { | |
343 | if ( col < dem->n_columns ) | |
344 | if ( row < GET_COLUMN(dem, col)->n_points ) | |
345 | return GET_COLUMN(dem, col)->points[row]; | |
346 | return VIK_DEM_INVALID_ELEVATION; | |
347 | } | |
348 | ||
349 | gint16 vik_dem_get_east_north ( VikDEM *dem, gdouble east, gdouble north ) | |
350 | { | |
351 | gint col, row; | |
352 | ||
353 | if ( east > dem->max_east || east < dem->min_east || | |
354 | north > dem->max_north || north < dem->min_north ) | |
355 | return VIK_DEM_INVALID_ELEVATION; | |
356 | ||
357 | col = (gint) floor((east - dem->min_east) / dem->east_scale); | |
358 | row = (gint) floor((north - dem->min_north) / dem->north_scale); | |
359 | ||
360 | return vik_dem_get_xy ( dem, col, row ); | |
361 | } | |
362 | ||
363 | void vik_dem_east_north_to_xy ( VikDEM *dem, gdouble east, gdouble north, guint *col, guint *row ) | |
364 | { | |
365 | *col = (gint) floor((east - dem->min_east) / dem->east_scale); | |
366 | *row = (gint) floor((north - dem->min_north) / dem->north_scale); | |
367 | if ( *col < 0 ) *col = 0; | |
368 | if ( *row < 0 ) *row = 0; | |
369 | } | |
370 |