Search code examples
rgisr-sp

Is there a quicker alternative to "gIntersection"?


I need to rapidly determine if a spatial polygon and a spatial line intersect. I am currently converting the polgon to a spatial line and using gIntersection(). Can anyone suggest a potentially quicker method? Perhaps using rasters instead of spatial line or something. I need to do this many thousands of times.

# .shp file to Spatial Line
polygon1 <- readShapeSpatial("C:.../SALandmass.shp")
polygon1filled <- SpatialPolygons(list(Polygons(list(polygon1@polygons[[1]]@Polygons[[1]]),ID=1)))
SL <- as(polygon1filled, "SpatialLines")


# Test if line between two coordinates cross the shape
Pt1 = list(x = c(CurrentLong, MapCoordsm$x[i]), y = c(CurrentLat, MapCoordsm$y[i]))
SpatialLine1 = SpatialLines(list(Lines(Line(cbind(Pt1$x,Pt1$y)), "L1")))
cross <- length(gIntersection(SpatialLine1, SL))

Solution

  • Where gIntersection returns a geometry with the intersection, gIntersects returns a logical indicating whether two geometries intersect.