ropensci / stplanr

Sustainable transport planning with R
https://docs.ropensci.org/stplanr
Other
417 stars 66 forks source link

Error in UseMethod("line2points") when using `stplanr::line_bearing` #493

Closed dncgst closed 11 months ago

dncgst commented 2 years ago

Dear all, I get this error when using stplanr::line_bearing with a spatial line object:

Error in UseMethod("line2points") : no applicable method for 'line2points' applied to an object of class "c('SpatialLinesDataFrame', 'SpatialLines', 'Spatial')"

I have a simple feature collection of LINESTRINGs (lines of 2 points each) and dimension XY, no CRS.

blank.ID geometry

1 1 (2.785004 6.564484, 3.656266 6.134538) 2 2 (-1.371373 4.042816, 0.4690544 2.776325) 3 3 (-3.291259 8.761345, -3.111156 8.934315) 4 4 (-3.373221 7.226384, -2.959783 7.827772) 5 5 (-2.31 12.6, -2.29 12.6) 6 6 (-4.488094 6.877866, -4.227137 6.706195) 7 7 (-2.301094 9.551979, -1.879827 9.198969) 8 8 (-3.841821 5.84188, -3.903458 5.88047) 9 9 (-2.37 14.22, -2.44 14.32) 10 10 (-2.646972 6.820567, -4.906757 6.394592) I want to calculate the bearing angle of each line. ``` foo <- foo %>% sf::as_Spatial() bearing.deg <- stplanr::line_bearing(l = foo, bidirectional = TRUE) ```
Robinlovelace commented 2 years ago

Do you have a reproducible example? If you can provide code starting with reading in an open access dataset, or a dataset in stplanr, plus your expectation of what the result should find, that will help.

Here's my starting point, does this work as you expected?

library(stplanr)
?line_bearing
bearings_sf_1_9 <- line_bearing(flowlines_sf[2:5, ])
bearings_sf_1_9 # lines of 0 length have NaN bearing
line_bearing(flowlines_sf[2:5, ], bidirectional = TRUE)

See here for full documentation: https://docs.ropensci.org/stplanr/reference/line_bearing.html

JosiahParry commented 11 months ago

sf::as_Spatial() creates an sp class object. The error is emitted because line2points() is an s3 generic. line_bearing() is not designed to be used with sp objects. This can probably be closed as designed.

Robinlovelace commented 11 months ago

Agreed, and yes, support for sp was phased out quite a while ago. Closing :tada: