@@ -62,6 +62,15 @@ GridMapGeo::GridMapGeo(const std::string &frame_id) { frame_id_ = frame_id; }
62
62
63
63
GridMapGeo::~GridMapGeo () {}
64
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
+
65
74
bool GridMapGeo::Load (const std::string &map_path, const std::string color_map_path) {
66
75
bool loaded = initializeFromGdalDataset (map_path);
67
76
if (!color_map_path.empty ()) { // Load color layer if the color path is nonempty
@@ -106,20 +115,30 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path) {
106
115
coordinate_name_ = spatial_ref->GetAttrValue (" geogcs" );
107
116
108
117
// Get image metadata
109
- unsigned width = dataset->GetRasterXSize ();
110
- unsigned height = dataset->GetRasterYSize ();
118
+ const auto raster_width = dataset->GetRasterXSize ();
119
+ const auto raster_height = dataset->GetRasterYSize ();
120
+
121
+ if (raster_width > max_raster_x_size_ || raster_height > max_raster_y_size_) {
122
+ std::cout << " Raster too big. Using a submap of size " << max_raster_x_size_ << " x" << max_raster_y_size_
123
+ << std::endl;
124
+ }
111
125
double resolution = pixelSizeX;
112
- std::cout << __LINE__ << " RasterX: " << width << " RasterY: " << height << " pixelSizeX: " << pixelSizeX << std::endl;
126
+ std::cout << " RasterX: " << raster_width << " RasterY: " << raster_height << " pixelSizeX: " << pixelSizeX
127
+ << std::endl;
128
+
129
+ // Limit grid map size to not exceed memory limitations
130
+ const auto grid_width = std::min (raster_width, max_raster_x_size_);
131
+ const auto grid_height = std::min (raster_height, max_raster_y_size_);
113
132
114
133
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
115
134
// Y pixel coordinates go towards the south
116
- const double lengthX = resolution * width ;
117
- const double lengthY = resolution * height ;
135
+ const double lengthX = resolution * grid_width ;
136
+ const double lengthY = resolution * grid_height ;
118
137
grid_map::Length length (lengthX, lengthY);
119
- std::cout << __LINE__ << " GMLX: " << lengthX << " GMLY: " << lengthY << std::endl;
138
+ std::cout << " GMLX: " << lengthX << " GMLY: " << lengthY << std::endl;
120
139
121
- double mapcenter_e = originX + pixelSizeX * width * 0.5 ;
122
- double mapcenter_n = originY + pixelSizeY * height * 0.5 ;
140
+ double mapcenter_e = originX + pixelSizeX * grid_width * 0.5 ;
141
+ double mapcenter_n = originY + pixelSizeY * grid_height * 0.5 ;
123
142
maporigin_.espg = ESPG::CH1903_LV03;
124
143
maporigin_.position = Eigen::Vector3d (mapcenter_e, mapcenter_n, 0.0 );
125
144
@@ -128,20 +147,26 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path) {
128
147
grid_map_.setGeometry (length, resolution, position);
129
148
grid_map_.setFrameId (frame_id_);
130
149
grid_map_.add (" elevation" );
131
- GDALRasterBand *elevationBand = dataset->GetRasterBand (1 );
132
150
133
- // std::vector<float> data(width * height, 0.0f);
134
- // elevationBand->RasterIO(GF_Read, 0, 0, width, height, &data[0], width, height, GDT_Float32, 0, 0);
151
+ const auto raster_count = dataset->GetRasterCount ();
152
+ assert (raster_count == 1 ); // expect only elevation data, otherwise it's the wrong dataset.
153
+ GDALRasterBand *elevationBand = dataset->GetRasterBand (1 );
135
154
136
- // grid_map::Matrix &layer_elevation = grid_map_["elevation"];
137
- // for (grid_map::GridMapIterator iterator(grid_map_); !iterator.isPastEnd(); ++iterator) {
138
- // const grid_map::Index gridMapIndex = *iterator;
139
- // // TODO: This may be wrong if the pixelSizeY > 0
140
- // int x = width - 1 - gridMapIndex(0);
141
- // int y = gridMapIndex(1);
155
+ std::vector<float > data (grid_width * grid_height, 0 .0f );
156
+ const auto raster_err = elevationBand->RasterIO (GF_Read, 0 , 0 , grid_width, grid_height, &data[0 ], grid_width,
157
+ grid_height, GDT_Float32, 0 , 0 );
158
+ if (raster_err != CPLE_None) {
159
+ return false ;
160
+ }
161
+ grid_map::Matrix &layer_elevation = grid_map_[" elevation" ];
162
+ for (grid_map::GridMapIterator iterator (grid_map_); !iterator.isPastEnd (); ++iterator) {
163
+ const grid_map::Index gridMapIndex = *iterator;
164
+ // TODO: This may be wrong if the pixelSizeY > 0
165
+ int x = grid_width - 1 - gridMapIndex (0 );
166
+ int y = gridMapIndex (1 );
142
167
143
- // layer_elevation(x, y) = data[gridMapIndex(0) + width * gridMapIndex(1)];
144
- // }
168
+ layer_elevation (x, y) = data[gridMapIndex (0 ) + grid_width * gridMapIndex (1 )];
169
+ }
145
170
146
171
return true ;
147
172
}
@@ -171,7 +196,7 @@ bool GridMapGeo::addColorFromGeotiff(const std::string &path) {
171
196
unsigned width = dataset->GetRasterXSize ();
172
197
unsigned height = dataset->GetRasterYSize ();
173
198
double resolution = pixelSizeX;
174
- std::cout << __LINE__ << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
199
+ std::cout << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
175
200
176
201
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
177
202
// Y pixel coordinates go towards the south
@@ -232,7 +257,7 @@ bool GridMapGeo::addLayerFromGeotiff(const std::string &layer_name, const std::s
232
257
unsigned width = dataset->GetRasterXSize ();
233
258
unsigned height = dataset->GetRasterYSize ();
234
259
double resolution = pixelSizeX;
235
- std::cout << __LINE__ << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
260
+ std::cout << " Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
236
261
237
262
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
238
263
// Y pixel coordinates go towards the south
0 commit comments