Skip to content

Commit e1e7974

Browse files
committed
Fix XY flip error
Signed-off-by: Ryan Friedman <[email protected]>
1 parent e49d1f5 commit e1e7974

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ grid_map_geo map_publisher --ros-args \
8787
# To debug from the launch file
8888
ros2 launch grid_map_geo load_vrt_launch.xml
8989
```
90+
91+
**Note:** `grid_map_geo` uses asserts to catch coding errors; they are enabled by default when
92+
building in debug mode, and removed from release mode.

include/grid_map_geo/transform.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ inline Eigen::Vector2d geoToImageNoRot(const std::array<double, 6>& geoTransform
167167
* @return
168168
*/
169169
inline bool getGeoCorners(const GDALDatasetUniquePtr& datasetPtr, Corners& corners) {
170-
double originX, originY, pixelSizeX, pixelSizeY;
171170
std::array<double, 6> geoTransform;
172171

173172
// https://gdal.org/tutorials/geotransforms_tut.html#introduction-to-geotransforms

src/grid_map_geo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path) {
155155
maporigin_.position = Eigen::Vector3d(mapcenter_e, mapcenter_n, 0.0);
156156
} else {
157157
if (maporigin_.position.x() < corners.top_left.x() || maporigin_.position.x() > corners.bottom_right.x() ||
158-
maporigin_.position.y() < corners.top_left.y() || maporigin_.position.y() > corners.bottom_left.y()) {
159-
std::cerr << "Requested map origin is outside of raster dataset" << std::endl;
158+
maporigin_.position.y() < corners.bottom_right.y() || maporigin_.position.y() > corners.top_left.y()) {
159+
std::cerr << "The configured map origin is outside of raster dataset!" << std::endl;
160160
return false;
161161
}
162162
}
@@ -173,12 +173,12 @@ bool GridMapGeo::initializeFromGdalDataset(const std::string &path) {
173173

174174
// Compute the center in pixel space, then get the raster bounds nXOff and nYOff to extract with RasterIO
175175
const auto center_px = geoToImageNoRot(geoTransform, {maporigin_.position.x(), maporigin_.position.y()});
176-
const auto nxOff = center_px.x() - grid_width / 2;
177-
const auto nYOff = center_px.y() - grid_height / 2;
176+
const auto raster_io_x_offset = center_px.x() - grid_width / 2;
177+
const auto raster_io_y_offset = center_px.y() - grid_height / 2;
178178

179179
std::vector<float> data(grid_width * grid_height, 0.0f);
180-
const auto raster_err = elevationBand->RasterIO(GF_Read, nxOff, nYOff, grid_width, grid_height, &data[0], grid_width,
181-
grid_height, GDT_Float32, 0, 0);
180+
const auto raster_err = elevationBand->RasterIO(GF_Read, raster_io_x_offset, raster_io_y_offset, grid_width,
181+
grid_height, &data[0], grid_width, grid_height, GDT_Float32, 0, 0);
182182
if (raster_err != CPLE_None) {
183183
return false;
184184
}

src/map_publisher.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,35 @@ class MapPublisher : public rclcpp::Node {
8282

8383
map_ = std::make_shared<GridMapGeo>(frame_id);
8484
map_->setMaxMapSizePixels(max_map_width, max_map_height);
85+
86+
rcl_interfaces::msg::ParameterDescriptor origin_descriptor;
87+
origin_descriptor.read_only = true;
88+
origin_descriptor.description = "Map origin latitude (WGS-84) in degrees.";
89+
rcl_interfaces::msg::FloatingPointRange origin_range;
90+
91+
origin_range.from_value = -90.0;
92+
origin_range.to_value = 90.0;
93+
origin_descriptor.floating_point_range.push_back(origin_range);
94+
95+
static_assert(std::numeric_limits<double>::has_quiet_NaN == true, "Need quiet NaN for default value");
96+
const auto map_origin_latitude = std::clamp(
97+
this->declare_parameter("map_origin_latitude", std::numeric_limits<double>::quiet_NaN(), origin_descriptor),
98+
origin_range.from_value, origin_range.to_value);
99+
100+
origin_range.from_value = -180.0;
101+
origin_range.to_value = 180.0;
102+
origin_descriptor.floating_point_range.at(0) = origin_range;
103+
104+
origin_descriptor.description = "Map origin longitude (WGS-84) in degrees.";
105+
const auto map_origin_longitude = std::clamp(
106+
this->declare_parameter("map_origin_longitude", std::numeric_limits<double>::quiet_NaN(), origin_descriptor),
107+
origin_range.from_value, origin_range.to_value);
108+
109+
map_ = std::make_shared<GridMapGeo>();
110+
map_->setMaxMapSizePixels(max_map_width, max_map_height);
111+
map_->setGlobalOrigin(ESPG::WGS84, Eigen::Vector3d(map_origin_longitude, map_origin_latitude, 0.0));
85112
map_->Load(gdal_dataset_path, color_path);
113+
86114
auto timer_callback = [this]() -> void {
87115
auto msg = grid_map::GridMapRosConverter::toMessage(map_->getGridMap());
88116
if (msg) {

0 commit comments

Comments
 (0)