使用 R 操作 shapefile 属性表 [英] Manipulate shapefile attribute table using R

查看:48
本文介绍了使用 R 操作 shapefile 属性表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布了这个问题GIS 堆栈交换,但没有看到太多流量.

I've posted this question on the GIS stack exchange, but it's not seeing much traffic.

我是一名 GIS 用户,多年来一直使用 R 进行统计,我很高兴看到发布了许多新的 GIS 功能(栅格、shapefile、rgdal 等).

I'm a GIS user who's been using R for stats for a few years, and I'm excited to see a lot of new GIS capabilities being released (raster, shapefiles, rgdal, etc).

我在 R 中完成了大量的数据库和表操作,因此能够在 shapefile 中添加和删除属性是一个强大的潜力.

I've done a lot of database and table manipulation in R, so being able to add and remove attributes from shapefiles is a powerful potential.

我希望我只是错过了那里的内容,但我似乎找不到从 shapefile 属性表中添加或删除属性的好方法.

I'm hoping I'm just missing what's out there, but I can't seem to find a good method for adding or removing attributes from a shapefile attribute table.

Overflow 的任何人都可以回复我的 GIS 帖子吗?或者我可以被告知如何将我的列表属性表放入数据框中并返回以替换当前的属性表?

Can anyone from Overflow respond to my GIS post? Or can I be advised how to get my list attribute table into a dataframe and back to replace the current attribute table?

自我的原始帖子以来取得了一些进展:

Some progress since my original post:

这是在弄清楚如何获取我的属性表 appart (.dbf),添加内容,现在我正在尝试将其重新组合起来以替换原始 dbf.

This is getting at figuring out how to take my attribute table appart (.dbf), add stuff, and now I'm trying to put it back together to replace the original dbf.

>libary(raster); library(rgdal); library(shapefiles)
>shp<-shapefile(Shape)  # D.C. area airport polygons
>summary(shp)           #Shapefile properties

Object of class SpatialPointsDataFrame
Coordinates:
                min       max
coords.x1  281314.2  337904.7
coords.x2 4288867.0 4313507.0
Is projected: TRUE 
proj4string :
[+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0]
Number of points: 4
Data attributes:
    ObjectID        LOCID               NAME              FIELD              STATE            STATE_FIPS           ACAIS              TOT_ENP            TYPE          
 Min.   :134.0   Length:4           Length:4           Length:4           Length:4           Length:4           Length:4           Min.   :    271   Length:4          
 1st Qu.:242.8   Class :character   Class :character   Class :character   Class :character   Class :character   Class :character   1st Qu.:   3876   Class :character  
 Median :339.0   Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character   Median :3409113   Mode  :character  
 Mean   :310.0                                                                                                                     Mean   :3717251                     
 3rd Qu.:406.2                                                                                                                     3rd Qu.:7122488                     
 Max.   :428.0

 >shp.AT<-read.dbf(gsub(".shp", ".dbf", Shape), header=TRUE)    #Read in the attribute table from the .dbf
 > shp.AT       # First object in the dbf looks like an attribute table!
$dbf
  ObjectID LOCID                            NAME               FIELD    STATE STATE_FIPS ACAIS TOT_ENP          TYPE
1      134   ADW                     Andrews AFB                <NA> Maryland         24     Y    5078      Military
2      279   DCA             Washington National                <NA> Virginia         51     Y 6813148          <NA>
3      399   HEF               Manassas Regional Harry P Davis Field Virginia         51     Y     271      Regional
4      428   IAD Washington Dulles International                <NA> Virginia         51     Y 8050506 International

$header
$header$file.version
[1] 3

$header$file.year
[1] 113

$header$file.month
[1] 4

$header$file.day
[1] 12

$header$num.records
[1] 4

$header$header.length
[1] 321

$header$record.length
[1] 148

$header$fields
        NAME TYPE LENGTH DECIMAL
1   ObjectID    N     10       0
2      LOCID    C      5       0
3       NAME    C     45       0
4      FIELD    C     30       0
5      STATE    C     24       0
6 STATE_FIPS    C      2       0
7      ACAIS    C      1       0
8    TOT_ENP    N     11       0
9       TYPE    C     20       0

>shp.tab<-as.data.frame(shp.AT[1]) # Grab the first object of the .dbf as a data.frame

> shp.tab       # First list object
  dbf.ObjectID dbf.LOCID                        dbf.NAME           dbf.FIELD dbf.STATE dbf.STATE_FIPS dbf.ACAIS dbf.TOT_ENP      dbf.TYPE
1          134       ADW                     Andrews AFB                <NA>  Maryland             24         Y        5078      Military
2          279       DCA             Washington National                <NA>  Virginia             51         Y     6813148          <NA>
3          399       HEF               Manassas Regional Harry P Davis Field  Virginia             51         Y         271      Regional
4          428       IAD Washington Dulles International                <NA>  Virginia             51         Y     8050506 International 

> shp.tab$NewAT<-1:nrow(shp.tab) # Add my new attribute

> shp.tab # Added my new attribute, now to get this back into my shapefile
  dbf.ObjectID dbf.LOCID                        dbf.NAME           dbf.FIELD dbf.STATE dbf.STATE_FIPS dbf.ACAIS dbf.TOT_ENP      dbf.TYPE NewAT
1          134       ADW                     Andrews AFB                <NA>  Maryland             24         Y        5078      Military     1
2          279       DCA             Washington National                <NA>  Virginia             51         Y     6813148          <NA>     2
3          399       HEF               Manassas Regional Harry P Davis Field  Virginia             51         Y         271      Regional     3
4          428       IAD Washington Dulles International                <NA>  Virginia             51         Y     8050506 International     4

>write.dbf(shp.tab, gsub(".shp", ".dbf", Shape)) # Knew this wouldn't work, but demonstrate attempt to write this object as .dbf.
ERROR:
invalid subscript type 'list'

> shp.AT[1]<-shp.tab # Try replacing the old Object[1] with my new table containing the new attribute.

> shp.AT # The table portion fo the shp.AT is gone. No attributes.
$dbf
[1] 134 279 399 428

$header
$header$file.version
[1] 3

$header$file.year
[1] 113

$header$file.month
[1] 4

$header$file.day
[1] 12

$header$num.records
[1] 4

>write.dbf(shp.AT, gsub(".shp", ".dbf", Shape)) # If I go ahead and overwrite anyway...

我的属性不见了,取而代之的是一个包含单个字段数据框"的属性表.我递归脚本再次读取属性表.

My attributes are gone and replaced with an attribute table that contains a single field "dataframe". I recurse the script to again read the attribute table.

> shp.tab
  dataframe
1       134
2       279
3       399
4       428

所以,我想我很接近了.谁能帮我把它放回 shapefile 属性表?或者,有没有更好的方法?

So, I think I'm close. Can anyone help me get this back into the shapefile attribute table? Or, is there a way better method?

谢谢

推荐答案

我不确定我是否完全理解你想要做什么.看起来你只是想在属性表中添加一个新列?如果这是正确的,那么就像对待任何数据框一样对待它.

I'm not sure I totally understand what you're trying to do. It looks like you just want to add a new column to the attribute table? If this is right, then just treat it like any dataframe.

library(rgdal)
dsn <- system.file("vectors", package = "rgdal")
shp<-readOGR(dsn = dsn, layer = 'cities')
shp$NewAT<-1:nrow(shp)

这与我系统上的 shapefile 完美配合.我通常依靠 rgdal 使用 readOGR() 函数读取我的 shapefile.我相当确定您调用的 shapefile() 函数也调用了 rgdal.

This works perfectly with a shapefile I have on my system. I typically rely on rgdal to read in my shapefiles, using the readOGR() function. I'm fairly certain the shapefile() function you were calling also calls rgdal.

编辑以添加可重现的数据集.

这篇关于使用 R 操作 shapefile 属性表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆