Comments (6)
There are some functions in the rgeos package that take a fraction and splits a line into segments based on the fraction of the line length. From a quick look I couldn't find if the function that splits the line is exported but it might be worth a look?
from stplanr.
Definitely worth a look - can't find anything here though... any idea: http://geos.osgeo.org/doxygen/namespaces.html
from stplanr.
Also trip::interpequal but it's buried deep. trajectories has some thing too. Just fyi, but happy to help if needed
from stplanr.
Hi @mdsumner your input will be most appreciated. Starting point: try to reproduce this plot:
This was created with the following code which I've just put in a pull request. If you could help document, test, break this new line_segment()
function that would be great, or suggest (perhaps more efficient) alternative implementations, that would be really useful...
#' Find the mid-point of lines
#'
#' This is a wrapper around \code{\link{SpatialLinesMidPoints}} that allows it to find the midpoint
#' of lines that are not projected, which have a lat/long CRS.
#' @inheritParams line2df
#' @export
#' @examples
#' l = routes_fast
line_midpoint = function(l){
gprojected(l, maptools::SpatialLinesMidPoints)
}
#' Calculate length of lines in geographic CRS
#' @inheritParams line2df
#' @param byid Logical determining whether the length is returned per object (default is true)
#' @export
line_length = function(l, byid = TRUE){
gprojected(l, rgeos::gLength, byid = byid)
}
#' Divide SpatialLines dataset into regular segments
#' @inheritParams line2df
#' @param n_segments The number of segments to divide the line into
#' @param segment_length The approximate length of segments in the output (overides n_segments if set)
#' @export
#' @examples
#' l = routes_fast[1,]
#' l_seg2 = line_segment(l = l, n_segments = 2)
#' plot(l_seg2, col = l_seg2$group, lwd = 50)
#' l_seg5 = line_segment(l = l, n_segments = 5)
#' plot(l_seg5, col = l_seg5$group, lwd = 30, add = TRUE)
#' l_seg100m = line_segment(l = l, segment_length = 100)
#' plot(l_seg100m, col = l_seg100m$group, lwd = 10, add = TRUE)
#' plot(l, col = "white", add = TRUE)
#' line_segment(l = l, segment_length = 100)
line_segment = function(l, n_segments, segment_length = NA){
if(!is.na(segment_length)){
l_length = line_length(l)
n_segments = round(l_length / segment_length)
}
if(n_segments == 2){
pseg = line_midpoint(l)
} else {
pseg = spsample(x = l, n = n_segments - 1, type = "regular")
}
l_geom = raster::geom(l)
l_coords = l_geom[, c("x", "y")]
knn_res = nabor::knn(data = l_coords, query = coordinates(pseg), k = 1)
sel_nearest = c(knn_res$nn.idx)
for(i in 1:(length(sel_nearest) + 1)){
ids = c(1, sel_nearest, nrow(l))
if(i == 1){
l_seg = points2line(l_coords[ids[i]:ids[(i + 1)],])
spChFIDs(l) = i
} else if(i == length(sel_nearest) + 1){
l_temp = points2line(l_coords[ids[i]:nrow(l_coords),])
spChFIDs(l_temp) = i
l_seg = maptools::spRbind(l_seg, l_temp)
} else {
l_temp = points2line(l_coords[ids[i]:ids[(i + 1)],])
spChFIDs(l_temp) = i
l_seg = maptools::spRbind(l_seg, l_temp)
}
}
SpatialLinesDataFrame(l_seg, data.frame(group = 1:i))
}
from stplanr.
Thank you for this, it's certainly something I want to explore - not sure yet if I can improve on this but I'll let you know how it goes. I think it can be a bit simpler by calculating "node" and "measure" space, and then applying approxfun across those to get the start and ends right while maintaining all the input vertices. It's also similar to trip::cut, which really gave me a headache and I've been loathe to revisit :), but maybe I could generalize so that distance or time (or other measure) is applied. I'd like to have better mastery over these "continuous" vs. "discrete" views of Spatial objects, and I've talked with a colleague about an "edge-index" approach (now in rangl) being used for dynamic vertex densification for different map projections (like the D3 flawed example stuff), and (I think) like what ggplot2's coord_munch is doing. I know this is a bit tangential, but I think it's important and that the specific need here has broader potential use in a lot of applications.
This is already a very interesting solution here, btw.
from stplanr.
Job done for now I'd say so closing.
from stplanr.
Related Issues (20)
- Add arguments to rnet_merge() preventing sideroads getting values of main roads HOT 3
- Speed-up `rnet_merge()` HOT 29
- GitHub actions failing
- Comparing the results between angle_diff with calculate_angle/get_vector HOT 1
- Error message in bind_sf
- CRAN issues
- bug in the rnet_merge function when defining the funs HOT 3
- Use different buffer options in `rnet_merge()`
- Convert large GeoJSON file to PMTiles HOT 1
- bug in geo_buffer HOT 7
- Possible speed enhancement to `mats2line()` HOT 3
- Invalid LineStrings in routes_fast_sf HOT 1
- Use `od::odc_to_sfc()` do the legwork in `mats2line()` HOT 3
- Bug in `line_segment()` when using certain values on projected data with `rsgeo` implementation HOT 8
- `rnet_merge()` fails when inputs are projected HOT 4
- `line_bearing()` is slow HOT 3
- Argument of segment_length in line_segment fun causes issue HOT 14
- Tried creating a route from desirelines using osrm function HOT 9
- rnet merge function can't not handle attributes containing strings HOT 1
- Add links to more papers in DESCRIPTION
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stplanr.