使用over()函数R后保存新形状文件时出错
我试图在R中使用sp package中的over函数,将多边形层中的属性值添加到点层中的新列,并将生成的层保存为新的形状文件。除保存新形状文件的最后一步外,所有接缝都将起作用使用over()函数R后保存新形状文件时出错,r,R,我试图在R中使用sp package中的over函数,将多边形层中的属性值添加到点层中的新列,并将生成的层保存为新的形状文件。除保存新形状文件的最后一步外,所有接缝都将起作用 library(rgdal) library(sp) county <- readOGR(dsn=home ,layer="county") # reads in polygon layer with counties species <- readOGR(dsn=hom
library(rgdal)
library(sp)
county <- readOGR(dsn=home ,layer="county") # reads in polygon layer with counties
species <- readOGR(dsn=home, layer="species") # reads point layer with with some species observations
species@data$county <- over(species, county[,1]) # adds according county name to every observation
writeOGR(trans_nh, dsn=home, layer ="nh_maakond", driver="ESRI Shapefile")
以防我在应用over()之后添加str(物种)的输出
str(species)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 3700 obs. of 4 variables:
.. ..$ LAD_NIMI: Factor w/ 19 levels "Chiroptera sp.",..: 13 1 2 4 6 1 11 1 2 2 ...
.. ..$ andmed : Factor w/ 401 levels "Eelisesse","EELIS_II_kat",..: 1 1 2 1 1 1 1 1 2 2 ...
.. ..$ Andmed_1: Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
.. ..$ county :'data.frame': 3700 obs. of 1 variable:
.. .. ..$ MNIMI: Factor w/ 15 levels "Harju maakond",..: 2 2 2 2 2 2 2 2 2 2 ...
..@ coords.nrs : num(0)
..@ coords : num [1:3700, 1:2] 397642 397642 400479 401127 401127 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
..@ bbox : num [1:2, 1:2] 388929 6381608 738925 6618427
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
.. .. ..@ projargs: chr "+proj=lcc +lat_1=58 +lat_2=59.33333333333334 +lat_0=57.51755393055556 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +units=m "| __truncated__
你知道我做错了什么吗?
谢谢你的努力
我又多玩了一点,我又多走了一步。正如Koekenbakker在一篇评论中所建议的那样,我试图获取的数据以某种方式被添加为数据框中的data.frame。我添加了一个额外的步骤,并丢失了over()函数的一个参数,它成功了。代码如下:
图书馆(rgdal)
图书馆(sp)
country看起来您的country
数据不是以向量的形式出现,而是以数据帧(@data
)中的data.frame的形式出现。试着改变你的over()。谢谢
str(species)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 3700 obs. of 4 variables:
.. ..$ LAD_NIMI: Factor w/ 19 levels "Chiroptera sp.",..: 13 1 2 4 6 1 11 1 2 2 ...
.. ..$ andmed : Factor w/ 401 levels "Eelisesse","EELIS_II_kat",..: 1 1 2 1 1 1 1 1 2 2 ...
.. ..$ Andmed_1: Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
.. ..$ county :'data.frame': 3700 obs. of 1 variable:
.. .. ..$ MNIMI: Factor w/ 15 levels "Harju maakond",..: 2 2 2 2 2 2 2 2 2 2 ...
..@ coords.nrs : num(0)
..@ coords : num [1:3700, 1:2] 397642 397642 400479 401127 401127 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
..@ bbox : num [1:2, 1:2] 388929 6381608 738925 6618427
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
.. .. ..@ projargs: chr "+proj=lcc +lat_1=58 +lat_2=59.33333333333334 +lat_0=57.51755393055556 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +units=m "| __truncated__
county <- readOGR(dsn=home ,layer="county") # reads in polygon layer with counties
species <- readOGR(dsn=home, layer="species") # reads point layer with with some species observations
species@data$county <- over(species, count) # adds according county name to every observation
trans_nh@data$county <- trans_nh@data$county$MNIMI # the new step added
writeOGR(trans_nh, dsn=home, layer ="nh_maakond", driver="ESRI Shapefile")