【问题标题】:How to subset a shapefile如何对 shapefile 进行子集化
【发布时间】:2014-11-17 02:05:22
【问题描述】:

shapefile“Property1A”的数据集是:

 df
#      suburb        area asst
# 0     Te Aro   14.541780 R076
# 1     Te Aro    7.655428 R076
# 2     Te Aro 3189.976134 <NA>
# 3     Te Aro  242.173386 0132
# 4     Karori  537.697290 R003
# 5    Pipitea 1116.954993 R105
# 6    Mt Cook  106.099900 R151
# 7     Te Aro   83.307421 R057
# 8 Aro Valley  544.665788 R105
# 9 Aro Valley   59.726882 R110

当我尝试对郊区“Te Aro”的 shapefile 进行子集化时,我使用的代码是:

TeAro <- subset(Property1A, suburb == "Te Aro") 

但是有一个错误:

TeAro <- subset(Property1A, suburb == "Te Aro")
# Error in eval(expr, envir, enclos) : object 'suburb' not found

为什么会出错?

【问题讨论】:

  • 请提供可重现的示例。您是否创建了一个名为 Property1A 的对象(您的示例表明您有一个名为 df 的对象)?你是怎么读到这个形状文件的?
  • df
  • Property1A 是一个 shapefile
  • 你是什么意思“Property1A”是一个shapefile。你是怎么读到R的?
  • Property1[Property1$suburb %in% "Te Aro",]

标签: r shapefile


【解决方案1】:

subset 名称应该可以工作。如果您使用readOGR 阅读,则不需要shapefiles 包:

> require(rgdal)
Loading required package: rgdal
Loading required package: sp
rgdal: version: 0.8-14, (SVN revision 496)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.0, released 2011/12/29
Path to GDAL shared files: /usr/share/gdal/1.9
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 470]
Path to PROJ.4 shared files: (autodetected)

将印度各州的 169 个多边形变成一个空间对象:

> India = readOGR(".","india_state")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "india_state"
with 169 features and 3 fields
Feature type: wkbPolygon with 2 dimensions

现在按名称子集:

> Gujarat = subset(India, NAME=="Gujarat")
> dim(Gujarat)
[1] 12  3
> dim(India)
[1] 169   3

我可以绘制这些对象,它们映射得很好:

> plot(Gujarat)

如果没有您的数据或不知道您拥有什么版本的 R 和软件包,您就无法知道为什么会失败。

包:rgdal 版本:0.8-14

包装:sp 版本:1.0-14

R 版本 3.0.2....

【讨论】:

    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2020-10-25
    • 2020-04-22
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多