This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathDem.cc
438 lines (372 loc) · 13.2 KB
/
Dem.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
313
314
315
316
317
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <algorithm>
#include <boost/filesystem.hpp>
#include <gazebo/gazebo_config.h>
#ifdef HAVE_GDAL
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
# include <ogr_spatialref.h>
# pragma GCC diagnostic pop
#endif
#include "gazebo/common/CommonIface.hh"
#include "gazebo/common/Console.hh"
#include "gazebo/common/Dem.hh"
#include "gazebo/common/DemPrivate.hh"
#include "gazebo/common/Exception.hh"
#include "gazebo/common/SphericalCoordinates.hh"
using namespace gazebo;
using namespace common;
#ifdef HAVE_GDAL
//////////////////////////////////////////////////
Dem::Dem()
: dataPtr(new DemPrivate)
{
this->dataPtr->dataSet = nullptr;
GDALAllRegister();
}
//////////////////////////////////////////////////
Dem::~Dem()
{
this->dataPtr->demData.clear();
if (this->dataPtr->dataSet)
GDALClose(reinterpret_cast<GDALDataset *>(this->dataPtr->dataSet));
delete this->dataPtr;
this->dataPtr = nullptr;
}
//////////////////////////////////////////////////
void Dem::SetSphericalCoordinates(
common::SphericalCoordinatesPtr _worldSphericalCoordinates)
{
this->dataPtr->sphericalCoordinates =_worldSphericalCoordinates;
}
//////////////////////////////////////////////////
int Dem::Load(const std::string &_filename)
{
unsigned int width;
unsigned int height;
int xSize, ySize;
double upLeftX, upLeftY, upRightX, upRightY, lowLeftX, lowLeftY;
ignition::math::Angle upLeftLat, upLeftLong, upRightLat, upRightLong;
ignition::math::Angle lowLeftLat, lowLeftLong;
// Sanity check
std::string fullName = _filename;
if (!boost::filesystem::exists(boost::filesystem::path(fullName)))
fullName = common::find_file(_filename);
if (!boost::filesystem::exists(boost::filesystem::path(fullName)))
{
gzerr << "Unable to open DEM file[" << _filename
<< "], check your GAZEBO_RESOURCE_PATH settings." << std::endl;
return -1;
}
this->dataPtr->dataSet = reinterpret_cast<GDALDataset *>(GDALOpen(
fullName.c_str(), GA_ReadOnly));
if (this->dataPtr->dataSet == nullptr)
{
gzerr << "Unable to open DEM file[" << fullName
<< "]. Format not recognised as a supported dataset." << std::endl;
return -1;
}
int nBands = this->dataPtr->dataSet->GetRasterCount();
if (nBands != 1)
{
gzerr << "Unsupported number of bands in file [" << fullName + "]. Found "
<< nBands << " but only 1 is a valid value." << std::endl;
return -1;
}
// Set the pointer to the band
this->dataPtr->band = this->dataPtr->dataSet->GetRasterBand(1);
// Raster width and height
xSize = this->dataPtr->dataSet->GetRasterXSize();
ySize = this->dataPtr->dataSet->GetRasterYSize();
// Corner coordinates
try
{
upLeftX = 0.0;
upLeftY = 0.0;
upRightX = xSize;
upRightY = 0.0;
lowLeftX = 0.0;
lowLeftY = ySize;
// Calculate the georeferenced coordinates of the terrain corners
this->GetGeoReference(upLeftX, upLeftY, upLeftLat, upLeftLong);
this->GetGeoReference(upRightX, upRightY, upRightLat, upRightLong);
this->GetGeoReference(lowLeftX, lowLeftY, lowLeftLat, lowLeftLong);
// Set the world width and height
this->dataPtr->worldWidth =
this->dataPtr->sphericalCoordinates->DistanceBetweenPoints(
upLeftLat, upLeftLong, upRightLat, upRightLong);
this->dataPtr->worldHeight =
this->dataPtr->sphericalCoordinates->DistanceBetweenPoints(
upLeftLat, upLeftLong, lowLeftLat, lowLeftLong);
}
catch(const common::Exception &)
{
gzwarn << "Failed to automatically compute DEM size. "
<< "Please use the <size> element to manually set DEM size."
<< std::endl;
}
// Set the terrain's side (the terrain will be squared after the padding)
if (ignition::math::isPowerOfTwo(ySize - 1))
height = ySize;
else
height = ignition::math::roundUpPowerOfTwo(ySize) + 1;
if (ignition::math::isPowerOfTwo(xSize - 1))
width = xSize;
else
width = ignition::math::roundUpPowerOfTwo(xSize) + 1;
this->dataPtr->side = std::max(width, height);
// Preload the DEM's data
if (this->LoadData() != 0)
return -1;
// Check for nodata value in dem data. This is used when computing the
// min elevation. If nodata value is not defined, we assume it will be one
// of the commonly used values such as -9999, -32768, etc.
// For simplicity, we will treat values <= -9999 as nodata values and
// ignore them when computing the min elevation.
int validNoData = 0;
const double defaultNoDataValue = -9999;
double noDataValue = this->dataPtr->band->GetNoDataValue(&validNoData);
if (validNoData <= 0)
noDataValue = defaultNoDataValue;
double min = ignition::math::MAX_D;
double max = -ignition::math::MAX_D;
for (auto d : this->dataPtr->demData)
{
if (d < min && d > noDataValue)
min = d;
if (d > max && d > noDataValue)
max = d;
}
if (ignition::math::equal(min, ignition::math::MAX_D) ||
ignition::math::equal(max, -ignition::math::MAX_D))
{
gzwarn << "Dem is composed of 'nodata' values!" << std::endl;
}
this->dataPtr->minElevation = min;
this->dataPtr->maxElevation = max;
return 0;
}
//////////////////////////////////////////////////
double Dem::GetElevation(double _x, double _y)
{
if (_x >= this->GetWidth() || _y >= this->GetHeight())
{
gzthrow("Illegal coordinates. You are asking for the elevation in (" <<
_x << "," << _y << ") but the terrain is [" << this->GetWidth() <<
" x " << this->GetHeight() << "]\n");
}
return this->dataPtr->demData.at(_y * this->GetWidth() + _x);
}
//////////////////////////////////////////////////
float Dem::GetMinElevation() const
{
return this->dataPtr->minElevation;
}
//////////////////////////////////////////////////
float Dem::GetMaxElevation() const
{
return this->dataPtr->maxElevation;
}
//////////////////////////////////////////////////
void Dem::GetGeoReference(double _x, double _y,
ignition::math::Angle &_latitude, ignition::math::Angle &_longitude) const
{
double geoTransf[6];
if (this->dataPtr->dataSet->GetGeoTransform(geoTransf) == CE_None)
{
OGRSpatialReference sourceCs;
OGRSpatialReference targetCs;
OGRCoordinateTransformation *cT;
double xGeoDeg, yGeoDeg;
// Transform the terrain's coordinate system to the appropriate
// coordinate system.
#if GDAL_VERSION_NUM >= 2030000
const char *importString;
#else
char *importString;
#endif
importString = strdup(this->dataPtr->dataSet->GetProjectionRef());
sourceCs.importFromWkt(&importString);
if (this->dataPtr->sphericalCoordinates->GetSurfaceType() ==
common::SphericalCoordinates::MOON_SCS)
{
targetCs = OGRSpatialReference();
double axisEquatorial =
this->dataPtr->sphericalCoordinates->SurfaceAxisEquatorial();
double axisPolar =
this->dataPtr->sphericalCoordinates->SurfaceAxisPolar();
std::string surfaceLatLongProjStr =
"+proj=latlong +a=" + std::to_string(axisEquatorial) +
" +b=" + std::to_string(axisPolar);
targetCs.importFromProj4(surfaceLatLongProjStr.c_str());
}
else
{
targetCs.SetWellKnownGeogCS("WGS84");
}
cT = OGRCreateCoordinateTransformation(&sourceCs, &targetCs);
if (nullptr == cT)
{
gzthrow("Unable to transform terrain coordinate system to WGS84 for "
<< "coordinates (" << _x << "," << _y << ")");
}
xGeoDeg = geoTransf[0] + _x * geoTransf[1] + _y * geoTransf[2];
yGeoDeg = geoTransf[3] + _x * geoTransf[4] + _y * geoTransf[5];
cT->Transform(1, &xGeoDeg, &yGeoDeg);
_latitude.Degree(yGeoDeg);
_longitude.Degree(xGeoDeg);
OCTDestroyCoordinateTransformation(cT);
}
else
{
gzthrow("Unable to obtain the georeferenced values for coordinates ("
<< _x << "," << _y << ")");
}
}
//////////////////////////////////////////////////
void Dem::GetGeoReferenceOrigin(ignition::math::Angle &_latitude,
ignition::math::Angle &_longitude) const
{
return this->GetGeoReference(0, 0, _latitude, _longitude);
}
//////////////////////////////////////////////////
unsigned int Dem::GetHeight() const
{
return this->dataPtr->side;
}
//////////////////////////////////////////////////
unsigned int Dem::GetWidth() const
{
return this->dataPtr->side;
}
//////////////////////////////////////////////////
double Dem::GetWorldWidth() const
{
return this->dataPtr->worldWidth;
}
//////////////////////////////////////////////////
double Dem::GetWorldHeight() const
{
return this->dataPtr->worldHeight;
}
//////////////////////////////////////////////////
void Dem::FillHeightMap(int _subSampling, unsigned int _vertSize,
const ignition::math::Vector3d &_size,
const ignition::math::Vector3d &_scale,
bool _flipY, std::vector<float> &_heights)
{
if (_subSampling <= 0)
{
gzerr << "Illegal subsampling value (" << _subSampling << ")\n";
return;
}
// Resize the vector to match the size of the vertices.
_heights.resize(_vertSize * _vertSize);
// Iterate over all the vertices
for (unsigned int y = 0; y < _vertSize; ++y)
{
double yf = y / static_cast<double>(_subSampling);
unsigned int y1 = floor(yf);
unsigned int y2 = ceil(yf);
if (y2 >= this->dataPtr->side)
y2 = this->dataPtr->side - 1;
double dy = yf - y1;
for (unsigned int x = 0; x < _vertSize; ++x)
{
double xf = x / static_cast<double>(_subSampling);
unsigned int x1 = floor(xf);
unsigned int x2 = ceil(xf);
if (x2 >= this->dataPtr->side)
x2 = this->dataPtr->side - 1;
double dx = xf - x1;
double px1 = this->dataPtr->demData[y1 * this->dataPtr->side + x1];
double px2 = this->dataPtr->demData[y1 * this->dataPtr->side + x2];
float h1 = (px1 - ((px1 - px2) * dx));
double px3 = this->dataPtr->demData[y2 * this->dataPtr->side + x1];
double px4 = this->dataPtr->demData[y2 * this->dataPtr->side + x2];
float h2 = (px3 - ((px3 - px4) * dx));
float h = this->dataPtr->minElevation +
(h1 - ((h1 - h2) * dy) - this->dataPtr->minElevation) * _scale.Z();
// Invert pixel definition so 1=ground, 0=full height,
// if the terrain size has a negative z component
// this is mainly for backward compatibility
if (_size.Z() < 0)
h *= -1;
// Convert to minElevation if a NODATA value is found
if (_size.Z() >= 0 && h < this->dataPtr->minElevation)
h = this->dataPtr->minElevation;
// Store the height for future use
if (!_flipY)
_heights[y * _vertSize + x] = h;
else
_heights[(_vertSize - y - 1) * _vertSize + x] = h;
}
}
}
//////////////////////////////////////////////////
int Dem::LoadData()
{
unsigned int destWidth;
unsigned int destHeight;
unsigned int nXSize = this->dataPtr->dataSet->GetRasterXSize();
unsigned int nYSize = this->dataPtr->dataSet->GetRasterYSize();
float ratio;
std::vector<float> buffer;
if (nXSize == 0 || nYSize == 0)
{
gzerr << "Illegal size loading a DEM file (" << nXSize << ","
<< nYSize << ")\n";
return -1;
}
// Scale the terrain keeping the same ratio between width and height
if (nXSize > nYSize)
{
ratio = static_cast<float>(nXSize) / static_cast<float>(nYSize);
destWidth = this->dataPtr->side;
// The decimal part is discarted for interpret the result as pixels
destHeight = static_cast<float>(destWidth) / static_cast<float>(ratio);
}
else
{
ratio = static_cast<float>(nYSize) / static_cast<float>(nXSize);
destHeight = this->dataPtr->side;
// The decimal part is discarted for interpret the result as pixels
destWidth = static_cast<float>(destHeight) / static_cast<float>(ratio);
}
// Read the whole raster data and convert it to a GDT_Float32 array.
// In this step the DEM is scaled to destWidth x destHeight
buffer.resize(destWidth * destHeight);
if (this->dataPtr->band->RasterIO(GF_Read, 0, 0, nXSize, nYSize, &buffer[0],
destWidth, destHeight, GDT_Float32, 0, 0) != CE_None)
{
gzerr << "Failure calling RasterIO while loading a DEM file\n";
return -1;
}
// Copy and align 'buffer' into the target vector. The destination vector is
// initialized to 0, so all the points not contained in 'buffer' will be
// extra padding
this->dataPtr->demData.resize(this->GetWidth() * this->GetHeight());
for (unsigned int y = 0; y < destHeight; ++y)
{
std::copy(&buffer[destWidth * y], &buffer[destWidth * y] + destWidth,
this->dataPtr->demData.begin() + this->GetWidth() * y);
}
buffer.clear();
return 0;
}
#endif