Debian Codegraph @

frmts/srtmhgt/srtmhgtdataset.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Project: SRTM HGT Driver
4 * Purpose: SRTM HGT File Read Support.
5 * http://dds.cr.usgs.gov/srtm/version2_1/Documentation/SRTM_Topo.pdf
6 * http://www2.jpl.nasa.gov/srtm/faq.html
7 * http://dds.cr.usgs.gov/srtm/version2_1
8 * Authors: Michael Mazzella, Even Rouault
9 *
10 ******************************************************************************
11 * Copyright (c) 2005, Frank Warmerdam <[email protected]>
12 * Copyright (c) 2007-2011, Even Rouault <even dot rouault at spatialys.com>
13 *
14 * SPDX-License-Identifier: MIT
15 ****************************************************************************/
16
17 #include "cpl_port.h"cxx . . $ `<file>/port/cpl_port.h`/
18 #include "cpl_string.h"cxx . . $ `<file>/port/cpl_string.h`/
19 #include "gdal_frmts.h"cxx . . $ `<file>/gcore/gdal_frmts.h`/
20 #include "gdal_pam.h"cxx . . $ `<file>/gcore/gdal_pam.h`/
21 #include "gdal_driver.h"cxx . . $ `<file>/gcore/gdal_driver.h`/
22 #include "gdal_drivermanager.h"cxx . . $ `<file>/gcore/gdal_drivermanager.h`/
23 #include "gdal_openinfo.h"cxx . . $ `<file>/gcore/gdal_openinfo.h`/
24 #include "gdal_cpp_functions.h"cxx . . $ `<file>/gcore/gdal_cpp_functions.h`/
25 #include "ogr_spatialref.h"cxx . . $ `<file>/ogr/ogr_spatialref.h`/
26
27 #include <cmath>
28
29 constexpr GInt16cxx . . $ GInt16# SRTMHG_NODATA_VALUEcxx . . $ SRTMHG_NODATA_VALUE. = -32768;
30
31 /************************************************************************/
32 /* ==================================================================== */
33 /* SRTMHGTDataset */
34 /* ==================================================================== */
35 /************************************************************************/
36
37 class SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#;
38
39 class SRTMHGTDatasetcxx . . $ SRTMHGTDataset# final : public GDALPamDatasetcxx . . $ GDALPamDataset#
40 {
41 friend class SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#;
42
43 VSILFILE *fpImagecxx . . $ SRTMHGTDataset#fpImage. = nullptr;
44 GDALGeoTransformcxx . . $ GDALGeoTransform# m_gtcxx . . $ SRTMHGTDataset#m_gt.{cxx . . $ GDALGeoTransform#GDALGeoTransform(ced63f7c635d850d).};
45 GBytecxx . . $ GByte# *pabyBuffercxx . . $ SRTMHGTDataset#pabyBuffer. = nullptr;
46 OGRSpatialReferencecxx . . $ OGRSpatialReference# m_oSRScxx . . $ SRTMHGTDataset#m_oSRS.{cxx . . $ OGRSpatialReference#OGRSpatialReference(85c52e162fed56f9).};
47
48 static GDALPamDatasetcxx . . $ GDALPamDataset# *OpenPAMcxx . . $ SRTMHGTDataset#OpenPAM(2bc92ff7293b0d84).(GDALOpenInfocxx . . $ GDALOpenInfo# *);
49
50 public:
51 SRTMHGTDatasetcxx . . $ SRTMHGTDataset#SRTMHGTDataset(49f6e7a06ebc5aa8).();
52 ~cxx . . $ SRTMHGTDataset#`~SRTMHGTDataset`(ced63f7c635d850d).SRTMHGTDatasetcxx . . $ SRTMHGTDataset#() override;
53
54 const OGRSpatialReferencecxx . . $ OGRSpatialReference# *GetSpatialRefcxx . . $ SRTMHGTDataset#GetSpatialRef(8a5496c2eeefaeaf).() const override;
55 CPLErr GetGeoTransformcxx . . $ SRTMHGTDataset#GetGeoTransform(9cfa0773fdb41a90).(GDALGeoTransformcxx . . $ GDALGeoTransform# &gt) const override;
56
57 static int Identifycxx . . $ SRTMHGTDataset#Identify(48f695ac6b30cd84).(GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo);
58 static GDALDatasetcxx . . $ GDALDataset# *Opencxx . . $ SRTMHGTDataset#Open(905b7966ba8c1649).(GDALOpenInfocxx . . $ GDALOpenInfo# *);
59 static GDALDatasetcxx . . $ GDALDataset# *CreateCopycxx . . $ SRTMHGTDataset#CreateCopy(8d8ece3624d4575).(const char *pszFilename,
60 GDALDatasetcxx . . $ GDALDataset# *poSrcDS, int bStrict,
61 CSLConstListcxx . . $ CSLConstList# papszOptions,
62 GDALProgressFunc pfnProgress,
63 void *pProgressData);
64 };
65
66 /************************************************************************/
67 /* ==================================================================== */
68 /* SRTMHGTRasterBand */
69 /* ==================================================================== */
70 /************************************************************************/
71
72 class SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand# final : public GDALPamRasterBandcxx . . $ GDALPamRasterBand#
73 {
74 friend class SRTMHGTDatasetcxx . . $ SRTMHGTDataset#;
75
76 int bNoDataSetcxx . . $ SRTMHGTRasterBand#bNoDataSet.;
77 double dfNoDataValuecxx . . $ SRTMHGTRasterBand#dfNoDataValue.;
78
79 public:
80 SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#SRTMHGTRasterBand(470396b4cd8290bf).(SRTMHGTDatasetcxx . . $ SRTMHGTDataset# *, int, GDALDataType);
81
82 CPLErr IReadBlockcxx . . $ SRTMHGTRasterBand#IReadBlock(282c12597d894d0d).(int, int, void *) override;
83 virtual CPLErr IWriteBlockcxx . . $ SRTMHGTRasterBand#IWriteBlock(282c12597d894d0d).(int nBlockXOff, int nBlockYOff,
84 void *pImage) override;
85
86 GDALColorInterp GetColorInterpretationcxx . . $ SRTMHGTRasterBand#GetColorInterpretation(35c4b535829078b5).() override;
87
88 double GetNoDataValuecxx . . $ SRTMHGTRasterBand#GetNoDataValue(3e6f4119bf25dad8).(int *pbSuccess = nullptr) override;
89
90 const char *GetUnitTypecxx . . $ SRTMHGTRasterBand#GetUnitType(33a1f659e94feb42).() override;
91 };
92
93 /************************************************************************/
94 /* SRTMHGTRasterBand() */
95 /************************************************************************/
96
97 SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#::SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#SRTMHGTRasterBand(470396b4cd8290bf).(SRTMHGTDatasetcxx . . $ SRTMHGTDataset# *poDSIn, int nBandIn,
98 GDALDataType eDT)
99 : bNoDataSetcxx . . $ SRTMHGTRasterBand#bNoDataSet.(TRUEcxx . . $ `port/cpl_port.h:1184:9`!), dfNoDataValuecxx . . $ SRTMHGTRasterBand#dfNoDataValue.(SRTMHG_NODATA_VALUEcxx . . $ SRTMHG_NODATA_VALUE.)
100 {
101 poDScxx . . $ GDALRasterBand#poDS. = poDSIn;
102 nBandcxx . . $ GDALRasterBand#nBand. = nBandIn;
103 eDataTypecxx . . $ GDALRasterBand#eDataType. = eDT;
104 nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize. = poDSIn->nRasterXSizecxx . . $ GDALDataset#nRasterXSize.;
105 nBlockYSizecxx . . $ GDALRasterBand#nBlockYSize. = 1;
106 }
107
108 /************************************************************************/
109 /* IReadBlock() */
110 /************************************************************************/
111
112 CPLErr SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#::IReadBlockcxx . . $ SRTMHGTRasterBand#IReadBlock(282c12597d894d0d).(int /*nBlockXOff*/, int nBlockYOff,
113 void *pImage)
114 {
115 SRTMHGTDatasetcxx . . $ SRTMHGTDataset# *poGDS = cpl::down_cast<SRTMHGTDatasetcxx . . $ SRTMHGTDataset# *>(poDScxx . . $ GDALRasterBand#poDS.);
116
117 /* -------------------------------------------------------------------- */
118 /* Load the desired data into the working buffer. */
119 /* -------------------------------------------------------------------- */
120 const int nDTSize = GDALGetDataTypeSizeBytes(eDataTypecxx . . $ GDALRasterBand#eDataType.);
121 VSIFSeekL(poGDS->fpImagecxx . . $ SRTMHGTDataset#fpImage.,
122 static_cast<vsi_l_offset>(nBlockYOff) * nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize. * nDTSize,
123 SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:114:9`!);
124 VSIFReadL((unsigned char *)pImage, nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize., nDTSize, poGDS->fpImagecxx . . $ SRTMHGTDataset#fpImage.);
125 #ifdef CPL_LSBcxx . . $ `port/cpl_port.h:608:9`!
126 GDALSwapWords(pImage, nDTSize, nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize., nDTSize);
127 #endif
128
129 return CE_None;
130 }
131
132 /************************************************************************/
133 /* IWriteBlock() */
134 /************************************************************************/
135
136 CPLErr SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#::IWriteBlockcxx . . $ SRTMHGTRasterBand#IWriteBlock(282c12597d894d0d).(int /*nBlockXOff*/, int nBlockYOff,
137 void *pImage)
138 {
139 SRTMHGTDatasetcxx . . $ SRTMHGTDataset# *poGDS = cpl::down_cast<SRTMHGTDatasetcxx . . $ SRTMHGTDataset# *>(poDScxx . . $ GDALRasterBand#poDS.);
140
141 if (poGDS->eAccesscxx . . $ GDALDataset#eAccess. != GA_Update)
142 return CE_Failure;
143
144 const int nDTSize = GDALGetDataTypeSizeBytes(eDataTypecxx . . $ GDALRasterBand#eDataType.);
145 VSIFSeekL(poGDS->fpImagecxx . . $ SRTMHGTDataset#fpImage.,
146 static_cast<vsi_l_offset>(nBlockYOff) * nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize. * nDTSize,
147 SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:114:9`!);
148
149 #ifdef CPL_LSBcxx . . $ `port/cpl_port.h:608:9`!
150 if (nDTSize > 1)
151 {
152 memcpy(poGDS->pabyBuffercxx . . $ SRTMHGTDataset#pabyBuffer., pImage,
153 static_cast<size_t>(nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize.) * nDTSize);
154 GDALSwapWords(poGDS->pabyBuffercxx . . $ SRTMHGTDataset#pabyBuffer., nDTSize, nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize., nDTSize);
155 VSIFWriteL(reinterpret_cast<unsigned char *>(poGDS->pabyBuffercxx . . $ SRTMHGTDataset#pabyBuffer.),
156 nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize., nDTSize, poGDS->fpImagecxx . . $ SRTMHGTDataset#fpImage.);
157 }
158 else
159 #endif
160 {
161 VSIFWriteL(reinterpret_cast<unsigned char *>(pImage), nBlockXSizecxx . . $ GDALRasterBand#nBlockXSize.,
162 nDTSize, poGDS->fpImagecxx . . $ SRTMHGTDataset#fpImage.);
163 }
164
165 return CE_None;
166 }
167
168 /************************************************************************/
169 /* GetNoDataValue() */
170 /************************************************************************/
171
172 double SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#::GetNoDataValuecxx . . $ SRTMHGTRasterBand#GetNoDataValue(3e6f4119bf25dad8).(int *pbSuccess)
173
174 {
175 if (eDataTypecxx . . $ GDALRasterBand#eDataType. == GDT_UInt8)
176 return GDALPamRasterBandcxx . . $ GDALPamRasterBand#::GetNoDataValuecxx . . $ GDALPamRasterBand#GetNoDataValue(3e6f4119bf25dad8).(pbSuccess);
177
178 if (pbSuccess)
179 *pbSuccess = bNoDataSetcxx . . $ SRTMHGTRasterBand#bNoDataSet.;
180
181 return dfNoDataValuecxx . . $ SRTMHGTRasterBand#dfNoDataValue.;
182 }
183
184 /************************************************************************/
185 /* GetUnitType() */
186 /************************************************************************/
187
188 const char *SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#::GetUnitTypecxx . . $ SRTMHGTRasterBand#GetUnitType(33a1f659e94feb42).()
189 {
190 const stdcxx . . $ std/::string osExt = CPLGetExtensionSafe(poDScxx . . $ GDALRasterBand#poDS.->GetDescriptioncxx . . $ GDALMajorObject#GetDescription(a9f41ea0e82d88cf).());
191 const char *pszExt = osExt.c_str();
192 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszExt, "err") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszExt, "img") || EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszExt, "num") ||
193 EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszExt, "swb"))
194 {
195 return "";
196 }
197 return "m";
198 }
199
200 /************************************************************************/
201 /* GetColorInterpretation() */
202 /************************************************************************/
203
204 GDALColorInterp SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#::GetColorInterpretationcxx . . $ SRTMHGTRasterBand#GetColorInterpretation(35c4b535829078b5).()
205 {
206 return GCI_Undefined;
207 }
208
209 /************************************************************************/
210 /* ==================================================================== */
211 /* SRTMHGTDataset */
212 /* ==================================================================== */
213 /************************************************************************/
214
215 /************************************************************************/
216 /* SRTMHGTDataset() */
217 /************************************************************************/
218
219 SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::SRTMHGTDatasetcxx . . $ SRTMHGTDataset#SRTMHGTDataset(49f6e7a06ebc5aa8).()
220 {
221 m_oSRScxx . . $ SRTMHGTDataset#m_oSRS..SetAxisMappingStrategycxx . . $ OGRSpatialReference#SetAxisMappingStrategy(3d39f43974738d6a).(OAMS_TRADITIONAL_GIS_ORDER);
222 if (CPLTestBool(CPLGetConfigOption("REPORT_COMPD_CS", "NO")))
223 {
224 m_oSRScxx . . $ SRTMHGTDataset#m_oSRS..importFromWktcxx . . $ OGRSpatialReference#importFromWkt(5c4306d8553b3696).(
225 "COMPD_CS[\"WGS 84 + EGM96 geoid height\", GEOGCS[\"WGS 84\", "
226 "DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\",6378137,298.257223563, "
227 "AUTHORITY[\"EPSG\",\"7030\"]], AUTHORITY[\"EPSG\",\"6326\"]], "
228 "PRIMEM[\"Greenwich\",0, AUTHORITY[\"EPSG\",\"8901\"]], "
229 "UNIT[\"degree\",0.0174532925199433, "
230 "AUTHORITY[\"EPSG\",\"9122\"]], AUTHORITY[\"EPSG\",\"4326\"]], "
231 "VERT_CS[\"EGM96 geoid height\", VERT_DATUM[\"EGM96 geoid\",2005, "
232 "AUTHORITY[\"EPSG\",\"5171\"]], UNIT[\"metre\",1, "
233 "AUTHORITY[\"EPSG\",\"9001\"]], AXIS[\"Up\",UP], "
234 "AUTHORITY[\"EPSG\",\"5773\"]]]");
235 }
236 else
237 {
238 m_oSRScxx . . $ SRTMHGTDataset#m_oSRS..importFromWktcxx . . $ OGRSpatialReference#importFromWkt(5c4306d8553b3696).(SRS_WKT_WGS84_LAT_LONGcxx . . $ `ogr/ogr_srs_api.h:63:9`!);
239 }
240 }
241
242 /************************************************************************/
243 /* ~SRTMHGTDataset() */
244 /************************************************************************/
245
246 SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::~cxx . . $ SRTMHGTDataset#`~SRTMHGTDataset`(ced63f7c635d850d).SRTMHGTDatasetcxx . . $ SRTMHGTDataset#()
247 {
248 FlushCachecxx . . $ GDALPamDataset#FlushCache(426a86739463d040).(true);
249 if (fpImagecxx . . $ SRTMHGTDataset#fpImage. != nullptr)
250 VSIFCloseL(fpImagecxx . . $ SRTMHGTDataset#fpImage.);
251 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pabyBuffercxx . . $ SRTMHGTDataset#pabyBuffer.);
252 }
253
254 /************************************************************************/
255 /* GetGeoTransform() */
256 /************************************************************************/
257
258 CPLErr SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::GetGeoTransformcxx . . $ SRTMHGTDataset#GetGeoTransform(9cfa0773fdb41a90).(GDALGeoTransformcxx . . $ GDALGeoTransform# &gt) const
259 {
260 gt =cxx . . $ GDALGeoTransform#`operator=`(341f2d3a242b8b78). m_gtcxx . . $ SRTMHGTDataset#m_gt.;
261 return CE_None;
262 }
263
264 /************************************************************************/
265 /* GetSpatialRef() */
266 /************************************************************************/
267
268 const OGRSpatialReferencecxx . . $ OGRSpatialReference# *SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::GetSpatialRefcxx . . $ SRTMHGTDataset#GetSpatialRef(8a5496c2eeefaeaf).() const
269
270 {
271 return &m_oSRScxx . . $ SRTMHGTDataset#m_oSRS.;
272 }
273
274 /************************************************************************/
275 /* Identify() */
276 /************************************************************************/
277
278 int SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::Identifycxx . . $ SRTMHGTDataset#Identify(48f695ac6b30cd84).(GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo)
279
280 {
281 const char *fileName = CPLGetFilename(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
282 if (strlen(fileName) < 11 || fileName[7] != '.')
283 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
284 CPLString osLCFilename(CPLString(fileName).tolower());
285 if ((osLCFilename[0] != 'n' && osLCFilename[0] != 's') ||
286 (osLCFilename[3] != 'e' && osLCFilename[3] != 'w'))
287 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
288 if (!STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(fileName, "/vsizip/") && osLCFilename.endsWith(".hgt.zip"))
289 {
290 CPLString osNewName("/vsizip/");
291 osNewName += poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.;
292 osNewName += "/";
293 osNewName += CPLString(fileName).substr(0, 7);
294 osNewName += ".hgt";
295 GDALOpenInfocxx . . $ GDALOpenInfo# oOpenInfocxx . . $ GDALOpenInfo#GDALOpenInfo(5369a9d766ba1673).(osNewName, GA_ReadOnly);
296 return Identifycxx . . $ SRTMHGTDataset#Identify(48f695ac6b30cd84).(&oOpenInfo);
297 }
298
299 if (!STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(fileName, "/vsizip/") &&
300 osLCFilename.endsWith(".srtmswbd.raw.zip"))
301 {
302 CPLString osNewName("/vsizip/");
303 osNewName += poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.;
304 osNewName += "/";
305 osNewName += CPLString(fileName).substr(0, 7);
306 osNewName += ".raw";
307 GDALOpenInfocxx . . $ GDALOpenInfo# oOpenInfocxx . . $ GDALOpenInfo#GDALOpenInfo(5369a9d766ba1673).(osNewName, GA_ReadOnly);
308 return Identifycxx . . $ SRTMHGTDataset#Identify(48f695ac6b30cd84).(&oOpenInfo);
309 }
310
311 // .hgts and .err files from
312 // https://e4ftl01.cr.usgs.gov/MEASURES/NASADEM_SHHP.001/2000.02.11/ .img
313 // and .img.num files from
314 // https://e4ftl01.cr.usgs.gov/MEASURES/NASADEM_SIM.001/2000.02.11/
315 if (!osLCFilename.endsWith(".hgt") && !osLCFilename.endsWith(".hgts") &&
316 !osLCFilename.endsWith(".err") && !osLCFilename.endsWith(".img") &&
317 !osLCFilename.endsWith(".num") && // .img.num or .num
318 !osLCFilename.endsWith(".raw") &&
319 !osLCFilename.endsWith(
320 ".swb") && // https://e4ftl01.cr.usgs.gov/MEASURES/NASADEM_HGT.001/2000.02.11/
321 !osLCFilename.endsWith(".hgt.gz"))
322 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
323
324 /* -------------------------------------------------------------------- */
325 /* We check the file size to see if it is */
326 /* SRTM1 (below or above lat 50) or SRTM 3 */
327 /* -------------------------------------------------------------------- */
328 VSIStatBufL fileStat;
329
330 if (VSIStatL(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename., &fileStat) != 0)
331 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
332 if (fileStat.st_size != 1201 * 1201 * 2 &&
333 fileStat.st_size != 1801 * 3601 * 2 &&
334 fileStat.st_size != 3601 * 3601 &&
335 fileStat.st_size != 3601 * 3601 * 2 &&
336 fileStat.st_size != 3601 * 3601 * 4 && // .hgts
337 fileStat.st_size != 7201 * 7201 * 2)
338 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
339
340 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
341 }
342
343 /************************************************************************/
344 /* Open() */
345 /************************************************************************/
346
347 GDALDatasetcxx . . $ GDALDataset# *SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::Opencxx . . $ SRTMHGTDataset#Open(905b7966ba8c1649).(GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo)
348 {
349 return OpenPAMcxx . . $ SRTMHGTDataset#OpenPAM(2bc92ff7293b0d84).(poOpenInfo);
350 }
351
352 /************************************************************************/
353 /* OpenPAM() */
354 /************************************************************************/
355
356 GDALPamDatasetcxx . . $ GDALPamDataset# *SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::OpenPAMcxx . . $ SRTMHGTDataset#OpenPAM(2bc92ff7293b0d84).(GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo)
357 {
358 if (!Identifycxx . . $ SRTMHGTDataset#Identify(48f695ac6b30cd84).(poOpenInfo))
359 return nullptr;
360
361 const char *fileName = CPLGetFilename(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
362 CPLString osLCFilename(CPLString(fileName).tolower());
363 if (!STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(fileName, "/vsizip/") && osLCFilename.endsWith(".hgt.zip"))
364 {
365 CPLString osFilename("/vsizip/");
366 osFilename += poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.;
367 osFilename += "/";
368 osFilename += CPLString(fileName).substr(0, 7);
369 osFilename += ".hgt";
370 GDALOpenInfocxx . . $ GDALOpenInfo# oOpenInfocxx . . $ GDALOpenInfo#GDALOpenInfo(5369a9d766ba1673).(osFilename, poOpenInfo->eAccesscxx . . $ GDALOpenInfo#eAccess.);
371 auto poDS = OpenPAMcxx . . $ SRTMHGTDataset#OpenPAM(2bc92ff7293b0d84).(&oOpenInfo);
372 if (poDS != nullptr)
373 {
374 // override description with the main one
375 poDS->SetDescriptioncxx . . $ GDALMajorObject#SetDescription(85c52e162fed56f9).(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
376 }
377 return poDS;
378 }
379
380 if (!STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(fileName, "/vsizip/") &&
381 osLCFilename.endsWith(".srtmswbd.raw.zip"))
382 {
383 CPLString osFilename("/vsizip/");
384 osFilename += poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.;
385 osFilename += "/";
386 osFilename += CPLString(fileName).substr(0, 7);
387 osFilename += ".raw";
388 GDALOpenInfocxx . . $ GDALOpenInfo# oOpenInfocxx . . $ GDALOpenInfo#GDALOpenInfo(5369a9d766ba1673).(osFilename, poOpenInfo->eAccesscxx . . $ GDALOpenInfo#eAccess.);
389 auto poDS = OpenPAMcxx . . $ SRTMHGTDataset#OpenPAM(2bc92ff7293b0d84).(&oOpenInfo);
390 if (poDS != nullptr)
391 {
392 // override description with the main one
393 poDS->SetDescriptioncxx . . $ GDALMajorObject#SetDescription(85c52e162fed56f9).(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
394 }
395 return poDS;
396 }
397
398 char latLonValueString[4];
399 memset(latLonValueString, 0, 4);
400 strncpy(latLonValueString, &fileName[1], 2);
401 int southWestLat = atoicxx . . $ atoi(f5ecf3191ed0cd93).(latLonValueString);
402 memset(latLonValueString, 0, 4);
403 // cppcheck-suppress redundantCopy
404 strncpy(latLonValueString, &fileName[4], 3);
405 int southWestLon = atoicxx . . $ atoi(f5ecf3191ed0cd93).(latLonValueString);
406
407 if (fileName[0] == 'N' || fileName[0] == 'n')
408 /*southWestLat = southWestLat */;
409 else if (fileName[0] == 'S' || fileName[0] == 's')
410 southWestLat = southWestLat * -1;
411 else
412 return nullptr;
413
414 if (fileName[3] == 'E' || fileName[3] == 'e')
415 /*southWestLon = southWestLon */;
416 else if (fileName[3] == 'W' || fileName[3] == 'w')
417 southWestLon = southWestLon * -1;
418 else
419 return nullptr;
420
421 /* -------------------------------------------------------------------- */
422 /* Create a corresponding GDALDataset. */
423 /* -------------------------------------------------------------------- */
424 auto poDS = stdcxx . . $ std/::make_unique<SRTMHGTDatasetcxx . . $ SRTMHGTDataset#>();
425
426 poDS->fpImagecxx . . $ SRTMHGTDataset#fpImage. = poOpenInfo->fpLcxx . . $ GDALOpenInfo#fpL.;
427 poOpenInfo->fpLcxx . . $ GDALOpenInfo#fpL. = nullptr;
428
429 VSIStatBufL fileStat;
430 if (VSIStatL(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename., &fileStat) != 0)
431 {
432 return nullptr;
433 }
434
435 int numPixels_x, numPixels_y;
436
437 GDALDataType eDT = GDT_Int16;
438 switch (fileStat.st_size)
439 {
440 case 1201 * 1201 * 2:
441 numPixels_x = numPixels_y = 1201;
442 break;
443 case 1801 * 3601 * 2:
444 numPixels_x = 1801;
445 numPixels_y = 3601;
446 break;
447 case 3601 * 3601:
448 numPixels_x = numPixels_y = 3601;
449 eDT = GDT_UInt8;
450 break;
451 case 3601 * 3601 * 2:
452 numPixels_x = numPixels_y = 3601;
453 break;
454 case 3601 * 3601 * 4: // .hgts
455 numPixels_x = numPixels_y = 3601;
456 eDT = GDT_Float32;
457 break;
458 case 7201 * 7201 * 2:
459 numPixels_x = numPixels_y = 7201;
460 break;
461 default:
462 numPixels_x = numPixels_y = 0;
463 break;
464 }
465
466 poDS->eAccesscxx . . $ GDALDataset#eAccess. = poOpenInfo->eAccesscxx . . $ GDALOpenInfo#eAccess.;
467 #ifdef CPL_LSBcxx . . $ `port/cpl_port.h:608:9`!
468 if (poDS->eAccesscxx . . $ GDALDataset#eAccess. == GA_Update && eDT != GDT_UInt8)
469 {
470 poDS->pabyBuffercxx . . $ SRTMHGTDataset#pabyBuffer. =
471 static_cast<GBytecxx . . $ GByte# *>(CPLMalloc(numPixels_x * sizeof(eDT)));
472 }
473 #endif
474
475 /* -------------------------------------------------------------------- */
476 /* Capture some information from the file that is of interest. */
477 /* -------------------------------------------------------------------- */
478 poDS->nRasterXSizecxx . . $ GDALDataset#nRasterXSize. = numPixels_x;
479 poDS->nRasterYSizecxx . . $ GDALDataset#nRasterYSize. = numPixels_y;
480 poDS->nBandscxx . . $ GDALDataset#nBands. = 1;
481
482 poDS->m_gtcxx . . $ SRTMHGTDataset#m_gt..xorigcxx . . $ GDALGeoTransform#xorig. = southWestLon - 0.5 / (numPixels_x - 1);
483 poDS->m_gtcxx . . $ SRTMHGTDataset#m_gt..xscalecxx . . $ GDALGeoTransform#xscale. = 1.0 / (numPixels_x - 1);
484 poDS->m_gtcxx . . $ SRTMHGTDataset#m_gt..xrotcxx . . $ GDALGeoTransform#xrot. = 0.0;
485 poDS->m_gtcxx . . $ SRTMHGTDataset#m_gt..yorigcxx . . $ GDALGeoTransform#yorig. = southWestLat + 1 + 0.5 / (numPixels_y - 1);
486 poDS->m_gtcxx . . $ SRTMHGTDataset#m_gt..yrotcxx . . $ GDALGeoTransform#yrot. = 0.0;
487 poDS->m_gtcxx . . $ SRTMHGTDataset#m_gt..yscalecxx . . $ GDALGeoTransform#yscale. = -1.0 / (numPixels_y - 1);
488
489 poDS->SetMetadataItemcxx . . $ GDALPamDataset#SetMetadataItem(216fc8eb9053a851).(GDALMD_AREA_OR_POINTcxx . . $ `gcore/gdal.h:390:9`!, GDALMD_AOP_POINTcxx . . $ `gcore/gdal.h:396:9`!);
490
491 /* -------------------------------------------------------------------- */
492 /* Create band information object. */
493 /* -------------------------------------------------------------------- */
494 SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand# *tmpBand = new SRTMHGTRasterBandcxx . . $ SRTMHGTRasterBand#SRTMHGTRasterBand(470396b4cd8290bf).(poDS.get(), 1, eDT);
495 poDS->SetBandcxx . . $ GDALDataset#SetBand(f7464fdc0fe5b8c5).(1, tmpBand);
496
497 /* -------------------------------------------------------------------- */
498 /* Initialize any PAM information. */
499 /* -------------------------------------------------------------------- */
500 poDS->SetDescriptioncxx . . $ GDALMajorObject#SetDescription(85c52e162fed56f9).(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
501 poDS->TryLoadXMLcxx . . $ GDALPamDataset#TryLoadXML(3a22ad7ac1be656d).();
502
503 /* -------------------------------------------------------------------- */
504 /* Support overviews. */
505 /* -------------------------------------------------------------------- */
506 poDS->oOvManagercxx . . $ GDALDataset#oOvManager..Initializecxx . . $ GDALDefaultOverviews#Initialize(96d7e00266b2f754).(poDS.get(), poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
507
508 return poDS.release();
509 }
510
511 /************************************************************************/
512 /* CreateCopy() */
513 /************************************************************************/
514
515 GDALDatasetcxx . . $ GDALDataset# *SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::CreateCopycxx . . $ SRTMHGTDataset#CreateCopy(8d8ece3624d4575).(const char *pszFilename,
516 GDALDatasetcxx . . $ GDALDataset# *poSrcDS, int bStrict,
517 CSLConstListcxx . . $ CSLConstList# /* papszOptions*/,
518 GDALProgressFunc pfnProgress,
519 void *pProgressData)
520 {
521 /* -------------------------------------------------------------------- */
522 /* Some some rudimentary checks */
523 /* -------------------------------------------------------------------- */
524 const int nBands = poSrcDS->GetRasterCountcxx . . $ GDALDataset#GetRasterCount(455f465bc33b4cdf).();
525 if (nBands == 0)
526 {
527 CPLError(CE_Failure, CPLE_NotSupportedcxx . . $ `port/cpl_error.h:118:9`!,
528 "SRTMHGT driver does not support source dataset with zero "
529 "bands.");
530 return nullptr;
531 }
532 else if (nBands != 1)
533 {
534 CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupportedcxx . . $ `port/cpl_error.h:118:9`!,
535 "SRTMHGT driver only uses the first band of the dataset.");
536 if (bStrict)
537 return nullptr;
538 }
539
540 /* -------------------------------------------------------------------- */
541 /* Checks the input SRS */
542 /* -------------------------------------------------------------------- */
543 OGRSpatialReferencecxx . . $ OGRSpatialReference# ogrsr_inputcxx . . $ OGRSpatialReference#OGRSpatialReference(85c52e162fed56f9).;
544 ogrsr_input.importFromWktcxx . . $ OGRSpatialReference#importFromWkt(5c4306d8553b3696).(poSrcDS->GetProjectionRefcxx . . $ GDALDataset#GetProjectionRef(a9f41ea0e82d88cf).());
545
546 OGRSpatialReferencecxx . . $ OGRSpatialReference# ogrsr_wgs84cxx . . $ OGRSpatialReference#OGRSpatialReference(85c52e162fed56f9).;
547 ogrsr_wgs84.SetWellKnownGeogCScxx . . $ OGRSpatialReference#SetWellKnownGeogCS(5c4306d8553b3696).("WGS84");
548
549 if (ogrsr_input.IsSameGeogCScxx . . $ OGRSpatialReference#IsSameGeogCS(1d22f83639af445a).(&ogrsr_wgs84) == FALSEcxx . . $ `port/cpl_port.h:1180:9`!)
550 {
551 CPLError(CE_Warning, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
552 "The source projection coordinate system is %s. Only WGS 84 "
553 "is supported.\nThe SRTMHGT driver will generate a file as "
554 "if the source was WGS 84 projection coordinate system.",
555 poSrcDS->GetProjectionRefcxx . . $ GDALDataset#GetProjectionRef(a9f41ea0e82d88cf).());
556 }
557
558 /* -------------------------------------------------------------------- */
559 /* Work out the LL origin. */
560 /* -------------------------------------------------------------------- */
561 GDALGeoTransformcxx . . $ GDALGeoTransform# gtcxx . . $ GDALGeoTransform#GDALGeoTransform(ced63f7c635d850d).;
562 if (poSrcDS->GetGeoTransformcxx . . $ GDALDataset#GetGeoTransform(9cfa0773fdb41a90).(gt) != CE_None)
563 {
564 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
565 "Source image must have a geo transform matrix.");
566 return nullptr;
567 }
568
569 const int nLLOriginLat = static_cast<int>(
570 stdcxx . . $ std/::floor(gt.yorigcxx . . $ GDALGeoTransform#yorig. + poSrcDS->GetRasterYSizecxx . . $ GDALDataset#GetRasterYSize(455f465bc33b4cdf).() * gt.yscalecxx . . $ GDALGeoTransform#yscale. + 0.5));
571
572 int nLLOriginLong = static_cast<int>(stdcxx . . $ std/::floor(gt.xorigcxx . . $ GDALGeoTransform#xorig. + 0.5));
573
574 if (stdcxx . . $ std/::abs(nLLOriginLat - (gt.yorigcxx . . $ GDALGeoTransform#yorig. + (poSrcDS->GetRasterYSizecxx . . $ GDALDataset#GetRasterYSize(455f465bc33b4cdf).() - 0.5) *
575 gt.yscalecxx . . $ GDALGeoTransform#yscale.)) > 1e-10 ||
576 stdcxx . . $ std/::abs(nLLOriginLong - (gt.xorigcxx . . $ GDALGeoTransform#xorig. + 0.5 * gt.xscalecxx . . $ GDALGeoTransform#xscale.)) > 1e-10)
577 {
578 CPLError(CE_Warning, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
579 "The corner coordinates of the source are not properly "
580 "aligned on plain latitude/longitude boundaries.");
581 }
582
583 /* -------------------------------------------------------------------- */
584 /* Check image dimensions. */
585 /* -------------------------------------------------------------------- */
586 const int nXSize = poSrcDS->GetRasterXSizecxx . . $ GDALDataset#GetRasterXSize(455f465bc33b4cdf).();
587 const int nYSize = poSrcDS->GetRasterYSizecxx . . $ GDALDataset#GetRasterYSize(455f465bc33b4cdf).();
588
589 if (!((nXSize == 1201 && nYSize == 1201) ||
590 (nXSize == 1801 && nYSize == 3601) ||
591 (nXSize == 3601 && nYSize == 3601) ||
592 (nXSize == 7201 && nYSize == 7201)))
593 {
594 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
595 "Image dimensions should be 1201x1201, 1801x3601, 3601x3601 "
596 "or 7201x7201.");
597 return nullptr;
598 }
599
600 /* -------------------------------------------------------------------- */
601 /* Check filename. */
602 /* -------------------------------------------------------------------- */
603 char expectedFileName[12];
604
605 CPLsnprintf(expectedFileName, sizeof(expectedFileName), "%c%02d%c%03d.HGT",
606 (nLLOriginLat >= 0) ? 'N' : 'S',
607 (nLLOriginLat >= 0) ? nLLOriginLat : -nLLOriginLat,
608 (nLLOriginLong >= 0) ? 'E' : 'W',
609 (nLLOriginLong >= 0) ? nLLOriginLong : -nLLOriginLong);
610
611 if (!EQUALcxx . . $ `port/cpl_port.h:532:9`!(expectedFileName, CPLGetFilename(pszFilename)))
612 {
613 CPLError(CE_Warning, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Expected output filename is %s.",
614 expectedFileName);
615 }
616
617 /* -------------------------------------------------------------------- */
618 /* Write output file. */
619 /* -------------------------------------------------------------------- */
620 VSILFILE *fp = VSIFOpenL(pszFilename, "wb");
621 if (fp == nullptr)
622 {
623 CPLError(CE_Failure, CPLE_FileIOcxx . . $ `port/cpl_error.h:112:9`!, "Cannot create file %s", pszFilename);
624 return nullptr;
625 }
626
627 GInt16cxx . . $ GInt16# *panData = static_cast<GInt16cxx . . $ GInt16# *>(CPLMalloc(sizeof(GInt16cxx . . $ GInt16#) * nXSize));
628 GDALRasterBandcxx . . $ GDALRasterBand# *poSrcBand = poSrcDS->GetRasterBandcxx . . $ GDALDataset#GetRasterBand(b7317453d4fdb323).(1);
629
630 int bSrcBandHasNoData;
631 double srcBandNoData = poSrcBand->GetNoDataValuecxx . . $ GDALRasterBand#GetNoDataValue(3e6f4119bf25dad8).(&bSrcBandHasNoData);
632
633 for (int iY = 0; iY < nYSize; iY++)
634 {
635 if (poSrcBand->RasterIOcxx . . $ GDALRasterBand#RasterIO(b70cc820beb1726b).(GF_Read, 0, iY, nXSize, 1,
636 reinterpret_cast<void *>(panData), nXSize, 1,
637 GDT_Int16, 0, 0, nullptr) != CE_None)
638 {
639 VSIFCloseL(fp);
640 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(panData);
641 return nullptr;
642 }
643
644 /* Translate nodata values */
645 if (bSrcBandHasNoData && srcBandNoData != SRTMHG_NODATA_VALUEcxx . . $ SRTMHG_NODATA_VALUE.)
646 {
647 for (int iX = 0; iX < nXSize; iX++)
648 {
649 if (panData[iX] == srcBandNoData)
650 panData[iX] = SRTMHG_NODATA_VALUEcxx . . $ SRTMHG_NODATA_VALUE.;
651 }
652 }
653
654 #ifdef CPL_LSBcxx . . $ `port/cpl_port.h:608:9`!
655 GDALSwapWords(panData, 2, nXSize, 2);
656 #endif
657
658 if (VSIFWriteL(panData, sizeof(GInt16cxx . . $ GInt16#) * nXSize, 1, fp) != 1)
659 {
660 CPLError(CE_Failure, CPLE_FileIOcxx . . $ `port/cpl_error.h:112:9`!,
661 "Failed to write line %d in SRTMHGT dataset.\n", iY);
662 VSIFCloseL(fp);
663 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(panData);
664 return nullptr;
665 }
666
667 if (pfnProgress && !pfnProgress((iY + 1) / static_cast<double>(nYSize),
668 nullptr, pProgressData))
669 {
670 CPLError(CE_Failure, CPLE_UserInterruptcxx . . $ `port/cpl_error.h:124:9`!,
671 "User terminated CreateCopy()");
672 VSIFCloseL(fp);
673 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(panData);
674 return nullptr;
675 }
676 }
677
678 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(panData);
679 VSIFCloseL(fp);
680
681 /* -------------------------------------------------------------------- */
682 /* Reopen and copy missing information into a PAM file. */
683 /* -------------------------------------------------------------------- */
684 GDALOpenInfocxx . . $ GDALOpenInfo# oOpenInfocxx . . $ GDALOpenInfo#GDALOpenInfo(5369a9d766ba1673).(pszFilename, GA_ReadOnly);
685 auto poDS = OpenPAMcxx . . $ SRTMHGTDataset#OpenPAM(2bc92ff7293b0d84).(&oOpenInfo);
686
687 if (poDS)
688 poDS->CloneInfocxx . . $ GDALPamDataset#CloneInfo(a4e6077b839cd67f).(poSrcDS, GCIF_PAM_DEFAULTcxx . . $ `gcore/gdal_pam.h:66:9`!);
689
690 return poDS;
691 }
692
693 /************************************************************************/
694 /* GDALRegister_SRTMHGT() */
695 /************************************************************************/
696 void GDALRegister_SRTMHGTcxx . . $ GDALRegister_SRTMHGT(49f6e7a06ebc5aa8).()
697 {
698 if (GDALGetDriverByName("SRTMHGT") != nullptr)
699 return;
700
701 GDALDrivercxx . . $ GDALDriver# *poDriver = new GDALDrivercxx . . $ GDALDriver#GDALDriver(49f6e7a06ebc5aa8).();
702
703 poDriver->SetDescriptioncxx . . $ GDALMajorObject#SetDescription(85c52e162fed56f9).("SRTMHGT");
704 poDriver->SetMetadataItemcxx . . $ GDALDriver#SetMetadataItem(216fc8eb9053a851).(GDAL_DCAP_RASTERcxx . . $ `gcore/gdal.h:678:9`!, "YES");
705 poDriver->SetMetadataItemcxx . . $ GDALDriver#SetMetadataItem(216fc8eb9053a851).(GDAL_DMD_LONGNAMEcxx . . $ `gcore/gdal.h:443:9`!, "SRTMHGT File Format");
706 poDriver->SetMetadataItemcxx . . $ GDALDriver#SetMetadataItem(216fc8eb9053a851).(GDAL_DMD_EXTENSIONcxx . . $ `gcore/gdal.h:452:9`!, "hgt");
707 poDriver->SetMetadataItemcxx . . $ GDALDriver#SetMetadataItem(216fc8eb9053a851).(GDAL_DMD_HELPTOPICcxx . . $ `gcore/gdal.h:446:9`!,
708 "drivers/raster/srtmhgt.html");
709 poDriver->SetMetadataItemcxx . . $ GDALDriver#SetMetadataItem(216fc8eb9053a851).(GDAL_DMD_CREATIONDATATYPEScxx . . $ `gcore/gdal.h:513:9`!, "Byte Int16 UInt16");
710 poDriver->SetMetadataItemcxx . . $ GDALDriver#SetMetadataItem(216fc8eb9053a851).(GDAL_DCAP_VIRTUALIOcxx . . $ `gcore/gdal.h:674:9`!, "YES");
711
712 poDriver->pfnIdentifycxx . . $ GDALDriver#pfnIdentify. = SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::Identifycxx . . $ SRTMHGTDataset#Identify(48f695ac6b30cd84).;
713 poDriver->pfnOpencxx . . $ GDALDriver#pfnOpen. = SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::Opencxx . . $ SRTMHGTDataset#Open(905b7966ba8c1649).;
714 poDriver->pfnCreateCopycxx . . $ GDALDriver#pfnCreateCopy. = SRTMHGTDatasetcxx . . $ SRTMHGTDataset#::CreateCopycxx . . $ SRTMHGTDataset#CreateCopy(8d8ece3624d4575).;
715
716 GetGDALDriverManager()->RegisterDrivercxx . . $ GDALDriverManager#RegisterDriver(ed1dabcc01594d64).(poDriver);
717 }