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)
27 #include <ctype.h> // isspace()
28 #include <math.h> // rint()
30 #include <stdlib.h> // atoi()
32 #include <sys/stat.h> // stat()
33 #include <unistd.h> // stat()
35 #include <zlib/zlib.h>
38 #include "leastsqs.hxx"
40 #include <Include/fg_constants.h>
44 # define MKDIR(a) mkdir(a,S_IRWXU) // I am just guessing at this flag (NHV)
48 fgDEM::fgDEM( void ) {
49 // printf("class fgDEM CONstructor called.\n");
50 dem_data = new float[DEM_SIZE_1][DEM_SIZE_1];
51 output_data = new float[DEM_SIZE_1][DEM_SIZE_1];
57 // return the file path name ( foo/bar/file.ext = foo/bar )
58 void extract_path (char *in, char *base) {
65 while ( (i >= 0) && (in[i] != '/') ) {
73 // Make a subdirectory
74 int my_mkdir (char *dir) {
80 result = stat (dir, &stat_buf);
84 result = stat (dir, &stat_buf);
86 printf ("problem creating %s\n", dir);
88 printf ("%s created\n", dir);
91 printf ("%s already exists\n", dir);
101 int fgDEM::open ( char *file ) {
102 // open input file (or read from stdin)
103 if ( strcmp(file, "-") == 0 ) {
104 printf("Loading DEM data file: stdin\n");
106 fd = gzdopen(STDIN_FILENO, "r");
108 if ( (fd = gzopen(file, "rb")) == NULL ) {
109 printf("Cannot gzopen %s\n", file);
112 printf("Loading DEM data file: %s\n", file);
120 int fgDEM::close ( void ) {
127 // return next token from input stream
128 static void next_token(gzFile fd, char *token) {
135 while ( (c != -1) && (c == ' ') ) {
138 while ( (c != -1) && (c != ' ') && (c != '\n') ){
146 strcpy(token, "__END_OF_FILE__");
147 printf(" Warning: Reached end of file!\n");
150 // printf(" returning %s\n", token);
154 // return next integer from input stream
155 static int next_int(gzFile fd) {
158 next_token(fd, token);
159 return ( atoi(token) );
163 // return next double from input stream
164 static double next_double(gzFile fd) {
167 next_token(fd, token);
168 return ( atof(token) );
172 // return next exponential num from input stream
173 static int next_exp(gzFile fd) {
179 next_token(fd, token);
181 sscanf(token, "%lfD%d", &mantissa, &exp);
183 // printf(" Mantissa = %.4f Exp = %d\n", mantissa, exp);
187 for ( i = 1; i <= exp; i++ ) {
190 } else if ( exp < 0 ) {
191 for ( i = -1; i >= exp; i-- ) {
196 return( (int)rint(mantissa * (double)acc) );
200 // read and parse DEM "A" record
201 int fgDEM::read_a_record( void ) {
208 // get the name field (144 characters)
209 for ( i = 0; i < 144; i++ ) {
210 name[i] = gzgetc(fd);
214 // clean off the whitespace at the end
215 for ( i = strlen(name)-2; i > 0; i-- ) {
216 if ( !isspace(name[i]) ) {
222 printf(" Quad name field: %s\n", name);
224 // DEM level code, 3 reflects processing by DMA
226 printf(" DEM level code = %d\n", inum);
232 // Pattern code, 1 indicates a regular elevation pattern
234 printf(" Pattern code = %d\n", inum);
236 // Planimetric reference system code, 0 indicates geographic
237 // coordinate system.
239 printf(" Planimetric reference code = %d\n", inum);
243 printf(" Zone code = %d\n", inum);
245 // Map projection parameters (ignored)
246 for ( i = 0; i < 15; i++ ) {
247 dnum = next_double(fd);
248 // printf("%d: %f\n",i,dnum);
251 // Units code, 3 represents arc-seconds as the unit of measure for
252 // ground planimetric coordinates throughout the file.
255 printf(" Unknown (X,Y) units code = %d!\n", inum);
259 // Units code; 2 represents meters as the unit of measure for
260 // elevation coordinates throughout the file.
263 printf(" Unknown (Z) units code = %d!\n", inum);
267 // Number (n) of sides in the polygon which defines the coverage of
268 // the DEM file (usually equal to 4).
271 printf(" Unknown polygon dimension = %d!\n", inum);
275 // Ground coordinates of bounding box in arc-seconds
276 dem_x1 = originx = next_exp(fd);
277 dem_y1 = originy = next_exp(fd);
278 printf(" Origin = (%.2f,%.2f)\n", originx, originy);
280 dem_x2 = next_exp(fd);
281 dem_y2 = next_exp(fd);
283 dem_x3 = next_exp(fd);
284 dem_y3 = next_exp(fd);
286 dem_x4 = next_exp(fd);
287 dem_y4 = next_exp(fd);
289 // Minimum/maximum elevations in meters
290 dem_z1 = next_exp(fd);
291 dem_z2 = next_exp(fd);
292 printf(" Elevation range %.4f %.4f\n", dem_z1, dem_z2);
294 // Counterclockwise angle from the primary axis of ground
295 // planimetric referenced to the primary axis of the DEM local
297 next_token(fd, token);
299 // Accuracy code; 0 indicates that a record of accuracy does not
300 // exist and that no record type C will follow.
302 // DEM spacial resolution. Usually (3,3,1) (3,6,1) or (3,9,1)
303 // depending on latitude
305 // I will eventually have to do something with this for data at
306 // higher latitudes */
307 next_token(fd, token);
308 printf(" accuracy & spacial resolution string = %s\n", token);
310 printf(" length = %d\n", i);
312 ptr = token + i - 12;
313 printf(" last field = %s = %.2f\n", ptr, atof(ptr));
317 col_step = atof(ptr);
318 printf(" last field = %s = %.2f\n", ptr, col_step);
322 row_step = atof(ptr);
323 printf(" last field = %s = %.2f\n", ptr, row_step);
326 // accuracy code = atod(token)
328 printf(" Accuracy code = %d\n", inum);
330 printf(" column step = %.2f row step = %.2f\n",
332 // dimension of arrays to follow (1)
333 next_token(fd, token);
335 // number of profiles
336 dem_num_profiles = cols = next_int(fd);
337 printf(" Expecting %d profiles\n", dem_num_profiles);
343 // read and parse DEM "B" record
344 void fgDEM::read_b_record( void ) {
348 // row / column id of this profile
349 prof_row = next_int(fd);
350 prof_col = next_int(fd);
351 // printf("col id = %d row id = %d\n", prof_col, prof_row);
353 // Number of columns and rows (elevations) in this profile
354 prof_num_rows = rows = next_int(fd);
355 prof_num_cols = next_int(fd);
356 // printf(" profile num rows = %d\n", prof_num_rows);
358 // Ground planimetric coordinates (arc-seconds) of the first
359 // elevation in the profile
360 prof_x1 = next_exp(fd);
361 prof_y1 = next_exp(fd);
362 // printf(" Starting at %.2f %.2f\n", prof_x1, prof_y1);
364 // Elevation of local datum for the profile. Always zero for
365 // 1-degree DEM, the reference is mean sea level.
366 next_token(fd, token);
368 // Minimum and maximum elevations for the profile.
369 next_token(fd, token);
370 next_token(fd, token);
372 // One (usually) dimensional array (prof_num_cols,1) of elevations
373 for ( i = 0; i < prof_num_rows; i++ ) {
374 prof_data = next_int(fd);
375 dem_data[cur_col][i] = (float)prof_data;
381 int fgDEM::parse( void ) {
386 if ( !read_a_record() ) {
390 for ( i = 0; i < dem_num_profiles; i++ ) {
391 // printf("Ready to read next b record\n");
395 if ( cur_col % 100 == 0 ) {
396 printf(" loaded %d profiles of data\n", cur_col);
400 printf(" Done parsing\n");
406 // return the current altitude based on mesh data. We should rewrite
407 // this to interpolate exact values, but for now this is good enough
408 double fgDEM::interpolate_altitude( double lon, double lat ) {
409 // we expect incoming (lon,lat) to be in arcsec for now
411 double xlocal, ylocal, dx, dy, zA, zB, elev;
412 int x1, x2, x3, y1, y2, y3;
416 /* determine if we are in the lower triangle or the upper triangle
424 then calculate our end points
427 xlocal = (lon - originx) / col_step;
428 ylocal = (lat - originy) / row_step;
430 xindex = (int)(xlocal);
431 yindex = (int)(ylocal);
433 // printf("xindex = %d yindex = %d\n", xindex, yindex);
435 if ( xindex + 1 == cols ) {
439 if ( yindex + 1 == rows ) {
443 if ( (xindex < 0) || (xindex + 1 >= cols) ||
444 (yindex < 0) || (yindex + 1 >= rows) ) {
448 dx = xlocal - xindex;
449 dy = ylocal - yindex;
453 // printf(" Lower triangle\n");
457 z1 = dem_data[x1][y1];
461 z2 = dem_data[x2][y2];
465 z3 = dem_data[x3][y3];
467 // printf(" dx = %.2f dy = %.2f\n", dx, dy);
468 // printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
469 // printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
470 // printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
472 zA = dx * (z2 - z1) + z1;
473 zB = dx * (z3 - z1) + z1;
475 // printf(" zA = %.2f zB = %.2f\n", zA, zB);
477 if ( dx > FG_EPSILON ) {
478 elev = dy * (zB - zA) / dx + zA;
484 // printf(" Upper triangle\n");
488 z1 = dem_data[x1][y1];
492 z2 = dem_data[x2][y2];
496 z3 = dem_data[x3][y3];
498 // printf(" dx = %.2f dy = %.2f\n", dx, dy);
499 // printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
500 // printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
501 // printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
503 zA = dy * (z2 - z1) + z1;
504 zB = dy * (z3 - z1) + z1;
506 // printf(" zA = %.2f zB = %.2f\n", zA, zB );
507 // printf(" xB - xA = %.2f\n", col_step * dy / row_step);
509 if ( dy > FG_EPSILON ) {
510 elev = dx * (zB - zA) / dy + zA;
520 // Use least squares to fit a simpler data set to dem data
521 void fgDEM::fit( double error, fgBUCKET *p ) {
522 double x[DEM_SIZE_1], y[DEM_SIZE_1];
523 double m, b, ave_error, max_error;
525 int n, row, start, end, good_fit;
526 int colmin, colmax, rowmin, rowmax;
527 // FILE *dem, *fit, *fit1;
529 printf("Initializing output mesh structure\n");
532 // determine dimensions
533 colmin = p->x * ( (cols - 1) / 8);
534 colmax = colmin + ( (cols - 1) / 8);
535 rowmin = p->y * ( (rows - 1) / 8);
536 rowmax = rowmin + ( (rows - 1) / 8);
537 printf("Fitting region = %d,%d to %d,%d\n", colmin, rowmin, colmax, rowmax);
539 // include the corners explicitly
540 outputmesh_set_pt(colmin, rowmin, dem_data[colmin][rowmin]);
541 outputmesh_set_pt(colmin, rowmax, dem_data[colmin][rowmax]);
542 outputmesh_set_pt(colmax, rowmax, dem_data[colmax][rowmax]);
543 outputmesh_set_pt(colmax, rowmin, dem_data[colmax][rowmin]);
545 printf("Beginning best fit procedure\n");
547 for ( row = rowmin; row <= rowmax; row++ ) {
548 // fit = fopen("fit.dat", "w");
549 // fit1 = fopen("fit1.dat", "w");
553 // printf(" fitting row = %d\n", row);
555 while ( start < colmax ) {
559 x[(end - start) - 1] = 0.0 + ( start * col_step );
560 y[(end - start) - 1] = dem_data[start][row];
562 while ( (end <= colmax) && good_fit ) {
563 n = (end - start) + 1;
564 // printf("Least square of first %d points\n", n);
565 x[end - start] = 0.0 + ( end * col_step );
566 y[end - start] = dem_data[end][row];
567 least_squares(x, y, n, &m, &b);
568 ave_error = least_squares_error(x, y, n, m, b);
569 max_error = least_squares_max_error(x, y, n, m, b);
572 printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
573 start, end, ave_error, max_error, m, b);
575 f = fopen("gnuplot.dat", "w");
576 for ( j = 0; j <= end; j++) {
577 fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
580 for ( j = start; j <= end; j++) {
581 fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
586 printf("Please hit return: "); gets(junk);
589 if ( max_error > error ) {
597 // error exceeded the threshold, back up
598 end -= 2; // back "end" up to the last good enough fit
599 n--; // back "n" up appropriately too
601 // we popped out of the above loop while still within
602 // the error threshold, so we must be at the end of
607 least_squares(x, y, n, &m, &b);
608 ave_error = least_squares_error(x, y, n, m, b);
609 max_error = least_squares_max_error(x, y, n, m, b);
613 printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
614 start, end, ave_error, max_error, m, b);
617 fprintf(fit1, "%.2f %.2f\n", x[0], m * x[0] + b);
618 fprintf(fit1, "%.2f %.2f\n", x[end-start], m * x[end-start] + b);
621 if ( start > colmin ) {
622 // skip this for the first line segment
624 outputmesh_set_pt(start, row, (lasty + cury) / 2);
625 // fprintf(fit, "%.2f %.2f\n", x[0], (lasty + cury) / 2);
628 lasty = m * x[end-start] + b;
636 dem = fopen("gnuplot.dat", "w");
637 for ( j = 0; j < DEM_SIZE_1; j++) {
638 fprintf(dem, "%.2f %.2f\n", 0.0 + ( j * col_step ),
644 // NOTICE, this is for testing only. This instance of
645 // output_nodes should be removed. It should be called only
646 // once at the end once all the nodes have been generated.
647 // newmesh_output_nodes(&nm, "mesh.node");
648 // printf("Please hit return: "); gets(junk);
651 // outputmesh_output_nodes(fg_root, p);
655 // Initialize output mesh structure
656 void fgDEM::outputmesh_init( void ) {
659 for ( j = 0; j < DEM_SIZE_1; j++ ) {
660 for ( i = 0; i < DEM_SIZE_1; i++ ) {
661 output_data[i][j] = -9999.0;
667 // Get the value of a mesh node
668 double fgDEM::outputmesh_get_pt( int i, int j ) {
669 return ( output_data[i][j] );
673 // Set the value of a mesh node
674 void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
675 // printf("Setting data[%d][%d] = %.2f\n", i, j, value);
676 output_data[i][j] = value;
680 // Write out a node file that can be used by the "triangle" program
681 void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
682 struct stat stat_buf;
683 char base_path[256], dir[256], file[256];
690 int colmin, colmax, rowmin, rowmax;
691 int i, j, count, result;
693 // determine dimensions
694 colmin = p->x * ( (cols - 1) / 8);
695 colmax = colmin + ( (cols - 1) / 8);
696 rowmin = p->y * ( (rows - 1) / 8);
697 rowmax = rowmin + ( (rows - 1) / 8);
698 printf(" dumping region = %d,%d to %d,%d\n",
699 colmin, rowmin, colmax, rowmax);
701 // generate the base directory
702 fgBucketGenBasePath(p, base_path);
703 printf("fg_root = %s Base Path = %s\n", fg_root, base_path);
704 sprintf(dir, "%s/Scenery/%s", fg_root, base_path);
705 printf("Dir = %s\n", dir);
707 // stat() directory and create if needed
708 result = stat(dir, &stat_buf);
710 printf("Stat error need to create directory\n");
714 sprintf(command, "mkdir -p %s\n", dir);
719 // Cygwin crashes when trying to output to node file
720 // explicitly making directory structure seems OK on Win95
722 extract_path (base_path, tmp_path);
724 sprintf (dir, "%s/Scenery", fg_root);
725 if (my_mkdir (dir)) { exit (-1); }
727 sprintf (dir, "%s/Scenery/%s", fg_root, tmp_path);
728 if (my_mkdir (dir)) { exit (-1); }
730 sprintf (dir, "%s/Scenery/%s", fg_root, base_path);
731 if (my_mkdir (dir)) { exit (-1); }
736 // assume directory exists
739 // get index and generate output file name
740 index = fgBucketGenIndex(p);
741 sprintf(file, "%s/%ld.node", dir, index);
743 printf("Creating node file: %s\n", file);
744 fd = fopen(file, "w");
746 // first count nodes to generate header
748 for ( j = rowmin; j <= rowmax; j++ ) {
749 for ( i = colmin; i <= colmax; i++ ) {
750 if ( output_data[i][j] > -9000.0 ) {
754 // printf(" count = %d\n", count);
756 fprintf(fd, "%d 2 1 0\n", count);
758 // now write out actual node data
760 for ( j = rowmin; j <= rowmax; j++ ) {
761 for ( i = colmin; i <= colmax; i++ ) {
762 if ( output_data[i][j] > -9000.0 ) {
763 fprintf(fd, "%d %.2f %.2f %.2f\n",
765 originx + (double)i * col_step,
766 originy + (double)j * row_step,
770 // printf(" count = %d\n", count);
777 fgDEM::~fgDEM( void ) {
778 // printf("class fgDEM DEstructor called.\n");
785 // Revision 1.8 1998/07/04 00:47:18 curt
786 // typedef'd struct fgBUCKET.
788 // Revision 1.7 1998/06/05 18:14:39 curt
789 // Abort out early when reading the "A" record if it doesn't look like
790 // a proper DEM file.
792 // Revision 1.6 1998/05/02 01:49:21 curt
793 // Fixed a bug where the wrong variable was being initialized.
795 // Revision 1.5 1998/04/25 15:00:32 curt
796 // Changed "r" to "rb" in gzopen() options. This fixes bad behavior in win32.
798 // Revision 1.4 1998/04/22 13:14:46 curt
799 // Fixed a bug in zlib usage.
801 // Revision 1.3 1998/04/18 03:53:05 curt
802 // Added zlib support.
804 // Revision 1.2 1998/04/14 02:43:27 curt
805 // Used "new" to auto-allocate large DEM parsing arrays in class constructor.
807 // Revision 1.1 1998/04/08 22:57:22 curt
808 // Adopted Gnu automake/autoconf system.
810 // Revision 1.3 1998/04/06 21:09:41 curt
811 // Additional win32 support.
812 // Fixed a bad bug in dem file parsing that was causing the output to be
813 // flipped about x = y.
815 // Revision 1.2 1998/03/23 20:35:41 curt
816 // Updated to use FG_EPSILON
818 // Revision 1.1 1998/03/19 02:54:47 curt
819 // Reorganized into a class lib called fgDEM.
821 // Revision 1.1 1998/03/19 01:46:28 curt