Skip to content
Advertisement

Intersect two sf data.frames based on date and geometry using R

So, I have two R “sf” “data.frames”, one with millions of linestring geometries (vsr_segments: see below) and the other with 5 polygons (vsr_zones: see below). Each linestring has a datetime and each polygon has a unique date range.

I’m trying to intersect the linestrings dataframe with the polygon data.frame based on whether the linestring datetime falls within a specific polygon’s date range. Basically, if this linestring datetime is within any of the polygons’ date ranges, perform the intersect with the appropriate polygon, and return a sf data.frame of the linestrings that intersect.

I have a sql query that essentially does this but this only works on my postgres database.

source: https://postgis.net/2014/03/14/tip_intersection_faster/

I’m curious if there’ a better way to do this. It’s simple, but when new data comes in, I have to drop the table, create a new one, and create new indexes.

I’d rather have a way where I only run this datetime within date range spatial intersection with new data (sf data.frame with ~5000 linestrings) and append the resulting data.frame to the existing database table. Is there an r way to do this? I’ve tried sqldf to perform the query below with my r data.frames versus performing the query on my database. Any assistance would be very appreciated.

sample data

Advertisement

Answer

A two-step approach could be to do a range join (or non-equi join) to find which rows of data overlap by date range, then a pair-wise geometry intersection to tell you whether they intersect spatially or not

I like using library(data.table) for all joining operations because of its superior memory management and speed

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement