通過嘗試從單個點和單個多邊形獲取相交結果,我發現我認爲只能是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的方法,它非常討厭,它會在發生錯誤時捕獲錯誤,並且定義應該來自交叉函數的點索引。我相信有人會發現你的評論有用,但! –