Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ElevationGrid.h
1 //
2 // vtElevationGrid.h
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project.
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef ELEVATIONGRIDH
9 #define ELEVATIONGRIDH
10 
11 #include "MathTypes.h"
12 #include "Projections.h"
13 #include "LocalConversion.h"
14 #include "HeightField.h"
15 #include "vtString.h"
16 
17 class vtDIB;
18 class OGRDataSource;
19 
20 enum vtElevError {
21  EGE_FILE_OPEN,
22  EGE_NOT_FORMAT,
23  EGE_UNSUPPORTED_VERSION,
24  EGE_READ_CRS,
25  EGE_READ_DATA,
26  EGE_CANCELLED
27 };
28 
43 {
44 public:
46  vtElevationGrid(const vtElevationGrid &rhs);
47  vtElevationGrid(const DRECT &area, int iColumns, int iRows, bool bFloat,
48  const vtProjection &proj);
49  virtual ~vtElevationGrid();
50 
52 
53  bool CopyFrom(const vtElevationGrid &rhs);
54  bool CopyHeaderFrom(const vtElevationGrid &rhs);
55  bool CopyDataFrom(const vtElevationGrid &rhs);
56 
57  bool Create(const DRECT &area, int iColumns, int iRows, bool bFloat,
58  const vtProjection &proj);
59  void FreeData();
60 
61  void Clear();
62  void Invalidate();
63  bool ConvertProjection(vtElevationGrid *pOld, const vtProjection &NewProj,
64  float bUpgradeToFloat, bool progress_callback(int) = NULL);
65  bool ReprojectExtents(const vtProjection &proj_new);
66  void Scale(float fScale, bool bDirect, bool bRecomputeExtents = true);
67  void VertOffset(float fAmount);
68  void ComputeHeightExtents();
69  void Offset(const DPoint2 &delta);
70  int ReplaceValue(float value1, float value2);
71  bool FillGaps(DRECT *area = NULL, bool progress_callback(int) = NULL);
72  bool FillGapsSmooth(DRECT *area = NULL, bool progress_callback(int) = NULL);
73 
74  int FillGapsByRegionGrowing(int radius_start=2, int radius_stop=5, bool progress_callback(int) = NULL);
75  int FillGapsByRegionGrowing(int radius, bool progress_callback(int) = NULL);
76 
77  // Load from unknown file format
78  bool LoadFromFile( const char *szFileName, bool progress_callback(int) = NULL);
79 
80  // Load from a specific kind of file
81  bool LoadFrom3TX(const char *szFileName, bool progress_callback(int) = NULL);
82  bool LoadFromASC(const char *szFileName, bool progress_callback(int) = NULL);
83  bool LoadFromDEM(const char *szFileName, bool progress_callback(int) = NULL);
84  bool LoadFromTerragen(const char *szFileName, bool progress_callback(int) = NULL);
85  bool LoadFromCDF(const char *szFileName, bool progress_callback(int) = NULL);
86  bool LoadFromDTED(const char *szFileName, bool progress_callback(int) = NULL);
87  bool LoadFromGTOPO30(const char *szFileName, bool progress_callback(int) = NULL);
88  bool LoadFromGLOBE(const char *szFileName, bool progress_callback(int) = NULL);
89  bool LoadFromGRD(const char *szFileName, bool progress_callback(int) = NULL);
90  bool LoadFromDSAA(const char* szFileName, bool progress_callback(int) = NULL);
91  bool LoadFromPGM(const char *szFileName, bool progress_callback(int) = NULL);
92  bool LoadFromRAW(const char *szFileName, int width, int height,
93  int bytes_per_element, float vertical_units, bool bBigEndian,
94  bool progress_callback(int));
95  bool LoadFromMicroDEM(const char *szFileName, bool progress_callback(int) = NULL);
96  bool LoadFromXYZ(const char *szFileName, bool progress_callback(int) = NULL);
97  bool LoadFromXYZ(FILE *fp, const char *format, bool progress_callback(int) = NULL);
98  bool LoadFromHGT(const char *szFileName, bool progress_callback(int) = NULL);
99  bool LoadFromBT(const char *szFileName, bool progress_callback(int) = NULL,
100  vtElevError *err = NULL);
101  bool LoadBTHeader(const char *szFileName, vtElevError *err = NULL);
102  bool LoadBTData(const char *szFileName, bool progress_callback(int) = NULL,
103  vtElevError *err = NULL);
104 
105  // Use GDAL to read a file
106  bool LoadWithGDAL(const char *szFileName, bool progress_callback(int) = NULL);
107 
108  // Use OGR to read a file
109  bool LoadFromNTF5(const char *szFileName, bool progress_callback(int) = NULL);
110 
111  // If there was a load error, this method should return a description
112  vtString GetErrorMsg() { return m_strError; }
113 
114  // Save
115  bool SaveTo3TX(const char *szFileName, bool progress_callback(int) = NULL);
116  bool SaveToGeoTIFF(const char *szFileName) const;
117  bool SaveToBMP(const char *szFileName) const;
118  bool SaveToTerragen(const char *szFileName) const;
119  bool SaveToBT(const char *szFileName, bool progress_callback(int) = NULL, bool bGZip = false);
120  bool SaveToSTM(const char *szFileName, bool progress_callback(int) = NULL);
121  bool SaveToPlanet(const char *szDirName, bool progress_callback(int) = NULL);
122  bool SaveToASC(const char *szFileName, bool progress_callback(int) = NULL) const;
123  bool SaveToVRML(const char *szFileName, bool progress_callback(int) = NULL) const;
124  bool SaveToXYZ(const char *szFileName, bool progress_callback(int) = NULL) const;
125  bool SaveToRAWINF(const char *szFileName, bool progress_callback(int) = NULL) const;
126  bool SaveToPNG16(const char *fname);
127 
128  // Set/Get height values
129  void SetFValue(int i, int j, float value);
130  void SetValue(int i, int j, short value);
131  short GetShortValue(int i, int j) const; // returns height value as a short integer
132  float GetFValue(int i, int j) const; // returns height value as a float
133  float GetFValueSafe(int i, int j) const;
134 
135  float GetClosestValue(const DPoint2 &p) const;
136  float GetFilteredValue(const DPoint2 &p) const;
137 
138  // Accessors
140  const char *GetDEMName() { return m_strOriginalDEMName; }
141 
143  DRECT GetAreaExtents() const;
144 
148  bool IsFloatMode() const { return m_bFloatMode; }
149 
150  void FillWithSingleValue(float fValue);
151  void GetEarthPoint(int i, int j, DPoint2 &p) const;
152  void GetEarthLocation(int i, int j, DPoint3 &loc) const;
153 
154  vtProjection &GetProjection() { return m_proj; }
155  const vtProjection &GetProjection() const { return m_proj; }
156  void SetProjection(const vtProjection &proj);
157 
158  bool GetCorners(DLine2 &line, bool bGeo) const;
159  void SetCorners(const DLine2 &line);
160 
161  short *GetData() { return m_pData; }
162  float *GetFloatData() { return m_pFData; }
163 
164  const short *GetData() const { return m_pData; }
165  const float *GetFloatData() const { return m_pFData; }
166 
167  void SetScale(float sc) { m_fVMeters = sc; }
168  float GetScale() const { return m_fVMeters; }
169 
170  bool HasData() const { return (m_pData != NULL || m_pFData != NULL); }
171  int MemoryNeededToLoad() const { return m_iColumns * m_iRows * (m_bFloatMode ? 4 : 2); }
172  int MemoryUsed() const { if (m_pData) return m_iColumns * m_iRows * 2;
173  else if (m_pFData) return m_iColumns * m_iRows * 4;
174  else return 0; }
175 
176  // Implement vtHeightField methods
177  bool FindAltitudeOnEarth(const DPoint2 &p, float &fAltitude, bool bTrue = false) const;
178 
179  // Implement vtHeightField3d methods
180  virtual float GetElevation(int iX, int iZ, bool bTrue = false) const;
181  virtual void GetWorldLocation(int i, int j, FPoint3 &loc, bool bTrue = false) const;
182 
183  // methods that deal with world coordinates
184  void SetupConversion(float fVerticalExag = 1.0f);
185  float GetWorldValue(int i, int j, bool bTrue = false) const;
186  bool FindAltitudeAtPoint(const FPoint3 &p3, float &fAltitude,
187  bool bTrue = false, int iCultureFlags = 0,
188  FPoint3 *vNormal = NULL) const;
189 
190 protected:
191  bool m_bFloatMode;
192  short *m_pData;
193  float *m_pFData;
194  float m_fVMeters; // scale factor to convert stored heights to meters
195  float m_fVerticalScale;
196 
197  void SetupMembers();
200  bool ParseNTF5(OGRDataSource *pDatasource, vtString &msg, bool progress_callback(int));
201  bool GetXYZLine(const char *buf, const char *pattern, const char *format,
202  int components, double *x, double *y, double *z);
203 
204  DPoint2 m_Corners[4]; // data corners, in the CRS of this terrain
205  vtProjection m_proj; // a grid always has some projection
206 
207  bool _AllocateArray();
208  vtString m_strOriginalDEMName;
209  vtString m_strError;
210 };
211 
212 #endif // ELEVATIONGRIDH
213