]> git.street.me.uk Git - andy/viking.git/blame - src/dem.c
Filter out irrelevant modifiers.
[andy/viking.git] / src / dem.c
CommitLineData
f6171fe3
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2008, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
72b6a3ed
JB
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
ad0a8c2d 26#include <stdio.h>
8c00358d 27#ifdef HAVE_STRING_H
ad0a8c2d 28#include <string.h>
8c00358d 29#endif
ad0a8c2d 30#include <glib.h>
8c00358d 31#ifdef HAVE_MATH_H
ad0a8c2d 32#include <math.h>
8c00358d
GB
33#endif
34#ifdef HAVE_STDLIB_H
ad0a8c2d 35#include <stdlib.h>
8c00358d 36#endif
46e4fa54
QT
37#include <zlib.h>
38#include <sys/types.h>
39#include <sys/stat.h>
31766324
MA
40#ifdef HAVE_UNISTD_H
41#include <unistd.h>
42#endif
46e4fa54 43
6af463da 44#include <glib/gstdio.h>
4c77d5e0
GB
45#include <glib/gi18n.h>
46
ad0a8c2d
EB
47#include "dem.h"
48#include "file.h"
49
50#define DEM_BLOCK_SIZE 1024
51#define GET_COLUMN(dem,n) ((VikDEMColumn *)g_ptr_array_index( (dem)->columns, (n) ))
52
53static gboolean get_double_and_continue ( gchar **buffer, gdouble *tmp, gboolean warn )
54{
55 gchar *endptr;
56 *tmp = g_strtod(*buffer, &endptr);
57 if ( endptr == NULL|| endptr == *buffer ) {
58 if ( warn )
4c77d5e0 59 g_warning(_("Invalid DEM"));
ad0a8c2d
EB
60 return FALSE;
61 }
62 *buffer=endptr;
63 return TRUE;
64}
65
66
67static gboolean get_int_and_continue ( gchar **buffer, gint *tmp, gboolean warn )
68{
69 gchar *endptr;
70 *tmp = strtol(*buffer, &endptr, 10);
71 if ( endptr == NULL|| endptr == *buffer ) {
72 if ( warn )
4c77d5e0 73 g_warning(_("Invalid DEM"));
ad0a8c2d
EB
74 return FALSE;
75 }
76 *buffer=endptr;
77 return TRUE;
78}
79
80static gboolean dem_parse_header ( gchar *buffer, VikDEM *dem )
81{
82 gdouble val;
83 gint int_val;
84 guint i;
85 gchar *tmp = buffer;
86
87 /* incomplete header */
88 if ( strlen(buffer) != 1024 )
89 return FALSE;
90
91 /* fix Fortran-style exponentiation 1.0D5 -> 1.0E5 */
92 while (*tmp) {
93 if ( *tmp=='D')
94 *tmp='E';
95 tmp++;
96 }
97
98 /* skip name */
99 buffer += 149;
100
101 /* "DEM level code, pattern code, palaimetric reference system code" -- skip */
102 get_int_and_continue(&buffer, &int_val, TRUE);
103 get_int_and_continue(&buffer, &int_val, TRUE);
104 get_int_and_continue(&buffer, &int_val, TRUE);
105
106 /* zone */
107 get_int_and_continue(&buffer, &int_val, TRUE);
108 dem->utm_zone = int_val;
109 /* TODO -- southern or northern hemisphere?! */
110 dem->utm_letter = 'N';
111
112 /* skip numbers 5-19 */
113 for ( i = 0; i < 15; i++ ) {
114 if ( ! get_double_and_continue(&buffer, &val, FALSE) ) {
4c77d5e0 115 g_warning (_("Invalid DEM header"));
ad0a8c2d
EB
116 return FALSE;
117 }
118 }
119
120 /* number 20 -- horizontal unit code (utm/ll) */
121 get_double_and_continue(&buffer, &val, TRUE);
122 dem->horiz_units = val;
123 get_double_and_continue(&buffer, &val, TRUE);
70147e60 124 /* dem->orig_vert_units = val; now done below */
ad0a8c2d
EB
125
126 /* TODO: do this for real. these are only for 1:24k and 1:250k USGS */
127 if ( dem->horiz_units == VIK_DEM_HORIZ_UTM_METERS ) {
128 dem->east_scale = 10.0; /* meters */
129 dem->north_scale = 10.0;
70147e60 130 dem->orig_vert_units = VIK_DEM_VERT_DECIMETERS;
ad0a8c2d
EB
131 } else {
132 dem->east_scale = 3.0; /* arcseconds */
133 dem->north_scale = 3.0;
70147e60 134 dem->orig_vert_units = VIK_DEM_VERT_METERS;
ad0a8c2d
EB
135 }
136
137 /* skip next */
138 get_double_and_continue(&buffer, &val, TRUE);
139
140 /* now we get the four corner points. record the min and max. */
141 get_double_and_continue(&buffer, &val, TRUE);
142 dem->min_east = dem->max_east = val;
143 get_double_and_continue(&buffer, &val, TRUE);
144 dem->min_north = dem->max_north = val;
145
146 for ( i = 0; i < 3; i++ ) {
147 get_double_and_continue(&buffer, &val, TRUE);
148 if ( val < dem->min_east ) dem->min_east = val;
149 if ( val > dem->max_east ) dem->max_east = val;
150 get_double_and_continue(&buffer, &val, TRUE);
151 if ( val < dem->min_north ) dem->min_north = val;
152 if ( val > dem->max_north ) dem->max_north = val;
153 }
154
155 return TRUE;
156}
157
158static void dem_parse_block_as_cont ( gchar *buffer, VikDEM *dem, gint *cur_column, gint *cur_row )
159{
160 gint tmp;
161 while ( *cur_row < GET_COLUMN(dem, *cur_column)->n_points ) {
70147e60
EB
162 if ( get_int_and_continue(&buffer, &tmp,FALSE) ) {
163 if ( dem->orig_vert_units == VIK_DEM_VERT_DECIMETERS )
164 GET_COLUMN(dem, *cur_column)->points[*cur_row] = (gint16) (tmp / 10);
165 else
166 GET_COLUMN(dem, *cur_column)->points[*cur_row] = (gint16) tmp;
167 } else
ad0a8c2d
EB
168 return;
169 (*cur_row)++;
170 }
171 *cur_row = -1; /* expecting new column */
172}
173
174static void dem_parse_block_as_header ( gchar *buffer, VikDEM *dem, gint *cur_column, gint *cur_row )
175{
176 guint n_rows;
177 gint i;
178 gdouble east_west, south;
179 gdouble tmp;
180
181 /* 1 x n_rows 1 east_west south x x x DATA */
182
183 if ( (!get_double_and_continue(&buffer, &tmp, TRUE)) || tmp != 1 ) {
4c77d5e0 184 g_warning(_("Incorrect DEM Class B record: expected 1"));
ad0a8c2d
EB
185 return;
186 }
187
188 /* don't need this */
189 if ( !get_double_and_continue(&buffer, &tmp, TRUE ) ) return;
190
191 /* n_rows */
192 if ( !get_double_and_continue(&buffer, &tmp, TRUE ) )
193 return;
194 n_rows = (guint) tmp;
195
196 if ( (!get_double_and_continue(&buffer, &tmp, TRUE)) || tmp != 1 ) {
4c77d5e0 197 g_warning(_("Incorrect DEM Class B record: expected 1"));
ad0a8c2d
EB
198 return;
199 }
200
201 if ( !get_double_and_continue(&buffer, &east_west, TRUE) )
202 return;
203 if ( !get_double_and_continue(&buffer, &south, TRUE) )
204 return;
205
206 /* next three things we don't need */
207 if ( !get_double_and_continue(&buffer, &tmp, TRUE)) return;
208 if ( !get_double_and_continue(&buffer, &tmp, TRUE)) return;
209 if ( !get_double_and_continue(&buffer, &tmp, TRUE)) return;
210
211
212 dem->n_columns ++;
213 (*cur_column) ++;
214
215 /* empty spaces for things before that were skipped */
216 (*cur_row) = (south - dem->min_north) / dem->north_scale;
217 if ( south > dem->max_north || (*cur_row) < 0 )
218 (*cur_row) = 0;
219
220 n_rows += *cur_row;
221
222 g_ptr_array_add ( dem->columns, g_malloc(sizeof(VikDEMColumn)) );
223 GET_COLUMN(dem,*cur_column)->east_west = east_west;
224 GET_COLUMN(dem,*cur_column)->south = south;
225 GET_COLUMN(dem,*cur_column)->n_points = n_rows;
226 GET_COLUMN(dem,*cur_column)->points = g_malloc(sizeof(gint16)*n_rows);
227
228 /* no information for things before that */
229 for ( i = 0; i < (*cur_row); i++ )
230 GET_COLUMN(dem,*cur_column)->points[i] = VIK_DEM_INVALID_ELEVATION;
231
232 /* now just continue */
233 dem_parse_block_as_cont ( buffer, dem, cur_column, cur_row );
234
235
236}
237
238static void dem_parse_block ( gchar *buffer, VikDEM *dem, gint *cur_column, gint *cur_row )
239{
240 /* if haven't read anything or have read all items in a columns and are expecting a new column */
241 if ( *cur_column == -1 || *cur_row == -1 ) {
242 dem_parse_block_as_header(buffer, dem, cur_column, cur_row);
243 } else {
244 dem_parse_block_as_cont(buffer, dem, cur_column, cur_row);
245 }
246}
247
46e4fa54
QT
248/* return size of unzip data or 0 if failed */
249/* can be made generic to uncompress zip, gzip, bzip2 data */
250static guint uncompress_data(void *uncompressed_buffer, guint uncompressed_size, void *compressed_data, guint compressed_size)
251{
252 z_stream stream;
253 int err;
254
255 stream.next_in = compressed_data;
256 stream.avail_in = compressed_size;
257 stream.next_out = uncompressed_buffer;
258 stream.avail_out = uncompressed_size;
259 stream.zalloc = (alloc_func)0;
260 stream.zfree = (free_func)0;
261 stream.opaque = (voidpf)0;
262
263 /* negative windowBits to inflateInit2 means "no header" */
264 if ((err = inflateInit2(&stream, -MAX_WBITS)) != Z_OK) {
265 g_warning("%s(): inflateInit2 failed", __PRETTY_FUNCTION__);
266 return 0;
267 }
268
269 err = inflate(&stream, Z_FINISH);
270 if ((err != Z_OK) && (err != Z_STREAM_END) && stream.msg) {
271 g_warning("%s() inflate failed err=%d \"%s\"", __PRETTY_FUNCTION__, err, stream.msg == NULL ? "unknown" : stream.msg);
272 inflateEnd(&stream);
273 return 0;
274 }
275
276 inflateEnd(&stream);
277 return(stream.total_out);
278}
279
280static void *unzip_hgt_file(gchar *zip_file, gulong *unzip_size)
281{
282 void *unzip_data = NULL;
283 gchar *zip_data;
284 struct _lfh {
285 guint32 sig;
286 guint16 extract_version;
287 guint16 flags;
288 guint16 comp_method;
289 guint16 time;
290 guint16 date;
291 guint32 crc_32;
292 guint32 compressed_size;
293 guint32 uncompressed_size;
294 guint16 filename_len;
295 guint16 extra_field_len;
296 } __attribute__ ((__packed__)) *local_file_header = NULL;
297
298
299 local_file_header = (struct _lfh *) zip_file;
300 if (local_file_header->sig != 0x04034b50) {
4258f4e2 301 g_warning("%s(): wrong format", __PRETTY_FUNCTION__);
46e4fa54
QT
302 g_free(unzip_data);
303 goto end;
304 }
305
306 zip_data = zip_file + sizeof(struct _lfh) + local_file_header->filename_len + local_file_header->extra_field_len;
307 unzip_data = g_malloc(local_file_header->uncompressed_size);
308 gulong uncompressed_size = local_file_header->uncompressed_size;
309
310 if (!(*unzip_size = uncompress_data(unzip_data, uncompressed_size, zip_data, local_file_header->compressed_size))) {
311 g_free(unzip_data);
312 unzip_data = NULL;
313 goto end;
314 }
315
316end:
317 return(unzip_data);
318}
319
31766324 320static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basename, gboolean zip)
ad0a8c2d 321{
ad0a8c2d
EB
322 gint i, j;
323 VikDEM *dem;
46e4fa54
QT
324 off_t file_size;
325 gint16 *dem_mem = NULL;
31766324 326 gchar *dem_file = NULL;
46e4fa54
QT
327 const gint num_rows_3sec = 1201;
328 const gint num_rows_1sec = 3601;
329 gint num_rows;
31766324 330 GMappedFile *mf;
46e4fa54 331 gint arcsec;
a95be497 332 GError *error = NULL;
ad0a8c2d
EB
333
334 dem = g_malloc(sizeof(VikDEM));
335
336 dem->horiz_units = VIK_DEM_HORIZ_LL_ARCSECONDS;
70147e60 337 dem->orig_vert_units = VIK_DEM_VERT_DECIMETERS;
ad0a8c2d
EB
338
339 /* TODO */
340 dem->min_north = atoi(basename+1) * 3600;
341 dem->min_east = atoi(basename+4) * 3600;
342 if ( basename[0] == 'S' )
343 dem->min_north = - dem->min_north;
344 if ( basename[3] == 'W' )
345 dem->min_east = - dem->min_east;
346
347 dem->max_north = 3600 + dem->min_north;
348 dem->max_east = 3600 + dem->min_east;
349
350 dem->columns = g_ptr_array_new();
351 dem->n_columns = 0;
352
a95be497
GB
353 if ((mf = g_mapped_file_new(file_name, FALSE, &error)) == NULL) {
354 g_error(_("Couldn't map file %s: %s"), file_name, error->message);
355 g_error_free(error);
31766324
MA
356 g_free(dem);
357 return NULL;
358 }
359 file_size = g_mapped_file_get_length(mf);
360 dem_file = g_mapped_file_get_contents(mf);
361
46e4fa54
QT
362 if (zip) {
363 void *unzip_mem = NULL;
364 gulong ucsize;
365
31766324
MA
366 if ((unzip_mem = unzip_hgt_file(dem_file, &ucsize)) == NULL) {
367 g_mapped_file_free(mf);
46e4fa54
QT
368 g_ptr_array_free(dem->columns, TRUE);
369 g_free(dem);
370 return NULL;
371 }
372
373 dem_mem = unzip_mem;
374 file_size = ucsize;
375 }
376
377 if (file_size == (num_rows_3sec * num_rows_3sec * sizeof(gint16)))
378 arcsec = 3;
379 else if (file_size == (num_rows_1sec * num_rows_1sec * sizeof(gint16)))
380 arcsec = 1;
381 else {
4258f4e2 382 g_warning("%s(): file %s does not have right size", __PRETTY_FUNCTION__, basename);
31766324 383 g_mapped_file_free(mf);
46e4fa54
QT
384 g_free(dem);
385 return NULL;
386 }
387
388 num_rows = (arcsec == 3) ? num_rows_3sec : num_rows_1sec;
389 dem->east_scale = dem->north_scale = arcsec;
390
391 for ( i = 0; i < num_rows; i++ ) {
ad0a8c2d
EB
392 dem->n_columns++;
393 g_ptr_array_add ( dem->columns, g_malloc(sizeof(VikDEMColumn)) );
46e4fa54 394 GET_COLUMN(dem,i)->east_west = dem->min_east + arcsec*i;
ad0a8c2d 395 GET_COLUMN(dem,i)->south = dem->min_north;
46e4fa54
QT
396 GET_COLUMN(dem,i)->n_points = num_rows;
397 GET_COLUMN(dem,i)->points = g_malloc(sizeof(gint16)*num_rows);
ad0a8c2d
EB
398 }
399
46e4fa54
QT
400 int ent = 0;
401 for ( i = (num_rows - 1); i >= 0; i-- ) {
402 for ( j = 0; j < num_rows; j++ ) {
792a7488
QT
403 GET_COLUMN(dem,j)->points[i] = GINT16_FROM_BE(dem_mem[ent]);
404 ent++;
ad0a8c2d
EB
405 }
406
407 }
408
46e4fa54
QT
409 if (zip)
410 g_free(dem_mem);
31766324 411 g_mapped_file_free(mf);
ad0a8c2d
EB
412 return dem;
413}
414
415#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'))
416
417VikDEM *vik_dem_new_from_file(const gchar *file)
418{
31766324 419 FILE *f=NULL;
ad0a8c2d
EB
420 VikDEM *rv;
421 gchar buffer[DEM_BLOCK_SIZE+1];
422
423 /* use to record state for dem_parse_block */
424 gint cur_column = -1;
425 gint cur_row = -1;
426 const gchar *basename = a_file_basename(file);
427
31766324 428 if ( g_access ( file, R_OK ) != 0 )
ad0a8c2d
EB
429 return NULL;
430
46e4fa54
QT
431 if ( (strlen(basename)==11 || ((strlen(basename) == 15) && (basename[11] == '.' && basename[12] == 'z' && basename[13] == 'i' && basename[14] == 'p'))) &&
432 basename[7]=='.' && basename[8]=='h' && basename[9]=='g' && basename[10]=='t' &&
2dc9ab5e 433 (basename[0] == 'N' || basename[0] == 'S') && (basename[3] == 'E' || basename[3] =='W')) {
46e4fa54 434 gboolean is_zip_file = (strlen(basename) == 15);
31766324 435 rv = vik_dem_read_srtm_hgt(file, basename, is_zip_file);
2dc9ab5e
QT
436 return(rv);
437 }
ad0a8c2d
EB
438
439 /* Create Structure */
440 rv = g_malloc(sizeof(VikDEM));
441
442 /* Header */
31766324
MA
443 f = g_fopen(file, "r");
444 if ( !f )
445 return NULL;
ad0a8c2d
EB
446 buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0';
447 if ( ! dem_parse_header ( buffer, rv ) ) {
448 g_free ( rv );
449 return NULL;
450 }
451 /* TODO: actually use header -- i.e. GET # OF COLUMNS EXPECTED */
452
453 rv->columns = g_ptr_array_new();
454 rv->n_columns = 0;
455
456 /* Column -- Data */
457 while (! feof(f) ) {
458 gchar *tmp;
459
460 /* read block */
461 buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0';
462
463 /* Fix Fortran-style exponentiation */
464 tmp = buffer;
465 while (*tmp) {
466 if ( *tmp=='D')
467 *tmp='E';
468 tmp++;
469 }
470
471 dem_parse_block(buffer, rv, &cur_column, &cur_row);
472 }
473
474 /* TODO - class C records (right now says 'Invalid' and dies) */
475
476 fclose(f);
8c060406 477 f = NULL;
ad0a8c2d
EB
478
479 /* 24k scale */
480 if ( rv->horiz_units == VIK_DEM_HORIZ_UTM_METERS && rv->n_columns >= 2 )
481 rv->north_scale = rv->east_scale = GET_COLUMN(rv, 1)->east_west - GET_COLUMN(rv,0)->east_west;
482
483 /* FIXME bug in 10m DEM's */
484 if ( rv->horiz_units == VIK_DEM_HORIZ_UTM_METERS && rv->north_scale == 10 ) {
485 rv->min_east -= 100;
486 rv->min_north += 200;
487 }
488
489
490 return rv;
491}
492
493void vik_dem_free ( VikDEM *dem )
494{
495 guint i;
496 for ( i = 0; i < dem->n_columns; i++)
497 g_free ( GET_COLUMN(dem, i)->points );
498 g_ptr_array_free ( dem->columns, TRUE );
499 g_free ( dem );
500}
501
502gint16 vik_dem_get_xy ( VikDEM *dem, guint col, guint row )
503{
504 if ( col < dem->n_columns )
505 if ( row < GET_COLUMN(dem, col)->n_points )
506 return GET_COLUMN(dem, col)->points[row];
507 return VIK_DEM_INVALID_ELEVATION;
508}
509
510gint16 vik_dem_get_east_north ( VikDEM *dem, gdouble east, gdouble north )
511{
512 gint col, row;
513
514 if ( east > dem->max_east || east < dem->min_east ||
515 north > dem->max_north || north < dem->min_north )
516 return VIK_DEM_INVALID_ELEVATION;
517
518 col = (gint) floor((east - dem->min_east) / dem->east_scale);
519 row = (gint) floor((north - dem->min_north) / dem->north_scale);
520
521 return vik_dem_get_xy ( dem, col, row );
522}
523
5ef1d57e
QT
524static gboolean dem_get_ref_points_elev_dist(VikDEM *dem,
525 gdouble east, gdouble north, /* in seconds */
526 gint16 *elevs, gint16 *dists)
527{
528 int i;
529 int cols[4], rows[4];
530 struct LatLon ll[4];
531 struct LatLon pos;
532
533 if ( east > dem->max_east || east < dem->min_east ||
534 north > dem->max_north || north < dem->min_north )
535 return FALSE; /* got nothing */
536
537 pos.lon = east/3600;
538 pos.lat = north/3600;
539
540 /* order of the data: sw, nw, ne, se */
245575e1
QT
541 /* sw */
542 cols[0] = (gint) floor((east - dem->min_east) / dem->east_scale);
5ef1d57e 543 rows[0] = (gint) floor((north - dem->min_north) / dem->north_scale);
228213c5
QT
544 ll[0].lon = (dem->min_east + dem->east_scale*cols[0])/3600;
545 ll[0].lat = (dem->min_north + dem->north_scale*rows[0])/3600;
245575e1
QT
546 /* nw */
547 cols[1] = cols[0];
5ef1d57e
QT
548 rows[1] = rows[0] + 1;
549 ll[1].lon = ll[0].lon;
550 ll[1].lat = ll[0].lat + (gdouble)dem->north_scale/3600;
245575e1
QT
551 /* ne */
552 cols[2] = cols[0] + 1;
5ef1d57e
QT
553 rows[2] = rows[0] + 1;
554 ll[2].lon = ll[0].lon + (gdouble)dem->east_scale/3600;
555 ll[2].lat = ll[0].lat + (gdouble)dem->north_scale/3600;
245575e1
QT
556 /* se */
557 cols[3] = cols[0] + 1;
5ef1d57e
QT
558 rows[3] = rows[0];
559 ll[3].lon = ll[0].lon + (gdouble)dem->east_scale/3600;
560 ll[3].lat = ll[0].lat;
561
562 for (i = 0; i < 4; i++) {
563 if ((elevs[i] = vik_dem_get_xy(dem, cols[i], rows[i])) == VIK_DEM_INVALID_ELEVATION)
564 return FALSE;
565 dists[i] = a_coords_latlon_diff(&pos, &ll[i]);
566 }
567
228213c5
QT
568#if 0 /* debug */
569 for (i = 0; i < 4; i++)
570 fprintf(stderr, "%f:%f:%d:%d ", ll[i].lat, ll[i].lon, dists[i], elevs[i]);
571 fprintf(stderr, " north_scale=%f\n", dem->north_scale);
572#endif
573
5ef1d57e
QT
574 return TRUE; /* all OK */
575}
576
577gint16 vik_dem_get_simple_interpol ( VikDEM *dem, gdouble east, gdouble north )
578{
579 int i;
580 gint16 elevs[4], dists[4];
581
582 if (!dem_get_ref_points_elev_dist(dem, east, north, elevs, dists))
583 return VIK_DEM_INVALID_ELEVATION;
584
585 for (i = 0; i < 4; i++) {
586 if (dists[i] < 1) {
587 return(elevs[i]);
588 }
589 }
590
591 gdouble t = (gdouble)elevs[0]/dists[0] + (gdouble)elevs[1]/dists[1] + (gdouble)elevs[2]/dists[2] + (gdouble)elevs[3]/dists[3];
592 gdouble b = 1.0/dists[0] + 1.0/dists[1] + 1.0/dists[2] + 1.0/dists[3];
593
594 return(t/b);
595}
596
597gint16 vik_dem_get_shepard_interpol ( VikDEM *dem, gdouble east, gdouble north )
598{
228213c5
QT
599 int i;
600 gint16 elevs[4], dists[4];
601 gint16 max_dist;
602 gdouble t = 0.0;
603 gdouble b = 0.0;
604
605 if (!dem_get_ref_points_elev_dist(dem, east, north, elevs, dists))
606 return VIK_DEM_INVALID_ELEVATION;
607
608 max_dist = 0;
609 for (i = 0; i < 4; i++) {
610 if (dists[i] < 1) {
611 return(elevs[i]);
612 }
613 if (dists[i] > max_dist)
614 max_dist = dists[i];
615 }
616
617 gdouble tmp;
618#if 0 /* derived method by Franke & Nielson. Does not seem to work too well here */
619 for (i = 0; i < 4; i++) {
620 tmp = pow((1.0*(max_dist - dists[i])/max_dist*dists[i]), 2);
621 t += tmp*elevs[i];
622 b += tmp;
623 }
624#endif
625
626 for (i = 0; i < 4; i++) {
627 tmp = pow((1.0/dists[i]), 2);
628 t += tmp*elevs[i];
629 b += tmp;
630 }
631
632 // fprintf(stderr, "DEBUG: tmp=%f t=%f b=%f %f\n", tmp, t, b, t/b);
633
634 return(t/b);
635
5ef1d57e
QT
636}
637
ad0a8c2d
EB
638void vik_dem_east_north_to_xy ( VikDEM *dem, gdouble east, gdouble north, guint *col, guint *row )
639{
640 *col = (gint) floor((east - dem->min_east) / dem->east_scale);
641 *row = (gint) floor((north - dem->min_north) / dem->north_scale);
642 if ( *col < 0 ) *col = 0;
643 if ( *row < 0 ) *row = 0;
644}
645