@@ -61,6 +61,17 @@ GridMapGeo::GridMapGeo() {}
61
61
62
62
GridMapGeo::~GridMapGeo () {}
63
63
64
+
65
+ void GridMapGeo::setMaxMapSizePixels (const int pixels_x, const int pixels_y) {
66
+ // Use int type to match GDAL , but validate it's a positive value.
67
+ assert (pixels_x >= 0 );
68
+ assert (pixels_y >= 0 );
69
+
70
+ max_raster_x_size_ = pixels_x;
71
+ max_raster_y_size_ = pixels_y;
72
+ }
73
+
74
+
64
75
bool GridMapGeo::Load (const std::string &map_path, bool algin_terrain, const std::string color_map_path) {
65
76
bool loaded = initializeFromGdalDataset (map_path, algin_terrain);
66
77
if (!color_map_path.empty ()) { // Load color layer if the color path is nonempty
@@ -101,20 +112,29 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path, bool align_t
101
112
}
102
113
103
114
// Get image metadata
104
- unsigned width = dataset->GetRasterXSize ();
105
- unsigned height = dataset->GetRasterYSize ();
115
+ const auto raster_width = dataset->GetRasterXSize ();
116
+ const auto raster_height = dataset->GetRasterYSize ();
117
+
118
+ if (raster_width > max_raster_x_size_ || raster_height > max_raster_y_size_) {
119
+ std::cout << " Raster too big. Using a submap of size " << max_raster_x_size_ << " x" << max_raster_y_size_ << std::endl;
120
+ }
106
121
double resolution = pixelSizeX;
107
- std::cout << __LINE__ << " RasterX: " << width << " RasterY: " << height << " pixelSizeX: " << pixelSizeX << std::endl;
122
+ std::cout << " RasterX: " << raster_width << " RasterY: " << raster_height << " pixelSizeX: " << pixelSizeX << std::endl;
123
+
124
+
125
+ // Limit grid map size to not exceed memory limitations
126
+ const auto grid_width = std::min (raster_width, max_raster_x_size_);
127
+ const auto grid_height = std::min (raster_height, max_raster_y_size_);
108
128
109
129
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
110
130
// Y pixel coordinates go towards the south
111
- const double lengthX = resolution * width ;
112
- const double lengthY = resolution * height ;
131
+ const double lengthX = resolution * grid_width ;
132
+ const double lengthY = resolution * grid_height ;
113
133
grid_map::Length length (lengthX, lengthY);
114
- std::cout << __LINE__ << " GMLX: " << lengthX << " GMLY: " << lengthY << std::endl;
134
+ std::cout << " GMLX: " << lengthX << " GMLY: " << lengthY << std::endl;
115
135
116
- double mapcenter_e = originX + pixelSizeX * width * 0.5 ;
117
- double mapcenter_n = originY + pixelSizeY * height * 0.5 ;
136
+ double mapcenter_e = originX + pixelSizeX * grid_width * 0.5 ;
137
+ double mapcenter_n = originY + pixelSizeY * grid_height * 0.5 ;
118
138
maporigin_.espg = ESPG::CH1903_LV03;
119
139
maporigin_.position = Eigen::Vector3d (mapcenter_e, mapcenter_n, 0.0 );
120
140
@@ -136,39 +156,41 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path, bool align_t
136
156
// }
137
157
138
158
grid_map_.setGeometry (length, resolution, position);
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
- // }
159
+ // / TODO: Use TF for geocoordinates
160
+ grid_map_.setFrameId (" map" );
161
+ grid_map_.add (" elevation" );
159
162
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
- // }
163
+ const auto raster_count = dataset->GetRasterCount ();
164
+ assert (raster_count == 1 ); // expect only elevation data, otherwise it's the wrong dataset.
165
+ GDALRasterBand *elevationBand = dataset->GetRasterBand (1 );
166
166
167
- // Eigen::Translation3d meshlab_translation(0.0, 0.0, -altitude);
168
- // Eigen::AngleAxisd meshlab_rotation(Eigen::AngleAxisd::Identity());
169
- // Eigen::Isometry3d transform = meshlab_translation * meshlab_rotation; // Apply affine transformation.
170
- // grid_map_ = grid_map_.getTransformedMap(transform, "elevation", grid_map_.getFrameId(), true);
171
- return false ;
167
+ std::vector<float > data (grid_width * grid_height, 0 .0f );
168
+ const auto raster_err = elevationBand->RasterIO (GF_Read, 0 , 0 , grid_width, grid_height, &data[0 ], grid_width, grid_height, GDT_Float32, 0 , 0 );
169
+ if (raster_err != CPLE_None ) {
170
+ return false ;
171
+ }
172
+ grid_map::Matrix &layer_elevation = grid_map_[" elevation" ];
173
+ for (grid_map::GridMapIterator iterator (grid_map_); !iterator.isPastEnd (); ++iterator) {
174
+ const grid_map::Index gridMapIndex = *iterator;
175
+ // TODO: This may be wrong if the pixelSizeY > 0
176
+ int x = grid_width - 1 - gridMapIndex (0 );
177
+ int y = gridMapIndex (1 );
178
+
179
+ layer_elevation (x, y) = data[gridMapIndex (0 ) + grid_width * gridMapIndex (1 )];
180
+ }
181
+
182
+ // / TODO: This is a workaround with the problem of gdal 3 not translating altitude correctly.
183
+ // / This section just levels the current position to the ground
184
+ double altitude{0.0 };
185
+ if (grid_map_.isInside (Eigen::Vector2d (0.0 , 0.0 ))) {
186
+ altitude = grid_map_.atPosition (" elevation" , Eigen::Vector2d (0.0 , 0.0 ));
187
+ }
188
+
189
+ Eigen::Translation3d meshlab_translation (0.0 , 0.0 , -altitude);
190
+ Eigen::AngleAxisd meshlab_rotation (Eigen::AngleAxisd::Identity ());
191
+ Eigen::Isometry3d transform = meshlab_translation * meshlab_rotation; // Apply affine transformation.
192
+ grid_map_ = grid_map_.getTransformedMap (transform, " elevation" , grid_map_.getFrameId (), true );
193
+ return true ;
172
194
}
173
195
174
196
bool GridMapGeo::addColorFromGeotiff (const std::string &path) {
@@ -196,7 +218,7 @@ bool GridMapGeo::addColorFromGeotiff(const std::string &path) {
196
218
unsigned width = dataset->GetRasterXSize ();
197
219
unsigned height = dataset->GetRasterYSize ();
198
220
double resolution = pixelSizeX;
199
- std::cout << __LINE__ << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
221
+ std::cout << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
200
222
201
223
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
202
224
// Y pixel coordinates go towards the south
@@ -257,7 +279,7 @@ bool GridMapGeo::addLayerFromGeotiff(const std::string &layer_name, const std::s
257
279
unsigned width = dataset->GetRasterXSize ();
258
280
unsigned height = dataset->GetRasterYSize ();
259
281
double resolution = pixelSizeX;
260
- std::cout << __LINE__ << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
282
+ std::cout << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
261
283
262
284
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
263
285
// Y pixel coordinates go towards the south
0 commit comments