3 // dem.c -- DEM management class
5 // Written by Curtis Olson, started March 1998.
7 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 // (Log is kept at end of this file)
31 #include <ctype.h> // isspace()
32 #include <math.h> // rint()
34 #include <stdlib.h> // atoi()
36 #include <sys/stat.h> // stat()
37 #include <unistd.h> // stat()
39 #include <zlib/zlib.h>
42 #include "leastsqs.hxx"
44 #include <Include/fg_constants.h>
48 # define MKDIR(a) mkdir(a,S_IRWXU) // I am just guessing at this flag (NHV)
52 fgDEM::fgDEM( void ) {
53 // printf("class fgDEM CONstructor called.\n");
54 dem_data = new float[DEM_SIZE_1][DEM_SIZE_1];
55 output_data = new float[DEM_SIZE_1][DEM_SIZE_1];
61 // return the file path name ( foo/bar/file.ext = foo/bar )
62 void extract_path (char *in, char *base) {
69 while ( (i >= 0) && (in[i] != '/') ) {
77 // Make a subdirectory
78 int my_mkdir (char *dir) {
84 result = stat (dir, &stat_buf);
88 result = stat (dir, &stat_buf);
90 printf ("problem creating %s\n", dir);
92 printf ("%s created\n", dir);
95 printf ("%s already exists\n", dir);
105 int fgDEM::open ( char *file ) {
106 // open input file (or read from stdin)
107 if ( strcmp(file, "-") == 0 ) {
108 printf("Loading DEM data file: stdin\n");
110 fd = gzdopen(STDIN_FILENO, "r");
112 if ( (fd = gzopen(file, "rb")) == NULL ) {
113 printf("Cannot gzopen %s\n", file);
116 printf("Loading DEM data file: %s\n", file);
124 int fgDEM::close ( void ) {
131 // return next token from input stream
132 static void next_token(gzFile fd, char *token) {
139 while ( (c != -1) && (c == ' ') ) {
142 while ( (c != -1) && (c != ' ') && (c != '\n') ){
150 strcpy(token, "__END_OF_FILE__");
151 printf(" Warning: Reached end of file!\n");
154 // printf(" returning %s\n", token);
158 // return next integer from input stream
159 static int next_int(gzFile fd) {
162 next_token(fd, token);
163 return ( atoi(token) );
167 // return next double from input stream
168 static double next_double(gzFile fd) {
171 next_token(fd, token);
172 return ( atof(token) );
176 // return next exponential num from input stream
177 static int next_exp(gzFile fd) {
183 next_token(fd, token);
185 sscanf(token, "%lfD%d", &mantissa, &exp);
187 // printf(" Mantissa = %.4f Exp = %d\n", mantissa, exp);
191 for ( i = 1; i <= exp; i++ ) {
194 } else if ( exp < 0 ) {
195 for ( i = -1; i >= exp; i-- ) {
200 return( (int)rint(mantissa * (double)acc) );
204 // read and parse DEM "A" record
205 int fgDEM::read_a_record( void ) {
212 // get the name field (144 characters)
213 for ( i = 0; i < 144; i++ ) {
214 name[i] = gzgetc(fd);
218 // clean off the whitespace at the end
219 for ( i = strlen(name)-2; i > 0; i-- ) {
220 if ( !isspace(name[i]) ) {
226 printf(" Quad name field: %s\n", name);
228 // DEM level code, 3 reflects processing by DMA
230 printf(" DEM level code = %d\n", inum);
236 // Pattern code, 1 indicates a regular elevation pattern
238 printf(" Pattern code = %d\n", inum);
240 // Planimetric reference system code, 0 indicates geographic
241 // coordinate system.
243 printf(" Planimetric reference code = %d\n", inum);
247 printf(" Zone code = %d\n", inum);
249 // Map projection parameters (ignored)
250 for ( i = 0; i < 15; i++ ) {
251 dnum = next_double(fd);
252 // printf("%d: %f\n",i,dnum);
255 // Units code, 3 represents arc-seconds as the unit of measure for
256 // ground planimetric coordinates throughout the file.
259 printf(" Unknown (X,Y) units code = %d!\n", inum);
263 // Units code; 2 represents meters as the unit of measure for
264 // elevation coordinates throughout the file.
267 printf(" Unknown (Z) units code = %d!\n", inum);
271 // Number (n) of sides in the polygon which defines the coverage of
272 // the DEM file (usually equal to 4).
275 printf(" Unknown polygon dimension = %d!\n", inum);
279 // Ground coordinates of bounding box in arc-seconds
280 dem_x1 = originx = next_exp(fd);
281 dem_y1 = originy = next_exp(fd);
282 printf(" Origin = (%.2f,%.2f)\n", originx, originy);
284 dem_x2 = next_exp(fd);
285 dem_y2 = next_exp(fd);
287 dem_x3 = next_exp(fd);
288 dem_y3 = next_exp(fd);
290 dem_x4 = next_exp(fd);
291 dem_y4 = next_exp(fd);
293 // Minimum/maximum elevations in meters
294 dem_z1 = next_exp(fd);
295 dem_z2 = next_exp(fd);
296 printf(" Elevation range %.4f %.4f\n", dem_z1, dem_z2);
298 // Counterclockwise angle from the primary axis of ground
299 // planimetric referenced to the primary axis of the DEM local
301 next_token(fd, token);
303 // Accuracy code; 0 indicates that a record of accuracy does not
304 // exist and that no record type C will follow.
306 // DEM spacial resolution. Usually (3,3,1) (3,6,1) or (3,9,1)
307 // depending on latitude
309 // I will eventually have to do something with this for data at
310 // higher latitudes */
311 next_token(fd, token);
312 printf(" accuracy & spacial resolution string = %s\n", token);
314 printf(" length = %d\n", i);
316 ptr = token + i - 12;
317 printf(" last field = %s = %.2f\n", ptr, atof(ptr));
321 col_step = atof(ptr);
322 printf(" last field = %s = %.2f\n", ptr, col_step);
326 row_step = atof(ptr);
327 printf(" last field = %s = %.2f\n", ptr, row_step);
330 // accuracy code = atod(token)
332 printf(" Accuracy code = %d\n", inum);
334 printf(" column step = %.2f row step = %.2f\n",
336 // dimension of arrays to follow (1)
337 next_token(fd, token);
339 // number of profiles
340 dem_num_profiles = cols = next_int(fd);
341 printf(" Expecting %d profiles\n", dem_num_profiles);
347 // read and parse DEM "B" record
348 void fgDEM::read_b_record( void ) {
352 // row / column id of this profile
353 prof_row = next_int(fd);
354 prof_col = next_int(fd);
355 // printf("col id = %d row id = %d\n", prof_col, prof_row);
357 // Number of columns and rows (elevations) in this profile
358 prof_num_rows = rows = next_int(fd);
359 prof_num_cols = next_int(fd);
360 // printf(" profile num rows = %d\n", prof_num_rows);
362 // Ground planimetric coordinates (arc-seconds) of the first
363 // elevation in the profile
364 prof_x1 = next_exp(fd);
365 prof_y1 = next_exp(fd);
366 // printf(" Starting at %.2f %.2f\n", prof_x1, prof_y1);
368 // Elevation of local datum for the profile. Always zero for
369 // 1-degree DEM, the reference is mean sea level.
370 next_token(fd, token);
372 // Minimum and maximum elevations for the profile.
373 next_token(fd, token);
374 next_token(fd, token);
376 // One (usually) dimensional array (prof_num_cols,1) of elevations
377 for ( i = 0; i < prof_num_rows; i++ ) {
378 prof_data = next_int(fd);
379 dem_data[cur_col][i] = (float)prof_data;
385 int fgDEM::parse( void ) {
390 if ( !read_a_record() ) {
394 for ( i = 0; i < dem_num_profiles; i++ ) {
395 // printf("Ready to read next b record\n");
399 if ( cur_col % 100 == 0 ) {
400 printf(" loaded %d profiles of data\n", cur_col);
404 printf(" Done parsing\n");
410 // return the current altitude based on mesh data. We should rewrite
411 // this to interpolate exact values, but for now this is good enough
412 double fgDEM::interpolate_altitude( double lon, double lat ) {
413 // we expect incoming (lon,lat) to be in arcsec for now
415 double xlocal, ylocal, dx, dy, zA, zB, elev;
416 int x1, x2, x3, y1, y2, y3;
420 /* determine if we are in the lower triangle or the upper triangle
428 then calculate our end points
431 xlocal = (lon - originx) / col_step;
432 ylocal = (lat - originy) / row_step;
434 xindex = (int)(xlocal);
435 yindex = (int)(ylocal);
437 // printf("xindex = %d yindex = %d\n", xindex, yindex);
439 if ( xindex + 1 == cols ) {
443 if ( yindex + 1 == rows ) {
447 if ( (xindex < 0) || (xindex + 1 >= cols) ||
448 (yindex < 0) || (yindex + 1 >= rows) ) {
452 dx = xlocal - xindex;
453 dy = ylocal - yindex;
457 // printf(" Lower triangle\n");
461 z1 = dem_data[x1][y1];
465 z2 = dem_data[x2][y2];
469 z3 = dem_data[x3][y3];
471 // printf(" dx = %.2f dy = %.2f\n", dx, dy);
472 // printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
473 // printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
474 // printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
476 zA = dx * (z2 - z1) + z1;
477 zB = dx * (z3 - z1) + z1;
479 // printf(" zA = %.2f zB = %.2f\n", zA, zB);
481 if ( dx > FG_EPSILON ) {
482 elev = dy * (zB - zA) / dx + zA;
488 // printf(" Upper triangle\n");
492 z1 = dem_data[x1][y1];
496 z2 = dem_data[x2][y2];
500 z3 = dem_data[x3][y3];
502 // printf(" dx = %.2f dy = %.2f\n", dx, dy);
503 // printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
504 // printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
505 // printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
507 zA = dy * (z2 - z1) + z1;
508 zB = dy * (z3 - z1) + z1;
510 // printf(" zA = %.2f zB = %.2f\n", zA, zB );
511 // printf(" xB - xA = %.2f\n", col_step * dy / row_step);
513 if ( dy > FG_EPSILON ) {
514 elev = dx * (zB - zA) / dy + zA;
524 // Use least squares to fit a simpler data set to dem data
525 void fgDEM::fit( double error, fgBUCKET *p ) {
526 double x[DEM_SIZE_1], y[DEM_SIZE_1];
527 double m, b, ave_error, max_error;
529 int n, row, start, end, good_fit;
530 int colmin, colmax, rowmin, rowmax;
531 // FILE *dem, *fit, *fit1;
533 printf("Initializing output mesh structure\n");
536 // determine dimensions
537 colmin = p->x * ( (cols - 1) / 8);
538 colmax = colmin + ( (cols - 1) / 8);
539 rowmin = p->y * ( (rows - 1) / 8);
540 rowmax = rowmin + ( (rows - 1) / 8);
541 printf("Fitting region = %d,%d to %d,%d\n", colmin, rowmin, colmax, rowmax);
543 // include the corners explicitly
544 outputmesh_set_pt(colmin, rowmin, dem_data[colmin][rowmin]);
545 outputmesh_set_pt(colmin, rowmax, dem_data[colmin][rowmax]);
546 outputmesh_set_pt(colmax, rowmax, dem_data[colmax][rowmax]);
547 outputmesh_set_pt(colmax, rowmin, dem_data[colmax][rowmin]);
549 printf("Beginning best fit procedure\n");
551 for ( row = rowmin; row <= rowmax; row++ ) {
552 // fit = fopen("fit.dat", "w");
553 // fit1 = fopen("fit1.dat", "w");
557 // printf(" fitting row = %d\n", row);
559 while ( start < colmax ) {
563 x[(end - start) - 1] = 0.0 + ( start * col_step );
564 y[(end - start) - 1] = dem_data[start][row];
566 while ( (end <= colmax) && good_fit ) {
567 n = (end - start) + 1;
568 // printf("Least square of first %d points\n", n);
569 x[end - start] = 0.0 + ( end * col_step );
570 y[end - start] = dem_data[end][row];
571 least_squares(x, y, n, &m, &b);
572 ave_error = least_squares_error(x, y, n, m, b);
573 max_error = least_squares_max_error(x, y, n, m, b);
576 printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
577 start, end, ave_error, max_error, m, b);
579 f = fopen("gnuplot.dat", "w");
580 for ( j = 0; j <= end; j++) {
581 fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
584 for ( j = start; j <= end; j++) {
585 fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
590 printf("Please hit return: "); gets(junk);
593 if ( max_error > error ) {
601 // error exceeded the threshold, back up
602 end -= 2; // back "end" up to the last good enough fit
603 n--; // back "n" up appropriately too
605 // we popped out of the above loop while still within
606 // the error threshold, so we must be at the end of
611 least_squares(x, y, n, &m, &b);
612 ave_error = least_squares_error(x, y, n, m, b);
613 max_error = least_squares_max_error(x, y, n, m, b);
617 printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
618 start, end, ave_error, max_error, m, b);
621 fprintf(fit1, "%.2f %.2f\n", x[0], m * x[0] + b);
622 fprintf(fit1, "%.2f %.2f\n", x[end-start], m * x[end-start] + b);
625 if ( start > colmin ) {
626 // skip this for the first line segment
628 outputmesh_set_pt(start, row, (lasty + cury) / 2);
629 // fprintf(fit, "%.2f %.2f\n", x[0], (lasty + cury) / 2);
632 lasty = m * x[end-start] + b;
640 dem = fopen("gnuplot.dat", "w");
641 for ( j = 0; j < DEM_SIZE_1; j++) {
642 fprintf(dem, "%.2f %.2f\n", 0.0 + ( j * col_step ),
648 // NOTICE, this is for testing only. This instance of
649 // output_nodes should be removed. It should be called only
650 // once at the end once all the nodes have been generated.
651 // newmesh_output_nodes(&nm, "mesh.node");
652 // printf("Please hit return: "); gets(junk);
655 // outputmesh_output_nodes(fg_root, p);
659 // Initialize output mesh structure
660 void fgDEM::outputmesh_init( void ) {
663 for ( j = 0; j < DEM_SIZE_1; j++ ) {
664 for ( i = 0; i < DEM_SIZE_1; i++ ) {
665 output_data[i][j] = -9999.0;
671 // Get the value of a mesh node
672 double fgDEM::outputmesh_get_pt( int i, int j ) {
673 return ( output_data[i][j] );
677 // Set the value of a mesh node
678 void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
679 // printf("Setting data[%d][%d] = %.2f\n", i, j, value);
680 output_data[i][j] = value;
684 // Write out a node file that can be used by the "triangle" program
685 void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
686 struct stat stat_buf;
687 char base_path[256], dir[256], file[256];
694 int colmin, colmax, rowmin, rowmax;
695 int i, j, count, result;
697 // determine dimensions
698 colmin = p->x * ( (cols - 1) / 8);
699 colmax = colmin + ( (cols - 1) / 8);
700 rowmin = p->y * ( (rows - 1) / 8);
701 rowmax = rowmin + ( (rows - 1) / 8);
702 printf(" dumping region = %d,%d to %d,%d\n",
703 colmin, rowmin, colmax, rowmax);
705 // generate the base directory
706 fgBucketGenBasePath(p, base_path);
707 printf("fg_root = %s Base Path = %s\n", fg_root, base_path);
708 sprintf(dir, "%s/Scenery/%s", fg_root, base_path);
709 printf("Dir = %s\n", dir);
711 // stat() directory and create if needed
712 result = stat(dir, &stat_buf);
714 printf("Stat error need to create directory\n");
718 sprintf(command, "mkdir -p %s\n", dir);
723 // Cygwin crashes when trying to output to node file
724 // explicitly making directory structure seems OK on Win95
726 extract_path (base_path, tmp_path);
728 sprintf (dir, "%s/Scenery", fg_root);
729 if (my_mkdir (dir)) { exit (-1); }
731 sprintf (dir, "%s/Scenery/%s", fg_root, tmp_path);
732 if (my_mkdir (dir)) { exit (-1); }
734 sprintf (dir, "%s/Scenery/%s", fg_root, base_path);
735 if (my_mkdir (dir)) { exit (-1); }
740 // assume directory exists
743 // get index and generate output file name
744 index = fgBucketGenIndex(p);
745 sprintf(file, "%s/%ld.node", dir, index);
747 printf("Creating node file: %s\n", file);
748 fd = fopen(file, "w");
750 // first count nodes to generate header
752 for ( j = rowmin; j <= rowmax; j++ ) {
753 for ( i = colmin; i <= colmax; i++ ) {
754 if ( output_data[i][j] > -9000.0 ) {
758 // printf(" count = %d\n", count);
760 fprintf(fd, "%d 2 1 0\n", count);
762 // now write out actual node data
764 for ( j = rowmin; j <= rowmax; j++ ) {
765 for ( i = colmin; i <= colmax; i++ ) {
766 if ( output_data[i][j] > -9000.0 ) {
767 fprintf(fd, "%d %.2f %.2f %.2f\n",
769 originx + (double)i * col_step,
770 originy + (double)j * row_step,
774 // printf(" count = %d\n", count);
781 fgDEM::~fgDEM( void ) {
782 // printf("class fgDEM DEstructor called.\n");
789 // Revision 1.9 1998/07/13 15:29:49 curt
790 // Added #ifdef HAVE_CONFIG_H
792 // Revision 1.8 1998/07/04 00:47:18 curt
793 // typedef'd struct fgBUCKET.
795 // Revision 1.7 1998/06/05 18:14:39 curt
796 // Abort out early when reading the "A" record if it doesn't look like
797 // a proper DEM file.
799 // Revision 1.6 1998/05/02 01:49:21 curt
800 // Fixed a bug where the wrong variable was being initialized.
802 // Revision 1.5 1998/04/25 15:00:32 curt
803 // Changed "r" to "rb" in gzopen() options. This fixes bad behavior in win32.
805 // Revision 1.4 1998/04/22 13:14:46 curt
806 // Fixed a bug in zlib usage.
808 // Revision 1.3 1998/04/18 03:53:05 curt
809 // Added zlib support.
811 // Revision 1.2 1998/04/14 02:43:27 curt
812 // Used "new" to auto-allocate large DEM parsing arrays in class constructor.
814 // Revision 1.1 1998/04/08 22:57:22 curt
815 // Adopted Gnu automake/autoconf system.
817 // Revision 1.3 1998/04/06 21:09:41 curt
818 // Additional win32 support.
819 // Fixed a bad bug in dem file parsing that was causing the output to be
820 // flipped about x = y.
822 // Revision 1.2 1998/03/23 20:35:41 curt
823 // Updated to use FG_EPSILON
825 // Revision 1.1 1998/03/19 02:54:47 curt
826 // Reorganized into a class lib called fgDEM.
828 // Revision 1.1 1998/03/19 01:46:28 curt