通过尝试从单个点和单个多边形获取相交结果,我发现我认为只能是R栅格中的一个错误包相交功能。当1个多边形与1个点相交时,R栅格包相交函数中的错误很可能
我有1个多边形1点,并使用交叉如下:
intersect(a_point, a_polygon)
凡a_point
包含一个id属性。这种失败,出现错误:
Error in j[, 2] : incorrect number of dimensions
但是,如果我扭转参数,并做到:
intersect(a_polygon, a_point)
它工作正常,但是从点状文件的一部分不返回ID我需要的结果。这是预期的行为,非常好,但我需要它以相反的方式工作。
为了排除我的多边形或点数据存在一些特殊性,我创建了一个单一的多边形和单点空间对象,并测试了相同的假设,并发生了与上述“原始”对象相同的结果。
以下是生成的完整性,因此这两个“假”对象的代码,它可以被复制:
test_list_x = list(530124, 530125) #For when I use 2 points
test_list_y = list(176949, 176950) #For when I use 2 points
data_frame_object = data.frame(530124, 176950)
names(data_frame_object) = c("Longitude", "Latitude")
coordinates(data_frame_object)=~Longitude+Latitude
proj4string(data_frame_object)=CRS("+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894")
fake_point_shape_object=SpatialPointsDataFrame(data_frame_object, data.frame(id=1:length(data_frame_object)))
coords = matrix( nrow=5, ncol=2)
coords[1,1] = 530106.8
coords[1,2] = 176953.3
coords[2,1] = 530127.5
coords[2,2] = 176953.3
coords[3,1] = 530127.5
coords[3,2] = 176933.3
coords[4,1] = 530106.8
coords[4,2] = 176933.3
coords[5,1] = 530106.8
coords[5,2] = 176953.3
my_fake_polygon = Polygon(coords)
polygon_list = list(my_fake_polygon)
polygon_set <- lapply(seq_along(polygon_list), function(i) Polygons(list(polygon_list[[i]]), i ))
new_polygons <- SpatialPolygons(polygon_set)
[email protected] = CRS("+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894")
df <- data.frame("1")
names(df) = "id"
my_fake_polygon <- SpatialPolygonsDataFrame(new_polygons,df)
现在,这里的东西,如果我创建2个相邻(所以他们都在多边形内)而不是一个,它工作正常,没有错误。建议有一个与1点和1多边形相交的相关错误,当点带有要在交集过程中返回的属性时。
您可能会问,为什么您实际需要返回属性,如果只有一个点,这是因为它是一个迭代过程,它可能不是一个点,它可能不是一个或多个。
我希望有人解释这个错误或确认我的发现。
谢谢,我实际上设计了一个解决bug的方法,它非常讨厌,它会在发生错误时捕获错误,并且定义应该来自交叉函数的点索引。我相信有人会发现你的评论有用,但! –