2017-08-10 88 views
0

通過嘗試從單個點和單個多邊形獲取相交結果,我發現我認爲只能是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多邊形相交的相關錯誤,當點帶有要在交集過程中返回的屬性時。

您可能會問,爲什麼您實際需要返回屬性,如果只有一個點,這是因爲它是一個迭代過程,它可能不是一個點,它可能不是一個或多個。

我希望有人解釋這個錯誤或確認我的發現。

回答

0

對不起,我不能回答你的相交錯誤的問題,但它可能是簡單的,現在使用SP ::在返回多邊形屬性點

# dummy polygon 
xym <- as.matrix(data.frame(x=c(16.48438,17.49512,24.74609,22.59277,16.48438), 
y=c(59.73633,55.12207,55.03418,61.14258,59.73633))) 

# make into SpatialPolygon 
p = Polygon(xym) 
ps = Polygons(list(p),1) 
sps = SpatialPolygons(list(ps)) 

# Promote to SPDF and give an attribute 
SPDF = SpatialPolygonsDataFrame(sps, data.frame(N = "hello", row.names = 1)) 

# make 2 points, one inside the polygon and one outside 
p <- data.frame(x=c(16,18),y=c(58,58)) 
coordinates(p) <- ~x + y 

# plot to check 
plot(sps) 
plot(p,add=T) 

# perform the over, returns a named vector for every point in the SpatialPoints 
res <- unname(over(p,SPDF)) 

# promote points to SpatialPointsDataFrame and put in new polygon attribute 
data <- data.frame(ID=row.names(p),pol=res) 
sp <- SpatialPointsDataFrame(p, data) 
+0

謝謝,我實際上設計了一個解決bug的方法,它非常討厭,它會在發生錯誤時捕獲錯誤,並且定義應該來自交叉函數的點索引。我相信有人會發現你的評論有用,但! –

1

這裏是您的示例數據更簡潔辦法。

library(raster) 

pnt <- SpatialPoints(cbind(530124, 176950)) 
pol <- spPolygons(matrix(c(530106.8, 530127.5, 530127.5, 530106.8, 530106.8, 176953.3, 176953.3, 176933.3, 176933.3, 176953.3), ncol=2)) 

現在說明問題。

intersect(pol, pnt) 
#class  : SpatialPolygons 
#features : 1 
#extent  : 530106.8, 530127.5, 176933.3, 176953.3 (xmin, xmax, ymin, ymax) 
#coord. ref. : NA 

# this fails 
intersect(pnt, pol) 
#Loading required namespace: rgeos 
#Error in j[, 2] : incorrect number of dimensions 

# but it works with two points! 
intersect(bind(pnt, pnt), pol) 
#class  : SpatialPoints 
#features : 2 
#extent  : 530124, 530124, 176950, 176950 (xmin, xmax, ymin, ymax) 
#coord. ref. : NA 

這是另一個drop=TRUE錯誤引起的由「滴」矩陣將R默認載體當選擇單個行。這在光柵版本2.6-11中已修復(尚未在CRAN上)。