"gIntersection" 是否有更快的替代方案?

Is there a quicker alternative to "gIntersection"?

我需要快速确定空间多边形和空间线是否相交。我目前正在将多边形转换为空间线并使用 gIntersection()。谁能建议一种可能更快的方法?也许使用栅格而不是空间线或其他东西。我需要这样做数千次。

# .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))

其中 gIntersection returns 具有交集的几何图形,gIntersects returns 表示两个几何图形是否相交的逻辑。

感谢 Edzer 和其他人的输入。

我 运行 对您的建议进行了一些测试,看来 gIntersects() 有很大的不同。先将多边形转为空间线与直接使用多边形基本相同

测试结果如下:

# Original approach
system.time({           
    Pt1 = list(x = c(long1, long2), y = c(lat1, lat2))
    SpatialLine1 = SpatialLines(list(Lines(Line(cbind(Pt1$x,Pt1$y)), "L1")))
    cross <- length(gIntersection(SpatialLine1, SL))
})

##  user  system elapsed 
##  0.53    0.00    0.53 

# Edzer suggestion: using gIntersects
system.time({           
    Pt1 = list(x = c(long1, long2), y = c(lat1, lat2))
    SpatialLine1 = SpatialLines(list(Lines(Line(cbind(Pt1$x,Pt1$y)), "L1")))
    cross <- (gIntersects(SpatialLine1, SL))
})  

#   user  system elapsed 
#   0.06    0.00    0.06            

# Edzer suggestion 2: using a polygon rather that spacial lines
system.time({           
    Pt1 = list(x = c(long1, long2), y = c(lat1, lat2))
    SpatialLine1 = SpatialLines(list(Lines(Line(cbind(Pt1$x,Pt1$y)), "L1")))
    cross <- length(gIntersection(SpatialLine1, polygon1filled))
})  

#   user  system elapsed 
#   0.43    0.05    0.48            

# Edzer suggestion 1&2: using a polygon rather that spacial lines and gIntersects
system.time({           
    Pt1 = list(x = c(long1, long2), y = c(lat1, lat2))
    SpatialLine1 = SpatialLines(list(Lines(Line(cbind(Pt1$x,Pt1$y)), "L1")))
    cross <- (gIntersects(SpatialLine1, polygon1filled))
})              

#   user  system elapsed 
#   0.06    0.00    0.07