I have an sf dataset with two geometry columns. Here's what it looks like:
> trip_geo
dstid sourceid dest_geom source_geom
1 1 1 MULTIPOLYGON (((-2.607985 5... MULTIPOLYGON (((-2.607985 5...
2 1 2 MULTIPOLYGON (((-2.607985 5... MULTIPOLYGON (((-2.57022 51...
3 1 3 MULTIPOLYGON (((-2.607985 5... MULTIPOLYGON (((-2.593213 5...
4 1 4 MULTIPOLYGON (((-2.607985 5... MULTIPOLYGON (((-2.608686 5...
5 1 5 MULTIPOLYGON (((-2.607985 5... MULTIPOLYGON (((-2.512852 5...
The active geometry is dest_geom
.
Each row corresponds to a trip between neighbourhoods. For each trip, I want to know which neighbourhoods lie in the way of the trip. That is, if you were to draw a straight line between source_geom and dest_geom for each row, which geometries would touch this straight line? I want to get all of the touching geometries for the row and then union them.
I have another dataset with the geometry corresponding to each id:
> id_geo
dstid geometry
1 1 MULTIPOLYGON (((-2.607985 5...
2 2 MULTIPOLYGON (((-2.57022 51...
3 3 MULTIPOLYGON (((-2.593213 5...
4 4 MULTIPOLYGON (((-2.608686 5...
5 5 MULTIPOLYGON (((-2.512852 5...
I imagine that the first step would be to define a line between the centroids of source_geom and dest_geom for each trip. Then, create an sf where the geometry column contains a list of the polygons which touch the line (I don't know if it's possible to have several geometries in one column in sf). Then, union the geometries contained in the same row/list.
I don't think the way Im approaching the problem is correct because, to the best of my knowledge, one cannot carry out operations on two geometries of an sf, such as defining a line. Additionally, I don't know how I would integrate a list to a dataframe/sf.
If you could suggest a more realistic way of approaching the problem, I would greatly appreciate it.
dput(dplyr::sample_n(trip_geo, 15))
as well as the rows inid_geo
that correspond to the IDs in that sample of 15 rows fromtrip_geo
. This way people can paste your data into R and try to provide a solution. – Eugene Chongdput
forsf
objects is very long. I'd try to break your analysis into smaller pieces and make simple dummy data that people could copy and paste to help you out – astrofunkswag