使用over()函数R后保存新形状文件时出错

时间:2014-05-14 21:19:00

标签: r

我正在尝试使用R中sp包的over函数,将多边形图层中的属性值添加到点图层中的新列,并将生成的图层另存为新的形状文件。除了保存新形状文件的最后一步之外,所有接缝都能正常工作。

   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")

直到writeOGR命令正常工作的所有步骤,但是当我尝试保存新的形状文件(或mapinfo)时,我收到以下错误:

Error in writeOGR(species, dsn = home, layer = "species_county", driver = "ESRI Shapefile") : 
  Can't convert columns of class: data.frame; column names: county

以防万一我在应用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)

    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")

我不确定它是否应该像那样工作,或者它只是一种解决方法。无论如何我完成了它。感谢。

0 个答案:

没有答案