2

我需要快速确定空间多边形和空间线是否相交。我目前正在将 polgon 转换为空间线并使用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))
4

2 回答 2

2

wheregIntersection返回一个几何与交集,gIntersects返回一个逻辑,指示两个几何是否相交。

于 2015-08-26T13:37:45.790 回答
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
于 2015-08-28T07:05:44.627 回答