OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimDemUtil.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: MIT
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Ken Melero
9 // Orginally written by Jamie Moyers (jmoyers@geeks.com)
10 // Adapted from the package KDEM.
11 // Description: This class provides some simple utilities for DEMs.
12 //
13 //********************************************************************
14 // $Id$
15 
20 #include <ossim/base/ossimNotify.h>
22 #include <cstring>
23 #include <iostream>
24 
25 static const char DEM_TYPE_KW[] = "dem_type";
26 static const char USGS_DEM_KW[] = "usgs_dem";
27 
29 {
30  bool result = false;
31 
32  ossimString ext = file.ext();
33  ext.downcase();
34  if (ext == "dem")
35  {
36  result = true;
37  }
38  else
39  {
40  // Look for file.omd
41  ossimFilename kwlFile = file;
42  kwlFile.setExtension("omd");
43  if (! kwlFile.exists() )
44  {
45  kwlFile.setExtension("OMD");
46  }
47 
48  if ( kwlFile.exists() )
49  {
50  ossimKeywordlist kwl(kwlFile);
52  {
53  const char* lookup = kwl.find(DEM_TYPE_KW);
54  if (lookup)
55  {
56  ossimString s = lookup;
57  s.downcase();
58  if (s == USGS_DEM_KW)
59  {
60  result = true;
61  }
62  }
63  }
64  }
65  }
66 
67  if ( result )
68  {
69 
70  // Open up the file for reading.
71  // std::shared_ptr<ossim::istream> is = ossim::StreamFactoryRegistry::instance()->
72  // createIstream(file, std::ios_base::in | std::ios_base::binary);
73  }
74 
75  return result;
76 }
77 
78 bool ossimDemUtil::isUsgsDem(std::shared_ptr<ossim::istream> str,
79  const std::string& connectionString)
80 {
81  bool result = false;
82  ossimFilename tempFile = connectionString;
83  ossimString ext = tempFile.ext();
84  ext.downcase();
85  if (ext == "dem")
86  {
87  result = true;
88  }
89  else
90  {
91  // Look for file.omd
92  ossimFilename kwlFile = tempFile;
93  kwlFile.setExtension("omd");
94  if (! kwlFile.exists() )
95  {
96  kwlFile.setExtension("OMD");
97  }
98 
99  if ( kwlFile.exists() )
100  {
101  ossimKeywordlist kwl(kwlFile);
103  {
104  const char* lookup = kwl.find(DEM_TYPE_KW);
105  if (lookup)
106  {
107  ossimString s = lookup;
108  s.downcase();
109  if (s == USGS_DEM_KW)
110  {
111  result = true;
112  }
113  }
114  }
115  }
116  }
117  if(result&&str&&str->good())
118  {
119  //---
120  // SPECIAL HACK (drb):
121  // Check some bytes and make sure there is no binary data.
122  // There are files out there with .dem extension that are binary
123  // rasters.
124  //---
125  ossim_uint8* ubuf = new ossim_uint8[512];
126  str->read((char*)ubuf, 512);
127  for (int i = 0; i < 512; ++i)
128  {
129  if (ubuf[i] > 0x7f)
130  {
131  result = false;
132  break;
133  }
134  }
135  delete [] ubuf;
136  ubuf = 0;
137  }
138  else
139  {
140  result = false;
141  }
142 
143  return result;
144 }
145 
146 bool ossimDemUtil::getRecord(ossim::istream& s, std::string& strbuf, long reclength)
147 {
148  char* buf = new char[reclength + 1];
149 
150  bool flag = ossimDemUtil::getRecord(s,buf,reclength);
151  if (flag == true)
152  strbuf = buf;
153 
154  delete [] buf;
155 
156  return flag;
157 }
158 
159 bool ossimDemUtil::getRecord(ossim::istream& s, char* buf, long reclength)
160 {
161  // buf is assumed to be at least reclength+1 in size.
162 
163  if (!s)
164  return false;
165 
166  long curpos = 0;
167  buf[curpos] = s.get();
168  while ((buf[curpos] != EOF) &&
169  (buf[curpos] != '\n') &&
170  (curpos < reclength-1))
171  {
172  curpos++;
173  buf[curpos] = s.get();
174  }
175  buf[curpos] = '\0';
176 
177  if (s.peek() == '\n')
178  s.get();
179 
180  return true;
181 
182 }
183 
184 bool
185 ossimDemUtil::getDouble(std::string const& strbuf,
186  long const startpos,
187  long const width,
188  double& val)
189 {
190  if ((startpos + width - 1) > (long)(strbuf.length()))
191  return false;
192 
193  // Convert FORTRAN 'D' exponent indicator to 'E'.
194  std::string tempbuf(strbuf.substr(startpos,width));
195  for (unsigned int i = 0; i < tempbuf.length(); i++)
196  if (tempbuf[i] == 'D')
197  tempbuf[i] = 'E';
198 
199  val = atof(tempbuf.c_str());
200  return true;
201 }
202 
203 long ossimDemUtil::getLong(char* const strbuf, // string to extract long from
204  long const startpos, // starting position of field
205  long const width) // width of field
206 {
207  char temp[1024];
208  std::strncpy(temp,strbuf+startpos,width);
209  temp[width] = '\0';
210  return atol(temp);
211 }
Represents serializable keyword/value map.
static const ossimErrorCode OSSIM_OK
const char * find(const char *key) const
static long getLong(char *const strbuf, long const startpos, long const width)
static bool isUsgsDem(const ossimFilename &file)
Does basic sanity checks to see if file is a dem.
bool exists() const
static bool getDouble(std::string const &strbuf, long const startpos, long const width, double &val)
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
virtual ossimErrorCode getErrorStatus() const
ossimString ext() const
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
static bool getRecord(ossim::istream &s, std::string &strbuf, long reclength=1024)
Reads a single record from a DEM.
unsigned char ossim_uint8