-
-
Notifications
You must be signed in to change notification settings - Fork 372
r.in.pdal: fix reported data extent when CRS doesn't match #6465
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
Open
petrasovaa
wants to merge
5
commits into
OSGeo:main
Choose a base branch
from
petrasovaa:pdal-extent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+347
−61
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fab7218
r.in.pdal: fix reported data extent when CRS doesn't match
petrasovaa 3787811
fix tests
petrasovaa b7e942f
forgot to add test las files
petrasovaa 4eb4596
address review, improve test
petrasovaa a325585
set higher delta for macOS
petrasovaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,19 @@ | |
| * Read the COPYING file that comes with GRASS for details. | ||
| * | ||
| */ | ||
| #include <cmath> | ||
| #include <stdbool.h> | ||
|
|
||
| #include <pdal/filters/ReprojectionFilter.hpp> | ||
| #include <pdal/io/BufferReader.hpp> | ||
|
|
||
| #include "info.h" | ||
| #include <cmath> | ||
|
|
||
| #ifdef PDAL_USE_NOSRS | ||
| extern "C" { | ||
| #include "projection.h" | ||
| } | ||
|
|
||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| void get_extent(struct StringList *infiles, double *min_x, double *max_x, | ||
| double *min_y, double *max_y, double *min_z, double *max_z, | ||
| bool nosrs) | ||
|
|
@@ -22,8 +30,9 @@ void get_extent(struct StringList *infiles, double *min_x, double *max_x, | |
| #endif | ||
| { | ||
| pdal::StageFactory factory; | ||
| bool first = 1; | ||
|
|
||
| pdal::SpatialReference spatial_reference; | ||
| bool first = true; | ||
| double min_x_, max_x_, min_y_, max_y_, min_z_, max_z_; | ||
| *min_x = *max_x = *min_y = *max_y = *min_z = *max_z = NAN; | ||
|
|
||
| for (int i = 0; i < infiles->num_items; i++) { | ||
|
|
@@ -38,7 +47,7 @@ void get_extent(struct StringList *infiles, double *min_x, double *max_x, | |
| pdal::Options las_opts; | ||
| pdal::Option las_opt("filename", infile); | ||
| las_opts.add(las_opt); | ||
| #ifdef PDAL_USE_NOSRS | ||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| if (nosrs) { | ||
| pdal::Option nosrs_opt("nosrs", true); | ||
| las_opts.add(nosrs_opt); | ||
|
|
@@ -52,43 +61,131 @@ void get_extent(struct StringList *infiles, double *min_x, double *max_x, | |
| catch (const std::exception &err) { | ||
| G_fatal_error(_("PDAL error: %s"), err.what()); | ||
| } | ||
| spatial_reference = las_reader.getSpatialReference(); | ||
| std::string dataset_wkt = spatial_reference.getWKT(); | ||
| bool proj_match = is_wkt_projection_same_as_loc(dataset_wkt.c_str()); | ||
|
|
||
| const pdal::LasHeader &las_header = las_reader.header(); | ||
|
|
||
| min_x_ = las_header.minX(); | ||
| min_y_ = las_header.minY(); | ||
| min_z_ = las_header.minZ(); | ||
| max_x_ = las_header.maxX(); | ||
| max_y_ = las_header.maxY(); | ||
| max_z_ = las_header.maxZ(); | ||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| bool need_to_reproject = !nosrs && !proj_match && | ||
| spatial_reference.valid() && | ||
| !spatial_reference.empty(); | ||
| #else | ||
| bool need_to_reproject = !proj_match && spatial_reference.valid() && | ||
| !spatial_reference.empty(); | ||
| #endif | ||
|
|
||
| if (need_to_reproject) { | ||
| get_reprojected_extent(spatial_reference, &min_x_, &max_x_, &min_y_, | ||
| &max_y_, &min_z_, &max_z_); | ||
| } | ||
| if (first) { | ||
| *min_x = las_header.minX(); | ||
| *min_y = las_header.minY(); | ||
| *min_z = las_header.minZ(); | ||
| *max_x = las_header.maxX(); | ||
| *max_y = las_header.maxY(); | ||
| *max_z = las_header.maxZ(); | ||
|
|
||
| first = 0; | ||
| *min_x = min_x_; | ||
| *min_y = min_y_; | ||
| *min_z = min_z_; | ||
| *max_x = max_x_; | ||
| *max_y = max_y_; | ||
| *max_z = max_z_; | ||
|
|
||
| first = false; | ||
| } | ||
| else { | ||
| if (*min_x > min_x_) | ||
| *min_x = min_x_; | ||
| if (*min_y > min_y_) | ||
| *min_y = min_y_; | ||
| if (*min_z > min_z_) | ||
| *min_z = min_z_; | ||
| if (*max_x < max_x_) | ||
| *max_x = max_x_; | ||
| if (*max_y < max_y_) | ||
| *max_y = max_y_; | ||
| if (*max_z < max_z_) | ||
| *max_z = max_z_; | ||
| } | ||
| } | ||
| } | ||
| /* Reproject 4 points representing bounding box using PDAL pipeline */ | ||
| void get_reprojected_extent(pdal::SpatialReference &spatial_reference, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to document this either with function doc or in-code comments just to provide a quick reference on the approach taken here (create bbox as points and reproject it using PDAL functions). |
||
| double *min_x, double *max_x, double *min_y, | ||
| double *max_y, double *min_z, double *max_z) | ||
| { | ||
| pdal::PointTable table; | ||
| table.layout()->registerDim(pdal::Dimension::Id::X); | ||
| table.layout()->registerDim(pdal::Dimension::Id::Y); | ||
| table.layout()->registerDim(pdal::Dimension::Id::Z); | ||
|
|
||
| pdal::PointViewPtr view(new pdal::PointView(table)); | ||
|
|
||
| view->setField(pdal::Dimension::Id::X, 0, *min_x); | ||
| view->setField(pdal::Dimension::Id::Y, 0, *min_y); | ||
| view->setField(pdal::Dimension::Id::Z, 0, *min_z); | ||
| view->setField(pdal::Dimension::Id::X, 1, *min_x); | ||
| view->setField(pdal::Dimension::Id::Y, 1, *max_y); | ||
| view->setField(pdal::Dimension::Id::Z, 1, *min_z); | ||
| view->setField(pdal::Dimension::Id::X, 2, *max_x); | ||
| view->setField(pdal::Dimension::Id::Y, 2, *min_y); | ||
| view->setField(pdal::Dimension::Id::Z, 2, *max_z); | ||
| view->setField(pdal::Dimension::Id::X, 3, *max_x); | ||
| view->setField(pdal::Dimension::Id::Y, 3, *max_y); | ||
| view->setField(pdal::Dimension::Id::Z, 3, *max_z); | ||
|
|
||
| pdal::BufferReader reader; | ||
| reader.addView(view); | ||
|
|
||
| pdal::StageFactory factory; | ||
| pdal::Stage *reproject = factory.createStage("filters.reprojection"); | ||
| pdal::Options reproject_options; | ||
| reproject_options.add("in_srs", spatial_reference.getWKT()); | ||
| reproject_options.add("out_srs", location_projection_as_wkt(false)); | ||
| reproject->setOptions(reproject_options); | ||
| reproject->setInput(reader); | ||
| reproject->prepare(table); | ||
| reproject->execute(table); | ||
|
|
||
| for (pdal::PointId i = 0; i < view->size(); ++i) { | ||
|
|
||
| double x = view->getFieldAs<double>(pdal::Dimension::Id::X, i); | ||
| double y = view->getFieldAs<double>(pdal::Dimension::Id::Y, i); | ||
| double z = view->getFieldAs<double>(pdal::Dimension::Id::Z, i); | ||
| if (i == 0) { | ||
| *min_x = *max_x = x; | ||
| *min_y = *max_y = y; | ||
| *min_z = *max_z = z; | ||
| } | ||
| else { | ||
| if (*min_x > las_header.minX()) | ||
| *min_x = las_header.minX(); | ||
| if (*min_y > las_header.minY()) | ||
| *min_y = las_header.minY(); | ||
| if (*min_z > las_header.minZ()) | ||
| *min_z = las_header.minZ(); | ||
| if (*max_x < las_header.maxX()) | ||
| *max_x = las_header.maxX(); | ||
| if (*max_y < las_header.maxY()) | ||
| *max_y = las_header.maxY(); | ||
| if (*max_z < las_header.maxZ()) | ||
| *max_z = las_header.maxZ(); | ||
| if (*min_x > x) | ||
| *min_x = x; | ||
| if (*min_y > y) | ||
| *min_y = y; | ||
| if (*min_z > z) | ||
| *min_z = z; | ||
| if (*max_x < x) | ||
| *max_x = x; | ||
| if (*max_y < y) | ||
| *max_y = y; | ||
| if (*max_z < z) | ||
| *max_z = z; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #ifdef PDAL_USE_NOSRS | ||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| void print_extent(struct StringList *infiles, bool nosrs) | ||
| #else | ||
| void print_extent(struct StringList *infiles) | ||
| #endif | ||
| { | ||
| double min_x, max_x, min_y, max_y, min_z, max_z; | ||
|
|
||
| #ifdef PDAL_USE_NOSRS | ||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| get_extent(infiles, &min_x, &max_x, &min_y, &max_y, &min_z, &max_z, nosrs); | ||
| #else | ||
| get_extent(infiles, &min_x, &max_x, &min_y, &max_y, &min_z, &max_z); | ||
|
|
@@ -97,7 +194,7 @@ void print_extent(struct StringList *infiles) | |
| min_x, min_z, max_z); | ||
| } | ||
|
|
||
| #ifdef PDAL_USE_NOSRS | ||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| void print_lasinfo(struct StringList *infiles, bool nosrs) | ||
| #else | ||
| void print_lasinfo(struct StringList *infiles) | ||
|
|
@@ -123,7 +220,7 @@ void print_lasinfo(struct StringList *infiles) | |
| pdal::Options las_opts; | ||
| pdal::Option las_opt("filename", infile); | ||
| las_opts.add(las_opt); | ||
| #ifdef PDAL_USE_NOSRS | ||
| #ifdef R_IN_PDAL_USE_NOSRS | ||
| if (nosrs) { | ||
| pdal::Option nosrs_opt("nosrs", true); | ||
| las_opts.add(nosrs_opt); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using only the 4 corner points might underestimate the real reprojected extents, see e.g. https://github.com/OSGeo/grass/blob/main/lib/proj/do_proj.c#L124 and https://github.com/OSGeo/gdal/blob/master/ogr/ogrct.cpp#L2110 for suggestions to insert some more vertices along the box edges.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was aware of that but given points clouds are typically local scale, I didn't consider the distortion significant. But sure, I can add that.