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