r - 通过shapefile区域加入数据框坐标,也就是按位置加入属性

时间:2018-01-21 18:36:20

标签: r raster shapefile qgis

我有一个大型数据集,在R中加载为data.frame。它包含与坐标点相关的观测值(纬度/经度)。

我还有北美的形状文件。

在我的数据框中标有NA的空列(BCR已填充)中,我想根据shapefile插入每个坐标所属的区域名称。

我知道如何QGIS使用Vector> Data Management Tools> Join Attributes by Location

单击HERE可以下载shapefile。

我的数据现在看起来像这样(样本):

LATITUDE    LONGITUDE   Year    EFF n   St  PJ  day BCR
50.406752   -104.613    2009    1   0   SK  90  2   NA
50.40678    -104.61256  2009    2   0   SK  120 3   NA
50.40678    -104.61256  2009    2   1   SK  136 2   NA
50.40678    -104.61256  2009    3   2   SK  149 4   NA
43.0026385  -79.2900467 2009    2   0   ON  112 3   NA
43.0026385  -79.2900467 2009    2   1   ON  122 3   NA

但我希望它看起来像这样:

LATITUDE    LONGITUDE   Year    EFF n   St  PJ  day BCR
50.406752   -104.613    2009    1   0   SK  90  2   Prairie Potholes
50.40678    -104.61256  2009    2   0   SK  120 3   Prairie Potholes
50.40678    -104.61256  2009    2   1   SK  136 2   Prairie Potholes
50.40678    -104.61256  2009    3   2   SK  149 4   Prairie Potholes
43.0026385  -79.2900467 2009    2   0   ON  112 3   Lower Great Lakes/St.Lawrence Plain
43.0026385  -79.2900467 2009    2   1   ON  122 3   Lower Great Lakes/St.Lawrence Plain

请注意,BCR列现在已填充相应的BCR区域名称。

到目前为止,我的代码只是导入和格式化数据和shapefile:

library(rgdal)
library(proj4)
library(sp)
library(raster)

# PFW data, full 2.5m observations
df = read.csv("MyData.csv")

# Clearning out empty coordinate data
pfw = df[(df$LATITUDE != 0) & (df$LONGITUDE != 0) & (!is.na(df$LATITUDE)) & (!is.na(df$LATITUDE)),]

# Creating a new column to be filled with associated Bird Conservation Regions
pfw["BCR"] = NA

# Making a duplicate data frame to conserve data
toSPDF = pfw

# Ensuring spatial formatting
#coordinates(toSPDF) = ~LATITUDE + LONGITUDE
SPDF <- SpatialPointsDataFrame(toSPDF[,c("LONGITUDE", "LATITUDE"),],
                                  toSPDF,
                                  proj4string = CRS("+init=epsg:4326"))

# BCR shape file, no state borders
shp = shapefile("C:/Users/User1/Desktop/BCR/BCR_Terrestrial_master_International.shx")
spPoly = spTransform(shp, CRS("+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

# Check
isTRUE(proj4string(spPoly) == proj4string(SPDF))

# Trying to join attributes by location
  #try1 = point.in.polygon(spPoly, SPDF) # Sounds good doesn't work
  #a.data <- over(SPDF, spPoly[,"BCRNAME"]) # Error: cannot allocate vector of size 204.7 Mb

1 个答案:

答案 0 :(得分:2)

我认为您希望使用点和多边形进行空间查询,以将多边形属性指定给点。您可以使用raster::extractsp::over

示例数据:

library(raster)
pols <- shapefile(system.file("external/lux.shp", package="raster")) 
set.seed(20180121) 
pts <- data.frame(coordinates(spsample(pols, 5, 'random')), name=letters[1:5])
plot(pols); points(pts)

解决方案:

e <- extract(pols, pts[, c('x', 'y')]) 
pts$BCR <- e$NAME_2 

pts
#         x        y name              BCR
#1 6.009390 49.98333    a            Wiltz
#2 5.766407 49.85188    b          Redange
#3 6.268405 49.62585    c       Luxembourg
#4 6.123015 49.56486    d       Luxembourg
#5 5.911638 49.53957    e Esch-sur-Alzette
相关问题