感谢 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