Manipulate a shapefile attribute table using R

I posted this question about sharing GIS stacks , but it does not see much traffic.

I am a GIS user who has been using R for statistics for several years, and I am very glad to see how many new GIS features (raster, shapefiles, rgdal, etc.) are released.

I did a lot of database and table operations in R, so the ability to add and remove attributes from shapefiles is a powerful potential.

I hope that I just skip what's there, but I cannot find a good method to add or remove attributes from the shapefile attribute table.

Can anyone from Overflow answer my GIS question? Or can I find out how to get the list attribute table in the data frame and vice versa to replace the current attribute table?

Change move

Some progress since my initial post:

This figures out how to take the table attribute table (.dbf), add stuff, and now I'm trying to combine it to replace the original dbf.

>libary(raster); library(rgdal); library(shapefiles) >shp<-shapefile(Shape) # DC 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 have disappeared and replaced with an attribute table that contains a single "dataframe" field. I am rewriting the script to read the attribute table again.

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

So, I think I'm close. Can someone help me return this to the shapefile attribute table? Or is there a better way?

thanks

+6
source share
1 answer

I'm not sure I fully understand what you are trying to do. Looks like you just want to add a new column to the attribute table? If this is correct, then just treat it like any data frame.

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

This works great with the shapefile that I have on my system. I usually rely on rgdal to read in my shapefiles using the readOGR () function. I'm pretty sure that the shapefile () function you called also calls rgdal.

Edited to add a playable dataset.

+4
source

Source: https://habr.com/ru/post/947898/


All Articles