我正在尝试将空间点数据连接到空间多边形数据。
不久前,发布了这个非常有用的线程: 如何使用 R 获取与点最近的关联多边形的信息?
我正在尝试使用 OP 发布的代码:
library(rgeos)
library(sp)
library(maptools)
library(rgdal)
library(sp)
ky.map <- readShapeSpatial("~/KYcounties.shp")
points.xy <- readShapeSpatial('~/points.shp') # Sample data of the points file
# is below.
我正在使用的功能是
IntersectPtWithPoly <- function(x, y) {
# Extracts values from a SpatialPolygonDataFrame with SpatialPointsDataFrame,
# and appends table (similar to ArcGIS intersect)
# Args:
# x: SpatialPoints*Frame
# y: SpatialPolygonsDataFrame
# Returns:
# SpatialPointsDataFrame with appended table of polygon attributes
# Set up overlay with new column of join IDs in x
z <- overlay(y, x)
# Bind captured data to points dataframe
x2 <- cbind(x, z)
# Make it back into a SpatialPointsDataFrame
# Account for different coordinate variable names
if(("coords.x1" %in% colnames(x2)) & ("coords.x2" %in% colnames(x2))) {
coordinates(x2) <- ~coords.x1 + coords.x2
} else if(("x" %in% colnames(x2)) & ("x" %in% colnames(x2))) {
coordinates(x2) <- ~x + y
}
# Reassign its projection if it has one
if(is.na(CRSargs(x@proj4string)) == "FALSE") {
x2@proj4string <- x@proj4string
}
return(x2)
}
并被称为
test <- IntersectPtWithPoly(points.xy, ky.map)
效果很好,除了我得到:
Warning message: 'overlay' is deprecated. Use 'over' instead.
以及用于连接多边形字段的一堆 NA。
很公平:
z <- over(y, x)
但这让我:
Error in data.frame(..., check.names = FALSE) : arguments imply differing
number of rows: 96, 121
下面是我正在使用的点数据示例(放入 QGIS 并导出为 shapefile):
structure(list(ID = c(5L, 11L, 33L, 41L, 73L, 119L, 175L, 206L,
216L, 229L), Latitude = c(38.239829, 38.9231075, 38.04169866,
38.13953849, 38.14800534, 37.85059283, 38.17820492, 38.00833019,
38.22189947, 36.87255699), Longitude = c(-85.746211, -84.4108925,
-84.49738797, -85.6776972, -85.02502406, -84.57452618, -85.56667237,
-84.42084641, -85.48653626, -84.22021486)), .Names = c("ID",
"Latitude", "Longitude"), row.names = c(NA, 10L), class = "data.frame")
我在 Google Drive 上分享了 shapefile:https ://docs.google.com/file/d/0B1bNxpx4XS8dSFJlOTlQSUw0LTg/edit?usp=sharing
想法?