-
Notifications
You must be signed in to change notification settings - Fork 66
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
od2line() throws an error if only one origin-destination relation is selected #562
Comments
Hey @yannikbuhl thanks for the message and detailed example. Quickfire follow-up question: have you tried At some point I'd like to replace the current |
Hi Robin, thanks for the quick response. I have not tried |
A quick test with the minimal reproducible example tells me that replacing
|
Awesome, glad it worked. Do close this issue if fixed for you and wish me luck updating |
In fact may be useful to keep open to track updating |
Hello,
I have recently been working with
stplanr
to create and plot desire lines. It worked fine almost all the times (thank you so much for this wonderful package!). However, in some edge cases I was tripping over an error. I have been able to trace it back to its origin and I am also able to suggest a solution. I will provide a minimal working example, too.The error occurs if one only wants to create OD desire lines between two polygons/points. Its origins lie in
od2line.sf()
, more specifically in this chunk of code:Here, the object
coords_o
is of type"matrix" "array"
. Whenorigin_matches
only contains one single index, the matrix subsetting in the last row of the above code does not return a matrix/array, but a named numeric vector. The same applies for the code that deals withdest_points
. One of the last actions of the function is to bind these two objects together:odm <- cbind(origin_points, dest_points)
However, if both of them are named vectors, the result will not be a matrix with one row and four columns named X Y X Y, but a 2x2 dataframe. Subsequently, the code fails once the next line is executed, because the following function
od_coords2line
and its sub-functions access the objectodm/odc
by subsettingodc[, 3:4]
when checking forNA
s in one of the OD coordinates:odsfc <- od_coords2line(odm, crs = sf::st_crs(zones), remove_duplicates = FALSE)
This leads to the following error:
Error in odc[, 3:4] : subscript out of bounds
.What follows is my minimal reproducible example based on
stplanr
s vignette:I think the easiest solution to the problem could be to use the argument
drop = FALSE
to preserve the matrix structure when subsetting the objectcoords_o
to create the objectsdest_points
andorigin_points
, like so (thanks to @staudtlex for hinting at that):Please let me know if you can reproduce the error and what you think of the solution. I have tested it and it worked in my case. I work on Windows and I am running R 4.4.0 but I have encountered this problem on R 4.1.0, too. If you wish, I can provide a PR to solve the problem.
The text was updated successfully, but these errors were encountered: