Giter Club home page Giter Club logo

Comments (6)

richardellison avatar richardellison commented on May 25, 2024

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.

Robinlovelace avatar Robinlovelace commented on May 25, 2024

Definitely worth a look - can't find anything here though... any idea: http://geos.osgeo.org/doxygen/namespaces.html

from stplanr.

mdsumner avatar mdsumner commented on May 25, 2024

Also trip::interpequal but it's buried deep. trajectories has some thing too. Just fyi, but happy to help if needed

from stplanr.

Robinlovelace avatar Robinlovelace commented on May 25, 2024

Hi @mdsumner your input will be most appreciated. Starting point: try to reproduce this plot:

screenshot from 2016-08-31 09-01-10

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.

mdsumner avatar mdsumner commented on May 25, 2024

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.

Robinlovelace avatar Robinlovelace commented on May 25, 2024

Job done for now I'd say so closing.

from stplanr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.