dem.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 /* Ported from CMU/Monarch's code, nov'98 -Padma.*/
00035 
00036 
00037 #include "config.h"
00038 #include <ctype.h>
00039 #include <errno.h>
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <dem.h>
00043 
00044 int
00045 DEMFile::open()
00046 {
00047     if((demfile = fopen(fname, "r")) == 0)
00048         return EINVAL;
00049     else
00050         return 0;
00051 }
00052 
00053 
00054 int
00055 DEMFile::read_int()
00056 {
00057     read_field();
00058     return (atoi(tempbuf));
00059 }
00060 
00061 float
00062 DEMFile::read_float()
00063 {
00064     read_field();
00065     return (atof(tempbuf));
00066 }
00067 
00068 void
00069 DEMFile::read_field()
00070 {
00071     int i;
00072     char ch;
00073 
00074     bzero(tempbuf, sizeof(tempbuf));
00075 
00076     do {
00077         ch = fgetc(demfile);
00078     } while (ch != EOF && isspace (ch));
00079     tempbuf[0] = ch;
00080 
00081     for(i = 1; ; i++) {
00082         if(feof(demfile)) {
00083             fprintf(stderr, "%s: EOF\n", __PRETTY_FUNCTION__);
00084             exit(1);
00085         }
00086         tempbuf[i] = fgetc(demfile);
00087         if(tempbuf[i] == 'D')
00088             tempbuf[i] = 'E';
00089         if(isspace(tempbuf[i]))
00090             break;
00091     }
00092 }
00093 
00094 
00095 void
00096 DEMFile::resolution(double &r)
00097 {
00098     r = 5.0;
00099 }
00100 
00101 
00102 void
00103 DEMFile::range(double &x, double &y)
00104 {
00105 #if 0
00106     int i;
00107     double minx = 0.0, miny = 0.0, maxx = 0.0, maxy = 0.0;
00108 
00109     for(i = 0; i < 4; i++) {
00110         if(minx == 0.0 || minx > a.corners[i][0]) {
00111             minx = a.corners[i][0];
00112             miny = a.corners[i][1];
00113         }
00114         else if(minx == a.corners[i][0] && miny > a.corners[i][1]) {
00115             miny = a.corners[i][1];
00116         }
00117 
00118         if(maxx == 0.0 || maxx < a.corners[i][0]) {
00119             maxx = a.corners[i][0];
00120             maxy = a.corners[i][1];
00121         }
00122         else if(maxx == a.corners[i][0] && maxy < a.corners[i][1]) {
00123             maxy = a.corners[i][1];
00124         }
00125     }
00126     x = maxx - minx;
00127     y = maxy - miny;
00128 #else
00129     x = y = (double) a.cols - 1;
00130 #endif
00131 }
00132 
00133 
00134 /* ======================================================================
00135    Returns a pointer to the "grid".
00136    ====================================================================== */
00137 int*
00138 DEMFile::process()
00139 {
00140     int i, j, offset = 0;
00141     char ch;
00142 
00143     if(fname == 0)
00144         return 0;
00145 
00146     if(open())
00147         return 0;
00148 
00149     /* ============================================================
00150        A Record
00151        ============================================================ */
00152     bzero(a.q_name, sizeof(a.q_name));
00153     for(i = 0; ! feof(demfile) && i < (int) sizeof(a.q_name) - 1; i++) {
00154         ch = fgetc(demfile);
00155         if(! isspace(ch)) {
00156             a.q_name[offset] = ch;
00157             offset++;
00158         }
00159         else {
00160             if(offset && ! isspace(a.q_name[offset-1])) {
00161                 a.q_name[offset] = ch;
00162                 offset++;
00163             }
00164         }
00165     }
00166 
00167     a.dl_code = read_int();
00168     a.p_code = read_int();
00169     a.pr_code = read_int();
00170     a.z_code = read_int();
00171 
00172     /* Map Projection Parameters */
00173     for(i = 0; i < 15; i++)
00174         a.p_parm[i] = read_float(); 
00175 
00176     a.g_units = read_int();
00177         a.e_units = read_int();
00178         a.sides = read_int();
00179 
00180     for(i = 0; i < 4; i++) {
00181         a.corners[i][0] = read_float();
00182         a.corners[i][1] = read_float();
00183     }
00184 
00185     a.min_elevation = read_float();
00186     a.max_elevation = read_float();
00187     a.angle = read_float();
00188 #if 0
00189     a.a_code = read_int();
00190     a.x_res = read_float();
00191     a.y_res = read_float();
00192     a.z_res = read_float();
00193 #else
00194     read_field();
00195 #endif
00196     a.rows = read_int();
00197     a.cols = read_int();
00198 
00199     grid = (int*) malloc(sizeof(int) * a.cols * a.cols);
00200 
00201     /* ============================================================
00202        B Records
00203        ============================================================ */
00204     for(int rows = 0; rows < a.cols; rows++) {
00205         b.row_id = read_int();
00206         b.col_id = read_int();
00207         b.rows = read_int();
00208         b.cols = read_int();
00209         b.x_gpc = read_float();
00210         b.y_gpc = read_float();
00211         b.elevation = read_float();
00212         b.min_elevation = read_float();
00213         b.max_elevation = read_float();
00214 
00215         i = rows * a.cols;
00216         for(j = 0; j < b.rows; j++)
00217             grid[i+j] = read_int();
00218     }
00219 
00220     return grid;
00221 }
00222 
00223 
00224 void
00225 DEMFile::dump_ARecord()
00226 {
00227     int i;
00228 
00229     fprintf(stdout, "*** A RECORD ***\n");
00230     fprintf(stdout, "Quadrangle name:                   %s\n", a.q_name);
00231     fprintf(stdout, "DEM Level code:                    %d\n", a.dl_code);
00232     fprintf(stdout, "Pattern code:                      %d\n", a.p_code);
00233     fprintf(stdout, "Planimetric reference system code: %d\n", a.pr_code);
00234     fprintf(stdout, "Zone code:                         %d\n", a.z_code);
00235 
00236     fprintf(stdout, "Map Projection Parameters:\n");
00237     for(i = 0; i < 15; i++)
00238         fprintf(stdout, "                                   %f\n",
00239             a.p_parm[i]);
00240 
00241     fprintf(stdout, "Ground planimetric coordinates:    %d\n", a.g_units);
00242     fprintf(stdout, "Elevation coordinates:             %d\n", a.e_units);
00243     fprintf(stdout, "Sides:                             %d\n", a.sides);
00244 
00245     fprintf(stdout, "Corners:\n");
00246     for(i = 0; i < 4; i++)
00247         fprintf(stdout, "                                   %f, %f\n",
00248             a.corners[i][0], a.corners[i][1]);
00249 
00250     fprintf(stdout, "Min Elevation:                     %f\n", a.min_elevation);
00251     fprintf(stdout, "Max Elevation:                     %f\n", a.max_elevation);
00252     fprintf(stdout, "Clockwise angle:                   %f\n", a.angle);
00253     fprintf(stdout, "Accuracy code:                     %d\n", a.a_code);
00254 
00255     fprintf(stdout, "Spatial Resolution:\n");
00256     fprintf(stdout, "                                   %f (x)\n", a.x_res);
00257     fprintf(stdout, "                                   %f (y)\n", a.y_res);
00258     fprintf(stdout, "                                   %f (z)\n", a.z_res);
00259 
00260     fprintf(stdout, "Rows:                              %d\n", a.rows);
00261     fprintf(stdout, "Columns:                           %d\n", a.cols);
00262 }
00263 
00264 
00265 void
00266 DEMFile::dump_BRecord()
00267 {
00268     fprintf(stdout, "*** B RECORD ***\n");
00269     fprintf(stdout, "Row ID:                            %d\n", b.row_id);
00270     fprintf(stdout, "Column ID:                         %d\n", b.col_id);
00271     fprintf(stdout, "Rows:                              %d\n", b.rows);
00272     fprintf(stdout, "Columns:                           %d\n", b.cols);
00273     fprintf(stdout, "Ground planimetric coordinates:    %f, %f\n",
00274         b.x_gpc, b.y_gpc);
00275     fprintf(stdout, "Elevation:                         %f\n", b.elevation);
00276     fprintf(stdout, "Min Elevation:                     %f\n", b.min_elevation);
00277     fprintf(stdout, "Max Elevation:                     %f\n", b.max_elevation);
00278 }
00279 
00280 
00281 void
00282 DEMFile::dump_grid()
00283 {
00284     int i, j;
00285     fprintf(stdout, "*** X,Y GRID ***\n");
00286     for(i = 0; i < a.cols; i++) {
00287         fprintf(stdout, "ROW %5d --- ", i);
00288         for(j = 0; j < a.cols; j++) {
00289             fprintf(stdout, "%5d ", grid[i*a.cols + j]);
00290         }
00291         fprintf(stdout, "\n");
00292     }
00293 }
00294 
00295 
00296 #if 0
00297 
00298 void main(int argc, char** argv)
00299 {
00300     DEMFile *F;
00301 
00302     if(argc != 2)
00303         exit(1);
00304 
00305     F = new DEMFile(argv[1]);
00306     F->process();
00307     F->dump_grid();
00308 }
00309 
00310 #endif
00311 

Generated on Tue Mar 6 16:47:44 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6