Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEM: Add support for GDAL vsicurl, vsizip support and avoid segfaults with huge VRT datasets #597

Conversation

Ryanf55
Copy link
Contributor

@Ryanf55 Ryanf55 commented Apr 20, 2024

🎉 New feature

Closes #596

Summary

  1. Adds initial support to load super large DEM datasets by exposing an API to limit the size of the loaded DEM.
  2. Adds an alternative way of loading datafiles that make use of features like vsizip and vsicurl that aren't true filepaths.
  3. Adds a compiler warning for if you use new versions of GDAL, which will lose precision in the current implementation, and potentially misrepresent NoData values.
    • This could be taken out, but could be an issue instead. A printed warning could be ok.
  4. Add a few asserts for safety to catch coding errors earlier
  5. The user-configurable size limits can be used to prevent segmentation faults by loading too big of a raster dataset
  6. FIxed overflow and segfault in RasterIO for massive DEM's and overflow risk in vector resize operations

Test it

I added a unit test which is self explanatory.

./build/bin/UNIT_Dem_TEST

I also hacked a demo with one SRTMHGT file, and confirmed it can load. Gazebo does not perform well with this terrain size (3601 x 3601), but it does load. SDF needs ability to truncate the size of the heightmap in order to be usable, or perhaps decimate the data resolution.
image

Open Questions

  • How do we want the logs to look? Perhaps queue the errors and only report them if the DEM loading fails.

  • Do we need to solve user-configurable origin to load? The previous implementation loaded everything in the raster, so there was nothing to configure

    • Not yet
  • Should we rely on ArduPilot's terrain server being up for the test, or generate a large in-memory dataset for testing large rasters?

    • No, we've relocated a large VRT locally to avoid dependence on an external resource

Future Improvements

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@github-actions github-actions bot added 🌱 garden Ignition Garden 🎵 harmonic Gazebo Harmonic labels Apr 20, 2024
@Ryanf55 Ryanf55 changed the title Implement large vrt with vsicurl and vsizip support for loading more kinds of DEM data DEM: Add support for GDAL vsicurl, vsizip support and avoid segfaults with huge VRT datasets Apr 20, 2024
geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
geospatial/src/Dem.cc Outdated Show resolved Hide resolved
@Ryanf55 Ryanf55 marked this pull request as ready for review April 24, 2024 06:30
@Ryanf55 Ryanf55 requested a review from marcoag as a code owner April 24, 2024 06:30
@Ryanf55
Copy link
Contributor Author

Ryanf55 commented Apr 26, 2024

@ahcorde Would you be able to provide another round of feedback on the functionality of this PR. I'm looking for some direction on how to best contribute my time to Gazebo's support of terrain data. We can use Gazebo for outdoor 3D aerial robotics applications that the aerial working group is currently working on.

geospatial/src/Dem_TEST.cc Outdated Show resolved Hide resolved
Copy link

codecov bot commented Apr 28, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 4 lines in your changes missing coverage. Please review.

Project coverage is 80.54%. Comparing base (2d06bf7) to head (7e8b197).
Report is 26 commits behind head on gz-common5.

Files Patch % Lines
geospatial/src/Dem.cc 90.90% 4 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           gz-common5     #597      +/-   ##
==============================================
- Coverage       83.65%   80.54%   -3.11%     
==============================================
  Files              90       93       +3     
  Lines           10273    13617    +3344     
==============================================
+ Hits             8594    10968    +2374     
- Misses           1679     2649     +970     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

geospatial/src/Dem.cc Outdated Show resolved Hide resolved
geospatial/src/Dem.cc Outdated Show resolved Hide resolved
@Ryanf55 Ryanf55 force-pushed the 596-implement-large-vrt-with-vsicurl-and-vsizip-support branch from c001ba1 to 9d02aba Compare May 10, 2024 18:06
Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for the contribution. I have a few comments, but they should all be easy to address.

geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
@@ -134,6 +146,8 @@ namespace gz
gz::math::Angle &_latitude,
gz::math::Angle &_longitude) const;

private: [[nodiscard]] bool ConfigureLoadedSize();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be placed in Dem::Implementation instead? Also can you document the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done! That made the code a lot cleaner too.

geospatial/src/Dem.cc Outdated Show resolved Hide resolved
geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
geospatial/src/Dem.cc Outdated Show resolved Hide resolved
geospatial/src/Dem.cc Show resolved Hide resolved
geospatial/src/Dem_TEST.cc Outdated Show resolved Hide resolved
common::Dem dem;
auto const path = common::testing::TestFile("data", "ap_srtm1.vrt");
// We expect not to be able to allocate the data, so ensure we throw instead of segfault.
EXPECT_THROW(dem.Load(path), std::bad_alloc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually causing Dem::Load to allocate more memory than available? I'm not sure if we want to do this in a unit test if it might badly affect other things running on CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it immediately throws on my machine, and then gtest catches it. I ran all the tests, and they seem reliable with it.
If you are concerned, what about a GTEST_SKIP so it's still compiled and able to be run manually if you desire?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing on macOS with a timeout so the behavior may not be the same on all platforms, so I'd prefer we use GTEST_SKIP as you suggested.

geospatial/src/Dem_TEST.cc Outdated Show resolved Hide resolved
@azeey
Copy link
Contributor

azeey commented May 21, 2024

Also, please merge from gz-common5 to get the latest changes that fix CI failures.

@azeey azeey added the beta Targeting beta release of upcoming collection label Jul 29, 2024
@azeey
Copy link
Contributor

azeey commented Jul 29, 2024

@Ryanf55 we are now in Feature freeze for Gazebo Ionic. Since PR is already open, I've applied the beta label which means it's slotted for the Ionic release if we can get it merged before 08/28/2024. Do you think that's feasible? If not, please let me know and I'll remove the beta label.

@Ryanf55
Copy link
Contributor Author

Ryanf55 commented Jul 29, 2024

Thanks for the heads up, I'll do my best to make time over the next 2 days.

Copy link
Contributor Author

@Ryanf55 Ryanf55 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azeey I've implemented all of your requests. This is ready for final approval, hopefully before the official release.

geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
geospatial/include/gz/common/geospatial/Dem.hh Outdated Show resolved Hide resolved
@@ -134,6 +146,8 @@ namespace gz
gz::math::Angle &_latitude,
gz::math::Angle &_longitude) const;

private: [[nodiscard]] bool ConfigureLoadedSize();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done! That made the code a lot cleaner too.

geospatial/src/Dem.cc Outdated Show resolved Hide resolved
geospatial/src/Dem.cc Outdated Show resolved Hide resolved
geospatial/src/Dem_TEST.cc Outdated Show resolved Hide resolved
common::Dem dem;
auto const path = common::testing::TestFile("data", "ap_srtm1.vrt");
// We expect not to be able to allocate the data, so ensure we throw instead of segfault.
EXPECT_THROW(dem.Load(path), std::bad_alloc);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it immediately throws on my machine, and then gtest catches it. I ran all the tests, and they seem reliable with it.
If you are concerned, what about a GTEST_SKIP so it's still compiled and able to be run manually if you desire?

geospatial/src/Dem_TEST.cc Outdated Show resolved Hide resolved
geospatial/src/Dem.cc Show resolved Hide resolved
Ryanf55 and others added 11 commits July 30, 2024 23:07
* Can now use GDAL vsicurl and GDAL vsizip features
* There is a new API to set loaded size limits to prevent segfaults from
  loading too big of a raster terrain
* For now, there is no configured way to change where in the large
  raster to load data from

Signed-off-by: Ryan Friedman <[email protected]>
Signed-off-by: Ryan Friedman <[email protected]>
Signed-off-by: Ryan Friedman <[email protected]>
Co-authored-by: Alejandro Hernández Cordero <[email protected]>
Signed-off-by: Ryan <[email protected]>
Co-authored-by: Alejandro Hernández Cordero <[email protected]>
Signed-off-by: Ryan <[email protected]>
Signed-off-by: Ryan Friedman <[email protected]>
* Add a VRT test file
* Add a zipped VRT test file
* Don't rely on internet for tests to pass

Signed-off-by: Ryan Friedman <[email protected]>
Signed-off-by: Ryan Friedman <[email protected]>
Co-authored-by: Addisu Z. Taddese <[email protected]>
Signed-off-by: Ryan <[email protected]>
* Enforce linter and naming conventions
* Add missing getters for size limits and test them
* Enforce 80char column limit
* Move ConfigureLoadedSize to the implementation
* Document all functions
* Fix lambda name case

Signed-off-by: Ryan Friedman <[email protected]>
@Ryanf55 Ryanf55 force-pushed the 596-implement-large-vrt-with-vsicurl-and-vsizip-support branch from 5e03fc3 to b722c57 Compare July 31, 2024 05:07
@Ryanf55
Copy link
Contributor Author

Ryanf55 commented Jul 31, 2024

gz-common5

Rebase complete.

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with green CI!

Ryanf55 and others added 2 commits August 6, 2024 08:56
@azeey azeey merged commit 85b12c6 into gazebosim:gz-common5 Aug 6, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta Targeting beta release of upcoming collection 🌱 garden Ignition Garden 🎵 harmonic Gazebo Harmonic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support GDAL /vsicurl and /vsizip prefixes to DEM paths
3 participants