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