vtkDEMReader.cxx 15.6 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkDEMReader.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13
14
15
16

=========================================================================*/
#include "vtkImageData.h"
#include "vtkDEMReader.h"
17
18
#include "vtkObjectFactory.h"

19
vtkCxxRevisionMacro(vtkDEMReader, "1.34");
Brad King's avatar
Brad King committed
20
vtkStandardNewMacro(vtkDEMReader);
21

22
23
24
25
#define VTK_SW  0
#define VTK_NW  1
#define VTK_NE  2
#define VTK_SE  3
Bill Lorensen's avatar
Bill Lorensen committed
26
27
#define VTK_METERS_PER_FEET .305
#define VTK_METERS_PER_ARC_SECOND 23.111
28
29
30
31
32

void ConvertDNotationToENotation (char *line);

vtkDEMReader::vtkDEMReader()
{
Bill Lorensen's avatar
Bill Lorensen committed
33
  int i, j;
34
35
  this->NumberOfColumns = 0;
  this->NumberOfRows = 0;
Bill Lorensen's avatar
Bill Lorensen committed
36
37
38
39
40
41
42
43
44
45
46
47
  for (i = 0; i < 6; i++)
    {
    this->WholeExtent[i] = 0;
    }
  this->FileName = NULL;
  for (i = 0; i < 145; i++)
    {
    this->MapLabel[i] = '\0';
    }
  this->DEMLevel = 0;
  this->ElevationPattern = 0;
  this->GroundSystem = 0;
48
  this->ProfileSeekOffset = 0;
Bill Lorensen's avatar
Bill Lorensen committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  this->GroundZone = 0;
  for (i = 0; i < 15; i++)
    {
    this->ProjectionParameters[i] = 0;
    }
  this->PlaneUnitOfMeasure = 0;
  this->ElevationUnitOfMeasure = 0;
  this->PolygonSize = 0;
  for (i = 0; i < 2; i++)
    {
    this->ElevationBounds[i] = 0;
    this->ProfileDimension[i] = 0;
    for (j = 0; j < 4; j++)
      {
      this->GroundCoords[j][i] = 0;
      }
    }
  this->LocalRotation = 0;
  this->AccuracyCode = 0;
Ken Martin's avatar
Ken Martin committed
68
  for (i = 0; i < 3; i++)
Bill Lorensen's avatar
Bill Lorensen committed
69
70
71
    {
    this->SpatialResolution[i] = 0;
    }
72
73
74
75
}

vtkDEMReader::~vtkDEMReader()
{
Bill Lorensen's avatar
Bill Lorensen committed
76
77
78
79
  if (this->FileName)
    {
    delete [] this->FileName;
    }
80
81
82
}

//----------------------------------------------------------------------------
83
void vtkDEMReader::ExecuteInformation()
84
{
Ken Martin's avatar
Ken Martin committed
85
  double spacing[3], origin[3];
86
87
  int extent[6];

88
89
90
91
92
93
  if (!this->FileName)
    {
    vtkErrorMacro(<< "A FileName must be specified.");
    return;
    }

94
95
96
97
98
99
100
  // read the header of the file to determine dimensions, origin and spacing
  this->ReadTypeARecord ();

  // compute the extent based on the header information
  this->ComputeExtentOriginAndSpacing (extent, origin, spacing);

  // fill in the pertinent stuff from the header
101
102
  this->GetOutput()->SetOrigin(origin);
  this->GetOutput()->SetSpacing(spacing);
103

104
105
  this->GetOutput()->SetNumberOfScalarComponents(1);
  this->GetOutput()->SetScalarType(VTK_FLOAT);
106
107

  // whole dem must be read
108
  this->GetOutput()->SetWholeExtent(extent);
109
110
}

111

112
113
//----------------------------------------------------------------------------
// Convert to Imaging API
Ken Martin's avatar
Ken Martin committed
114
void vtkDEMReader::ExecuteData(vtkDataObject *)
115
{
116
  vtkImageData *output = this->GetOutput();
117

118
119
  output->SetExtent(output->GetWholeExtent());
  output->AllocateScalars();
120

121
122
123
124
125
126
  if (!this->FileName)
    {
    vtkErrorMacro(<< "A FileName must be specified.");
    return;
    }

127
  if (output->GetScalarType() != VTK_FLOAT)
128
    {
129
    vtkErrorMacro("Execute: This source only outputs floats.");
130
131
132
133
134
135
136
137
138
139
140
    return;
    }
  
//
// Read header
//
  if (this->ReadTypeARecord () == 0)
    {
    //
    // Read Profiles
    //
141
    this->ReadProfiles (output);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    }
}

int vtkDEMReader::ReadTypeARecord ()
{
  char record[1025];
  float elevationConversion;
  FILE *fp;

  if (this->GetMTime () < this->ReadHeaderTime)
    {
    return 0;
    }

156
157
158
159
160
161
  if (!this->FileName)
    {
    vtkErrorMacro(<< "A FileName must be specified.");
    return -1;
    }

162
  if ((fp = fopen(this->FileName, "rb")) == NULL)
163
164
165
166
167
168
169
170
171
172
    {
    vtkErrorMacro(<< "File " << this->FileName << " not found");
    return -1;
    }

  vtkDebugMacro (<< "reading DEM header: type A record");

  //
  // read the record. it is always 1024 characters long
  //
173
174
  fscanf(fp, "%512c", record); 
  fscanf(fp, "%512c", record+512); 
175
176
177
178
179
180
181
182
183
184
  record[1024] = '\0';

  //
  // convert any D+ or D- to E+ or E-. c++ and c i/o cannot read D+/-
  //
  ConvertDNotationToENotation (record);

  char *current = record;

  this->MapLabel[144] = '\0';
Bill Lorensen's avatar
Bill Lorensen committed
185
  sscanf(current, "%144c", this->MapLabel);
186
187
  current += 144;

Bill Lorensen's avatar
Bill Lorensen committed
188
  sscanf(current, "%6d%6d%6d%6d",
189
190
191
192
                 &this->DEMLevel,
                 &this->ElevationPattern,
                 &this->GroundSystem,
                 &this->GroundZone);
193
  current += 24;
Bill Lorensen's avatar
Bill Lorensen committed
194
  sscanf(current, "%24g%24g%24g%24g%24g%24g%24g%24g%24g%24g%24g%24g%24g%24g%24g",
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
   &this->ProjectionParameters[0],
   &this->ProjectionParameters[1],
   &this->ProjectionParameters[2],
   &this->ProjectionParameters[3],
   &this->ProjectionParameters[4],
   &this->ProjectionParameters[5],
   &this->ProjectionParameters[6],
   &this->ProjectionParameters[7],
   &this->ProjectionParameters[8],
   &this->ProjectionParameters[9],
   &this->ProjectionParameters[10],
   &this->ProjectionParameters[11],
   &this->ProjectionParameters[12],
   &this->ProjectionParameters[13],
   &this->ProjectionParameters[14]);
  current += 360;
Bill Lorensen's avatar
Bill Lorensen committed
211
  sscanf(current, "%6d%6d%6d",
212
213
214
215
   &this->PlaneUnitOfMeasure,
   &this->ElevationUnitOfMeasure,
   &this->PolygonSize);
  current += 18;
Bill Lorensen's avatar
Bill Lorensen committed
216
  sscanf(current, "%24g%24g%24g%24g%24g%24g%24g%24g",
217
218
219
220
221
   &this->GroundCoords[0][0], &this->GroundCoords[0][1],
   &this->GroundCoords[1][0], &this->GroundCoords[1][1],
   &this->GroundCoords[2][0], &this->GroundCoords[2][1],
   &this->GroundCoords[3][0], &this->GroundCoords[3][1]);
  current += 192;
Bill Lorensen's avatar
Bill Lorensen committed
222
  sscanf(current, "%24g%24g",
223
224
225
226
   &this->ElevationBounds[0], &this->ElevationBounds[1]);
  elevationConversion = 1.0;
  if (this->ElevationUnitOfMeasure == 1) // feet
    {
Bill Lorensen's avatar
Bill Lorensen committed
227
    elevationConversion = VTK_METERS_PER_FEET;
228
229
230
    }
  else if (this->ElevationUnitOfMeasure == 3) // arc-seconds
    {
Bill Lorensen's avatar
Bill Lorensen committed
231
    elevationConversion = VTK_METERS_PER_ARC_SECOND;
232
233
234
235
    }
  this->ElevationBounds[0] *= elevationConversion;
  this->ElevationBounds[1] *= elevationConversion;
  current += 48;
Bill Lorensen's avatar
Bill Lorensen committed
236
  sscanf(current, "%24g",
237
238
   &this->LocalRotation);
  current += 24;
Bill Lorensen's avatar
Bill Lorensen committed
239
  sscanf(current, "%6d",
240
241
   &this->AccuracyCode);
  current += 6;
242
243
244
245
246
247
248
249
  char buf[13];
  buf[12] = 0;
  strncpy(buf, current, 12);
  sscanf(buf, "%12g", &this->SpatialResolution[0]);
  strncpy(buf, current+12, 12);
  sscanf(buf, "%12g", &this->SpatialResolution[1]);
  strncpy(buf, current+24, 12);
  sscanf(buf, "%12g", &this->SpatialResolution[2]);
250
  current += 36;
Bill Lorensen's avatar
Bill Lorensen committed
251
  sscanf(current, "%6d%6d",
252
253
254
255
256
257
258
259
260
261
262
263
   &this->ProfileDimension[0],
   &this->ProfileDimension[1]);

  this->ProfileSeekOffset = ftell (fp);

  this->ReadHeaderTime.Modified();
  fclose (fp);

  return 0;
}

void vtkDEMReader::ComputeExtentOriginAndSpacing (int extent[6],
Ken Martin's avatar
Ken Martin committed
264
265
                                                  double origin[3],
                                                  double spacing[3])
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
{
  float eastMost, westMost, northMost, southMost;
  float planeConversion;

  //
  // compute number of samples
  //
  eastMost = this->GroundCoords[VTK_NE][0];
  if (eastMost <  this->GroundCoords[VTK_SE][0])
    {
    eastMost = this->GroundCoords[VTK_SE][0];
    }
  westMost = this->GroundCoords[VTK_NW][0];
  if (westMost >  this->GroundCoords[VTK_SW][0])
    {
    westMost = this->GroundCoords[VTK_SW][0];
    }
  northMost = this->GroundCoords[VTK_NE][1];
  if (northMost <  this->GroundCoords[VTK_NW][1])
    {
    northMost = this->GroundCoords[VTK_NW][1];
    }
  southMost = this->GroundCoords[VTK_SW][1];
  if (southMost >  this->GroundCoords[VTK_SE][1])
    {
    southMost = this->GroundCoords[VTK_SE][1];
    }

  //
  // compute the number of rows and columns
  //
  this->NumberOfColumns = (int) ((eastMost - westMost) / this->SpatialResolution[0] + 1.0);
  this->NumberOfRows = (int) ((northMost - southMost) / this->SpatialResolution[1] + 1.0);

  //
  // convert to extent
  //
  extent[0] = 0; extent[1] = this->NumberOfColumns - 1;
  extent[2] = 0; extent[3] = this->NumberOfRows - 1;
  extent[4] = 0; extent[5] = 0;;

  //
  // compute the spacing in meters
  //
  planeConversion = 1.0;
  if (this->PlaneUnitOfMeasure == 1) // feet
    {
Bill Lorensen's avatar
Bill Lorensen committed
313
    planeConversion = VTK_METERS_PER_FEET;
314
315
316
    }
  else if (this->PlaneUnitOfMeasure == 3) // arc-seconds
    {
Bill Lorensen's avatar
Bill Lorensen committed
317
    planeConversion = VTK_METERS_PER_ARC_SECOND;
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    }

  //
  // get the origin from the ground coordinates
  //
  origin[0] = this->GroundCoords[VTK_SW][0];
  origin[1] = this->GroundCoords[VTK_SW][1];
  origin[2] = this->ElevationBounds[0];

  spacing[0] = this->SpatialResolution[0] * planeConversion;
  spacing[1] = this->SpatialResolution[1] * planeConversion;
  spacing[2] = 0;
}

int vtkDEMReader::ReadProfiles (vtkImageData *data)
{
  char record[145];
  float *outPtr, *ptr;
  float elevationExtrema[2];
  float localElevation;
  float planCoords[2];
  float units = this->SpatialResolution[2];
  float elevationConversion;
  float lowPoint;
  int column, row;
  int columnCount;
  int elevation;
  int lastRow;
  int numberOfColumns;
  int profileId[2], profileSize[2];
  int rowId, columnId;
  int updateInterval;
Bill Lorensen's avatar
Bill Lorensen committed
350
  int status = 0;
351
352
  FILE *fp;

353
354
355
356
357
358
  if (!this->FileName)
    {
    vtkErrorMacro(<< "A FileName must be specified.");
    return -1;
    }

359
  if ((fp = fopen(this->FileName, "rb")) == NULL)
360
361
362
363
364
365
366
367
368
369
370
    {
    vtkErrorMacro(<< "File " << this->FileName << " not found");
    return -1;
    }

  vtkDebugMacro (<< "reading profiles");

  // elevation will always be stored in meters
  elevationConversion = 1.0;
  if (this->ElevationUnitOfMeasure == 1) // feet
    {
Bill Lorensen's avatar
Bill Lorensen committed
371
    elevationConversion = VTK_METERS_PER_FEET;
372
373
374
    }
  else if (this->ElevationUnitOfMeasure == 3) // arc-seconds
    {
Bill Lorensen's avatar
Bill Lorensen committed
375
    elevationConversion = VTK_METERS_PER_ARC_SECOND;
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    }

  units *= elevationConversion;
  // seek to start of profiles
  fseek (fp, this->ProfileSeekOffset, SEEK_SET);
  record[120] = '\0';

  // initialize output to the lowest elevation
  lowPoint = this->ElevationBounds[0];
  ptr = outPtr = (float *) data->GetScalarPointer();
  for (int i = 0; i < this->NumberOfColumns * this->NumberOfRows; i++)
    {
    *ptr++ = lowPoint;
    }
  numberOfColumns = this->NumberOfColumns;
  updateInterval = numberOfColumns / 100;
  columnCount = this->ProfileDimension[1];
  for (column = 0; column < columnCount; column++)
    {
    //
    // read four int's
    //
    status = fscanf (fp, "%6d%6d%6d%6d",
399
400
401
402
                   &profileId[0],       /* 1 */
                   &profileId[1],       /* 1 */
                   &profileSize[0],     /* 2 */
                   &profileSize[1]);    /* 2 */
Bill Lorensen's avatar
Bill Lorensen committed
403
404
405
406
    if (status == EOF)
      {
      break;
      }
407
408
409
410
411
412
413
414
    //
    // read the doubles as strings so we can convert floating point format
    //
    (void) fscanf(fp, "%120c", record);
    //
    // convert any D+ or D- to E+ or E-
    //
    ConvertDNotationToENotation (record);
Jim Miller's avatar
Jim Miller committed
415
    sscanf(record, "%24g%24g%24g%24g%24g",
416
417
418
419
420
                   &planCoords[0],      /* 3 */
                   &planCoords[1],      /* 3 */
                   &localElevation,     /* 4 */
                   &elevationExtrema[0],        /* 5 */
                   &elevationExtrema[1]);       /* 5 */
421
422
423
424
425
426
427
428
    rowId = profileId[0] - 1;
    columnId = profileId[1] - 1;
    lastRow = rowId + profileSize[0] - 1;
    // report progress at the start of each column
    if (column % updateInterval == 0)
      {
      this->UpdateProgress ((float) column / ((float) columnCount - 1));
      if (this->GetAbortExecute())
429
430
431
        {
        break;
        }
432
433
434
435
436
437
438
439
440
441
      }
    // read a column
    for (row = rowId; row <= lastRow; row++)
      {
      (void) fscanf(fp, "%6d", &elevation);
      *(outPtr + columnId + row * numberOfColumns) = elevation * units;
      }
    }
  fclose (fp);

Bill Lorensen's avatar
Bill Lorensen committed
442
  return status;
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
}

// Description: Converts Fortran D notation to C++ e notation
void ConvertDNotationToENotation (char *line)
{
  char *ptr = line;

  // first convert D+ to E+
  while (*ptr && (ptr = strstr (ptr, "D+")))
    {
    *ptr = 'e'; ptr++;
    *ptr = '+'; ptr++;
    }

  // first now D- to E-
  ptr = line;
  while (*ptr && (ptr = strstr (ptr, "D-")))
    {
    *ptr = 'e'; ptr++;
    *ptr = '-'; ptr++;
    }
}    

466
void vtkDEMReader::PrintSelf(ostream& os, vtkIndent indent)
467
{
Brad King's avatar
Brad King committed
468
  this->Superclass::PrintSelf(os,indent);
469
470
471
472
473

  os << indent << "File Name: " 
     << (this->FileName ? this->FileName : "(none)") << "\n";
  if (this->FileName)
    {
474
    this->UpdateInformation ();
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    os << indent << "MapLabel: " << this->MapLabel << "\n";
    os << indent << "DEMLevel: " << this->DEMLevel << "\n";
    os << indent << "ElevationPattern: " << this->ElevationPattern
       << (this->ElevationPattern == 1 ? " (regular)" : " (random)") << "\n";
    os << indent << "GroundSystem: " << this->GroundSystem;
    if (this->GroundSystem == 0)
      {
      os << " (Geographic)\n";
      }
    else if (this->GroundSystem == 1)
      {
      os << " (UTM)\n";
      }
    else if (this->GroundSystem == 2)
      {
      os << " (State plane)\n";
      }
    else
      {
      os << " (unknown)\n";
      }
    os << indent << "GroundZone: " << this->GroundZone << "\n";
Bill Lorensen's avatar
Bill Lorensen committed
497
    os << indent << "ProjectionParameters: all zero" << "\n"; // this->ProjectionParameters
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    os << indent << "PlaneUnitOfMeasure: " << this->PlaneUnitOfMeasure;
    if (this->PlaneUnitOfMeasure == 0)
      {
      os << indent << " (radians)\n";
      }
    else if (this->PlaneUnitOfMeasure == 1)
      {
      os << indent << " (feet)\n";
      }
    else if (this->PlaneUnitOfMeasure == 2)
      {
      os << indent << " (meters)\n";
      }
    else if (this->PlaneUnitOfMeasure == 3)
      {
      os << indent << " (arc-seconds)\n";
      }
    else
      {
      os << indent << " (unknown)\n";
      }
  
    os << indent << "ElevationUnitOfMeasure: " << this->ElevationUnitOfMeasure;
    if (this->ElevationUnitOfMeasure == 1)
      {
      os << indent << " (feet)\n";
      }
    else if (this->ElevationUnitOfMeasure == 2)
      {
      os << indent << " (meters)\n";
      }
    else
      {
      os << indent << " (unknown)\n";
      }
    os << indent << "PolygonSize: " << this->PolygonSize << "\n";
    os << indent << "GroundCoordinates: \n";
Bill Lorensen's avatar
Bill Lorensen committed
535
536
537
538
    os << indent << "        " << this->GroundCoords[0][0] << ", " << this->GroundCoords[0][1] << "\n";
    os << indent << "        " << this->GroundCoords[1][0] << ", " << this->GroundCoords[1][1] << "\n";
    os << indent << "        " << this->GroundCoords[2][0] << ", " << this->GroundCoords[2][1] << "\n";
    os << indent << "        " << this->GroundCoords[3][0] << ", " << this->GroundCoords[3][1] << "\n";
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  
    os << indent << "ElevationBounds: " << this->ElevationBounds[0] << ", "
                                        << this->ElevationBounds[1]
                                        << " (meters)\n";
    os << indent << "LocalRotation: " << this->LocalRotation << "\n";
    os << indent << "AccuracyCode: " << this->AccuracyCode << "\n";
    os << indent << "SpatialResolution: " << this->SpatialResolution[0] << ", "
                                          << this->SpatialResolution[1];
    if (this->PlaneUnitOfMeasure == 0)
      {
      os << indent << "(radians)";
      }
    else if (this->PlaneUnitOfMeasure == 1)
      {
      os << indent << "(feet)";
      }
    else if (this->PlaneUnitOfMeasure == 2)
      {
      os << indent << "(meters)";
      }
    else if (this->PlaneUnitOfMeasure == 3)
      {
      os << indent << "(arc-seconds)";
      }
    else
      {
      os << indent << " (unknown)\n";
      }
  
    os << indent << this->SpatialResolution[2];
    if (this->ElevationUnitOfMeasure == 1)
      {
      os << indent << "(feet)\n";
      }
    else if (this->ElevationUnitOfMeasure == 2)
      {
      os << indent << "(meters)\n";
      }
    else
      {
      os << indent << "(unknown)\n";
      }
    os << indent << "ProfileDimension: " << this->ProfileDimension[0] << ", "
                                         << this->ProfileDimension[1] << "\n";
  }
}