我需要快速确定空间多边形和空间线是否相交。我目前正在使用gIntersection()
将polgon转换为空间线。任何人都可以建议一种更快的方法吗?也许使用栅格而不是空间线或其他东西。我需要这么做几千次。
# .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))
答案 0 :(得分:2)
gIntersects
返回带有交集的几何,2015-08-26 15:17:29,195 - [DEBUG] - from application in main
Loading timeout value into cache from configuration for key DEFAULT: Not configured, falling back to default.
2015-08-26 15:17:29,519 - [DEBUG] - from application in main
Loading timeout value into cache from configuration for key node_refresh: Not configured, falling back to default.
2015-08-26 15:17:29,727 - [INFO] - from play in main
Application started (Prod)
2015-08-26 15:17:29,754 - [INFO] - from play in main
Listening for HTTP on /0.0.0.0:9000
2015-08-26 15:17:36,936 - [DEBUG] - from application in play-akka.actor.default-dispatcher-2
Loading timeout value into cache from configuration for key sources_all: Not configured, falling back to default.
返回一个逻辑,指示两个几何是否相交。
答案 1 :(得分:2)
感谢输入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