Repository navigation
66 working with segments vignette #76
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
36cec5f
#66 update column index reference from numeric to L1 in elevation_add()
temospena 753ec37
fix vignette with linestring break segments and add stplanar example …
temospena 9eb8246
#66 add route_to_segments function to split linestrings into vertex-t…
temospena 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -98,15 +98,15 @@ library(sf) | |
|
|
||
| # Load example data | ||
| data(lisbon_route) | ||
| dem_lisbon = dem_lisbon() | ||
| dem_lisbon <- dem_lisbon() | ||
|
Collaborator
Author
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. these operator changes are annoying. please ignore. |
||
| ``` | ||
|
|
||
| ## Add elevation to a linestring | ||
|
|
||
| If you have a 2D linestring and a DEM, you can add elevation data to the linestring using `elevation_add()`: | ||
|
|
||
| ```{r} | ||
| sf_linestring_xyz_local = elevation_add(lisbon_route, dem = dem_lisbon) | ||
| sf_linestring_xyz_local <- elevation_add(lisbon_route, dem = dem_lisbon) | ||
| head(sf::st_coordinates(sf_linestring_xyz_local)) | ||
| ``` | ||
|
|
||
|
|
@@ -123,7 +123,7 @@ If you don't have a local DEM, `elevation_add()` can download elevation data (th | |
| Once you have a 3D linestring (with XYZ coordinates), you can calculate its average slope using `slope_xyz()`: | ||
|
|
||
| ```{r} | ||
| slope = slope_xyz(sf_linestring_xyz_local) | ||
| slope <- slope_xyz(sf_linestring_xyz_local) | ||
| slope | ||
| ``` | ||
|
|
||
|
|
@@ -141,37 +141,48 @@ plot_slope(sf_linestring_xyz_local, pal = pal, brks = brks) | |
|
|
||
| ## Working with segments | ||
|
|
||
| The `slopes` package can also work with individual segments of a linestring. | ||
| First, let's segment the `lisbon_route`: | ||
| The `slopes` package can also work with individual segments of a linestring. | ||
| There are two ways to split a route into segments: | ||
|
|
||
| ```{r} | ||
| lisbon_route_segments = sf::st_segmentize(lisbon_route, dfMaxLength = 100) # Arbitrary length | ||
| lisbon_route_segments = sf::st_cast(lisbon_route_segments, "LINESTRING") | ||
| # Add elevation to segments | ||
| lisbon_route_segments_xyz = elevation_add(lisbon_route_segments, dem = dem_lisbon) | ||
| ``` | ||
| ### By vertex (native segments of the linestring) | ||
|
|
||
| Now calculate the slope for each segment: | ||
| `route_to_segments()` splits the route at every existing vertex, producing one 2-point segment per coordinate pair: | ||
|
|
||
| ```{r} | ||
| lisbon_route_segments_xyz$slope = slope_xyz(lisbon_route_segments_xyz) | ||
| lisbon_route_xyz <- elevation_add(lisbon_route, dem = dem_lisbon()) | ||
| lisbon_route_segments_xyz <- route_to_segments(lisbon_route_xyz) | ||
| lisbon_route_segments_xyz$slope <- slope_xyz(lisbon_route_segments_xyz) | ||
| summary(lisbon_route_segments_xyz$slope) | ||
| ``` | ||
|
|
||
| You can plot these segments, for example, colored by their slope. Here we use `tmap` for a more advanced plot (requires `tmap` package). | ||
|
|
||
| ```{r, eval=FALSE} | ||
| # Requires tmap package | ||
| # library(tmap) | ||
| # qtm(lisbon_route_segments_xyz, lines.col = "slope", lines.lwd = 3) | ||
| ```{r} | ||
| plot(st_geometry(lisbon_route_segments_xyz), | ||
| col = heat.colors(length(lisbon_route_segments_xyz$slope))[rank(lisbon_route_segments_xyz$slope)], | ||
| lwd = 3, main = "Slope by vertex segment" | ||
| ) | ||
| ``` | ||
|
|
||
| Alternatively, using base R graphics: | ||
| ### By fixed length (using stplanr) | ||
|
|
||
| `stplanr::line_segment()` splits the route into segments of a given length (e.g. 100 m). | ||
| Elevation must be added after segmenting, since the new endpoints won't have Z coordinates yet: | ||
|
|
||
| ```{r} | ||
| plot(st_geometry(lisbon_route_segments_xyz), col = heat.colors(length(lisbon_route_segments_xyz$slope))[rank(lisbon_route_segments_xyz$slope)], lwd = 3) | ||
| if (requireNamespace("stplanr", quietly = TRUE)) { | ||
| lisbon_route_100m <- stplanr::line_segment(lisbon_route, segment_length = 100) | ||
| lisbon_route_100m_xyz <- elevation_add(lisbon_route_100m, dem = dem_lisbon()) | ||
| lisbon_route_100m_xyz$slope <- slope_xyz(lisbon_route_100m_xyz) | ||
| summary(lisbon_route_100m_xyz$slope) | ||
| } | ||
| ``` | ||
|
|
||
| This vignette provides a basic overview. For more detailed information and advanced use cases, please refer to the other vignettes and the function documentation. | ||
|
|
||
| ```{r} | ||
| if (requireNamespace("stplanr", quietly = TRUE)) { | ||
| plot(st_geometry(lisbon_route_100m_xyz), | ||
| col = heat.colors(length(lisbon_route_100m_xyz$slope))[rank(lisbon_route_100m_xyz$slope)], | ||
| lwd = 3, main = "Slope by 100 m segment" | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| This vignette provides a basic overview. For more detailed information and advanced use cases, please refer to the other vignettes and the function documentation. | ||
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.
Would this be more specific and clear, seeing as we want a vector output?
Not tested but imagine it should work..
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.
yes, for a data.frame. but this is a matrix (from st_coordinates()), right? not sure if works the same way...