Skip to content
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

Element-by-element distance #437

Closed
barryrowlingson opened this issue Jul 24, 2017 · 3 comments
Closed

Element-by-element distance #437

barryrowlingson opened this issue Jul 24, 2017 · 3 comments

Comments

@barryrowlingson
Copy link
Contributor

barryrowlingson commented Jul 24, 2017

st_distance(x,y) returns the full matrix of distances from all x to all y. Sometimes we might have two geometry vector - maybe source and destination - and want to compute the distance from each source to its corresponding destination.

This can be done with sapply(1:length(g1),function(i)st_distance(g1[[i]],g2[[i]])) if g1 and g2 are geometry vectors with the same length, and returns a vector of the same length. But maybe a "element_wise" parameter to st_distance that changes its behaviour might be a nice thing to have.

Would ideally use R's usual vector recycling rules if lengths don't match, with the usual warnings on vectors that don't repeat nicely.

I think this is even easier:

> mapply(st_distance, g1, g2)
[1] 152687.52 192552.37 297678.52 262699.58  76465.13
Warning message:
In mapply(st_distance, g1, g2) :
  longer argument not a multiple of length of shorter

Seems easy enough, maybe not worth adding the complexity of a new argument, but people will try things like this: https://gis.stackexchange.com/questions/249762/is-there-a-convenient-way-to-calculate-distances-between-two-geometry-columns if there's no way documented.

@edzer
Copy link
Member

edzer commented Jul 24, 2017

Good idea, but how to call it - would pairwise be a better description? mapply does the recycling already.

@barryrowlingson
Copy link
Contributor Author

Maybe st_distance(g1, g2, by_element=TRUE), in a similar (but not identical) way that byid in gDistance works.

How would you do this with ST_Distance in PostGIS or SpatiaLITE? I don't have a spatial DB handy...

@edzer edzer closed this as completed in 3f92bb1 Jul 24, 2017
@edzer
Copy link
Member

edzer commented Jul 24, 2017

I have no clue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants