更新时间:2023-12-01 11:32:28
停止使用 shapefiles
包.
安装 sp
和 rgdal
包.
使用以下命令读取 shapefile:
Read shapefile with:
chn = readOGR(".","CHN_adm1") # first arg is path, second is shapefile name w/o .shp
现在 chn
就像一个数据框.实际上 chn@data
是一个数据框.对该数据框执行您喜欢的操作,但保持相同的顺序,然后您可以通过以下方式使用新数据保存更新的 shapefile:
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")
注意你不应该直接操作 chn@data
数据框,你可以使用 chn
就像它在很多方面都是一个数据框,例如 chn$foo
获取名为 foo
的列,或者 chn$popden = chn$pop/chn$area
将创建一个新的人口密度列,如果你有人口和面积列.
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")
将映射您刚刚创建的 popden
列,并且:
will map by the popden
column you just created, and:
head(as.data.frame(chn))
应该显示 shapefile 数据的前几行.
should show you the first few lines of the shapefile data.