43
43
#include < grid_map_core/iterators/CircleIterator.hpp>
44
44
#include < grid_map_core/iterators/GridMapIterator.hpp>
45
45
46
+ #if __APPLE__
47
+ #include < cpl_string.h>
48
+ #include < gdal.h>
49
+ #include < gdal_priv.h>
50
+ #include < ogr_p.h>
51
+ #include < ogr_spatialref.h>
52
+ #else
53
+ #include < gdal/cpl_string.h>
54
+ #include < gdal/gdal.h>
55
+ #include < gdal/gdal_priv.h>
56
+ #include < gdal/ogr_p.h>
57
+ #include < gdal/ogr_spatialref.h>
58
+ #endif
59
+
46
60
GridMapGeo::GridMapGeo () {}
47
61
48
62
GridMapGeo::~GridMapGeo () {}
49
63
50
64
bool GridMapGeo::Load (const std::string &map_path, bool algin_terrain, const std::string color_map_path) {
51
- bool loaded = initializeFromGeotiff (map_path, algin_terrain);
65
+ bool loaded = initializeFromGdalDataset (map_path, algin_terrain);
52
66
if (!color_map_path.empty ()) { // Load color layer if the color path is nonempty
53
67
bool color_loaded = addColorFromGeotiff (color_map_path);
54
68
}
55
69
if (!loaded) return false ;
56
70
return true ;
57
71
}
58
72
59
- bool GridMapGeo::initializeFromGeotiff (const std::string &path, bool align_terrain) {
73
+ bool GridMapGeo::initializeFromGdalDataset (const std::string &path, bool align_terrain) {
60
74
GDALAllRegister ();
61
75
GDALDataset *dataset = (GDALDataset *)GDALOpen (path.c_str (), GA_ReadOnly);
62
76
if (!dataset) {
63
77
std::cout << " Failed to open" << std::endl;
64
78
return false ;
65
79
}
66
- std::cout << std::endl << " Loading GeoTIFF file for gridmap" << std::endl;
80
+ std::cout << std::endl << " Loading GDAL Dataset for gridmap" << std::endl;
67
81
68
82
double originX, originY, pixelSizeX, pixelSizeY;
69
83
double geoTransform[6 ];
@@ -80,17 +94,24 @@ bool GridMapGeo::initializeFromGeotiff(const std::string &path, bool align_terra
80
94
const char *pszProjection = dataset->GetProjectionRef ();
81
95
std::cout << std::endl << " Wkt ProjectionRef: " << pszProjection << std::endl;
82
96
97
+ if (strlen (pszProjection) == 0 ) {
98
+ // https://gdal.org/user/raster_data_model.html#coordinate-system
99
+ std::cerr << std::endl << " Wkt ProjectionRef is unknown!" << std::endl;
100
+ return false ;
101
+ }
102
+
83
103
// Get image metadata
84
104
unsigned width = dataset->GetRasterXSize ();
85
105
unsigned height = dataset->GetRasterYSize ();
86
106
double resolution = pixelSizeX;
87
- std::cout << " Width : " << width << " Height : " << height << " Resolution : " << resolution << std::endl;
107
+ std::cout << __LINE__ << " RasterX : " << width << " RasterY : " << height << " pixelSizeX : " << pixelSizeX << std::endl;
88
108
89
109
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
90
110
// Y pixel coordinates go towards the south
91
111
const double lengthX = resolution * width;
92
112
const double lengthY = resolution * height;
93
113
grid_map::Length length (lengthX, lengthY);
114
+ std::cout << __LINE__ << " GMLX: " << lengthX << " GMLY: " << lengthY << std::endl;
94
115
95
116
double mapcenter_e = originX + pixelSizeX * width * 0.5 ;
96
117
double mapcenter_n = originY + pixelSizeY * height * 0.5 ;
@@ -115,36 +136,39 @@ bool GridMapGeo::initializeFromGeotiff(const std::string &path, bool align_terra
115
136
// }
116
137
117
138
grid_map_.setGeometry (length, resolution, position);
118
- // / TODO: Use TF for geocoordinates
119
- grid_map_.setFrameId (" map" );
120
- grid_map_.add (" elevation" );
121
- GDALRasterBand *elevationBand = dataset->GetRasterBand (1 );
122
-
123
- std::vector<float > data (width * height, 0 .0f );
124
- elevationBand->RasterIO (GF_Read, 0 , 0 , width, height, &data[0 ], width, height, GDT_Float32, 0 , 0 );
125
-
126
- grid_map::Matrix &layer_elevation = grid_map_[" elevation" ];
127
- for (grid_map::GridMapIterator iterator (grid_map_); !iterator.isPastEnd (); ++iterator) {
128
- const grid_map::Index gridMapIndex = *iterator;
129
- // TODO: This may be wrong if the pixelSizeY > 0
130
- int x = width - 1 - gridMapIndex (0 );
131
- int y = gridMapIndex (1 );
132
-
133
- layer_elevation (x, y) = data[gridMapIndex (0 ) + width * gridMapIndex (1 )];
134
- }
139
+ // /// TODO: Use TF for geocoordinates
140
+ // grid_map_.setFrameId("map");
141
+ // grid_map_.add("elevation");
142
+ // const auto raster_count = dataset->GetRasterCount();
143
+ // assert(raster_count == 2);
144
+ // std::cout << __LINE__ << "Raster Count: " << raster_count << std::endl;
145
+ // GDALRasterBand *elevationBand = dataset->GetRasterBand(1);
146
+
147
+ // std::vector<float> data(width * height, 0.0f);
148
+ // elevationBand->RasterIO(GF_Read, 0, 0, width, height, &data[0], width, height, GDT_Float32, 0, 0);
149
+
150
+ // grid_map::Matrix &layer_elevation = grid_map_["elevation"];
151
+ // for (grid_map::GridMapIterator iterator(grid_map_); !iterator.isPastEnd(); ++iterator) {
152
+ // const grid_map::Index gridMapIndex = *iterator;
153
+ // // TODO: This may be wrong if the pixelSizeY > 0
154
+ // int x = width - 1 - gridMapIndex(0);
155
+ // int y = gridMapIndex(1);
156
+
157
+ // layer_elevation(x, y) = data[gridMapIndex(0) + width * gridMapIndex(1)];
158
+ // }
135
159
136
- // / TODO: This is a workaround with the problem of gdal 3 not translating altitude correctly.
137
- // / This section just levels the current position to the ground
138
- double altitude{0.0 };
139
- if (grid_map_.isInside (Eigen::Vector2d (0.0 , 0.0 ))) {
140
- altitude = grid_map_.atPosition (" elevation" , Eigen::Vector2d (0.0 , 0.0 ));
141
- }
160
+ // // / TODO: This is a workaround with the problem of gdal 3 not translating altitude correctly.
161
+ // // / This section just levels the current position to the ground
162
+ // double altitude{0.0};
163
+ // if (grid_map_.isInside(Eigen::Vector2d(0.0, 0.0))) {
164
+ // altitude = grid_map_.atPosition("elevation", Eigen::Vector2d(0.0, 0.0));
165
+ // }
142
166
143
167
// Eigen::Translation3d meshlab_translation(0.0, 0.0, -altitude);
144
168
// Eigen::AngleAxisd meshlab_rotation(Eigen::AngleAxisd::Identity());
145
169
// Eigen::Isometry3d transform = meshlab_translation * meshlab_rotation; // Apply affine transformation.
146
170
// grid_map_ = grid_map_.getTransformedMap(transform, "elevation", grid_map_.getFrameId(), true);
147
- return true ;
171
+ return false ;
148
172
}
149
173
150
174
bool GridMapGeo::addColorFromGeotiff (const std::string &path) {
@@ -172,7 +196,7 @@ bool GridMapGeo::addColorFromGeotiff(const std::string &path) {
172
196
unsigned width = dataset->GetRasterXSize ();
173
197
unsigned height = dataset->GetRasterYSize ();
174
198
double resolution = pixelSizeX;
175
- std::cout << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
199
+ std::cout << __LINE__ << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
176
200
177
201
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
178
202
// Y pixel coordinates go towards the south
@@ -233,7 +257,7 @@ bool GridMapGeo::addLayerFromGeotiff(const std::string &layer_name, const std::s
233
257
unsigned width = dataset->GetRasterXSize ();
234
258
unsigned height = dataset->GetRasterYSize ();
235
259
double resolution = pixelSizeX;
236
- std::cout << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
260
+ std::cout << __LINE__ << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
237
261
238
262
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
239
263
// Y pixel coordinates go towards the south
0 commit comments