Adding extra data column to shapefile using convert.to.shapefile in R's shapefiles package

前提是你 提交于 2019-12-11 18:06:29

问题


My goal is very simple, namely to add 1 column of statistical data to a shapefile so that I can use it for example to colour a geographical area. The data are a country file from gadm. To this end I usually use the foreign package in R thus:

library(foreign)

newdbf <- read.dbf("CHN_adm1.dbf") #original shape file

incrdata <- read.csv("CHN_test.csv") #.csv file with same region names column + new data column

mergedbf <- merge(newdbf,incrdata)

write.dbf(mergedbf,"CHN_New")

This achieves what I want in almost all circumstances, but one of the pieces of software I am dealing with external to R will only recognize .shp files and will not read .dbf (although clearly in a sense that statement is a slight contradiction). Not sure why it won't. Anyhow, essentially it leaves me needing to do the same thing as above, but with a shapefile. I think that according to notes on shapefiles package, the process should run something like this:

library(shapefiles)

shaper <- read.shp("CHN_adm1.shp")

simplified <- convert.to.simple(shaper)

simplified <- change.id(simplified,incrdata$DataNew) #DataNew being new column of data from the .csv

simpleAsList <- by(simplified,simplified[,1],function(x)x)

####This is where I hit problems####

backToShape <- convert.to.shapefile(simplified,
              data.frame(index=c("20","30","40","50","60","70","80")),"index",5)

write.shapefile(backToShape,"CHN_TestShape")

I'm afraid that I can't get my head around shapefiles, since I can't unpick them or visualize them in a way I can with dataframes, and so the resultant shape has been screwed up when it goes back to the external charting package.

To be clear: in 'backToShape' I just want to add the column of data and reconstruct the shapefile. It so happens that the data I have appears as a factor, ie 20,30,40 etc, but the data could just as easily be continuous, and I'm sure I don't need to type in all possibilities, but it was the only way I could seem to get it to be accepted. Can somebody please put me on the right track, and if I'm missing a simpler way, I'd also be extremely grateful to hear a suggestion. Many thanks in advance.


回答1:


Stop using the shapefiles package.

Install the sp and rgdal packages.

Read shapefile with:

chn = readOGR(".","CHN_adm1") # first arg is path, second is shapefile name w/o .shp

Now chn is like a data frame. In fact chn@data is a data frame. Do what you like to that data frame but keep it in the same order, and then you can save the updated shapefile with the new data by:

writeOGR(chn, ".", "CHN_new", driver="ESRI Shapefile")

Note you shouldn't really manipulate the chn@data data frame directly, you can work with chn like it is a data frame in many respects, for example chn$foo gets the column named foo, or chn$popden = chn$pop/chn$area would create a new column of population density if you have population and area columns.

spplot(chn, "popden")

will map by the popden column you just created, and:

head(as.data.frame(chn))

should show you the first few lines of the shapefile data.



来源:https://stackoverflow.com/questions/24548372/adding-extra-data-column-to-shapefile-using-convert-to-shapefile-in-rs-shapefil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!