如何将.shp文件转换为R的.csv文件?
我想将两个.shp文件转换成一个数据库,这样我就可以将地图绘制在一起。如何将.shp文件转换为R的.csv文件?
此外,有没有办法将.shp文件转换为.csv文件?我希望能够在.csv格式下个性化并添加一些对我来说更简单的数据。我想到的是如果在地图上添加叠加产量数据和降水量数据。
以下是Morocco和Western Sahara的形状文件。
代码的两个文件情节:
# This is code for mapping of CGE_Morocco results
# Loading administrative coordinates for Morocco maps
library(sp)
library(maptools)
library(mapdata)
# Loading shape files
Mor <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
Sah <- readShapeSpatial("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
# Ploting the maps (raw)
png("Morocco.png")
Morocco <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/MAR_adm1.shp")
plot(Morocco)
dev.off()
png("WesternSahara.png")
WesternSahara <- readShapePoly("F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/ESH_adm1.shp")
plot(WesternSahara)
dev.off()
寻找到从@AriBFriedman和@PaulHiemstra,随后建议搞清楚如何合并.SHP文件后,我已成功地使用以下代码和数据生成以下映射(对于.shp数据,参见上面的链接)
代码:
个# Merging Mor and Sah .shp files into one .shp file
MoroccoData <- rbind([email protected],[email protected]) # First, 'stack' the attribute list rows using rbind()
MoroccoPolys <- c([email protected],[email protected]) # Next, combine the two polygon lists into a single list using c()
summary(MoroccoData)
summary(MoroccoPolys)
offset <- length(MoroccoPolys) # Next, generate a new polygon ID for the new SpatialPolygonDataFrame object
browser()
for (i in 1: offset)
{
sNew = as.character(i)
MoroccoPolys[[i]]@ID = sNew
}
ID <- c(as.character(1:length(MoroccoPolys))) # Create an identical ID field and append it to the merged Data component
MoroccoDataWithID <- cbind(ID,MoroccoData)
MoroccoPolysSP <- SpatialPolygons(MoroccoPolys,proj4string=CRS(proj4string(Sah))) # Promote the merged list to a SpatialPolygons data object
Morocco <- SpatialPolygonsDataFrame(MoroccoPolysSP,data = MoroccoDataWithID,match.ID = FALSE) # Combine the merged Data and Polygon components into a new SpatialPolygonsDataFrame.
[email protected]$id <- rownames([email protected])
Morocco.fort <- fortify(Morocco, region='id')
Morocco.fort <- Morocco.fort[order(Morocco.fort$order), ]
MoroccoMap <- ggplot(data=Morocco.fort, aes(long, lat, group=group)) +
geom_polygon(colour='black',fill='white') +
theme_bw()
结果:
新问题:
1 - 如何消除尽管削减一半的地图边界数据?
2-如何在.shp文件中结合不同的区域?
谢谢大家。
P.S:在stackoverflow.com的社区是美好的和非常有帮助的,特别是像初学者:)只是想强调它。
将空间文件加载到Spatial {Lines/Polygons} DataFrames(classes从sp-package),您可以使用通用函数fortify
将它们转换为平坦的data.frame格式。 ggplot2
软件包中包含fortify
通用软件的具体功能,因此您需要首先加载。一个代码示例:
library(ggplot2)
polygon_dataframe = fortify(polygon_spdf)
其中polygon_spdf
为SpatialPolygonsDataFrame
。类似的方法适用于SpatialLinesDataFrame
。
我的解决方案与@AriBFriedman的解决方案之间的区别在于,除了与这些polgons /行关联的数据之外,我还包括多边形/行的x
和y
坐标。我真的很喜欢用ggplot2
软件包来显示我的空间数据。
将数据保存在正常的data.frame
中后,只需使用write.csv
即可在磁盘上生成csv文件。
我想你的意思是你想要每个关联的data.frame?
如果是这样,它可以通过@
插槽访问功能进行访问。该槽被称为data
:
write.csv([email protected], file="/home/wherever/myWesternSahara.csv")
然后,当你读这回与read.csv
,你可以尝试分配:
myEdits <- read.csv("/home/wherever/myWesternSahara_modified.csv")
[email protected] <- myEdits
你可能需要做行名称的一些按摩等方式来获得它接受新的data.frame为有效的。我可能会尝试将现有的data.frame与您在R中读取的csv进行合并,而不是对其进行破坏性编辑。
+1,尽管这不包括多边形/线的空间坐标。为此,您可以在加载'ggplot2'包后使用'fortify',查看我的答案以获取更多详细信息。 – 2013-04-28 18:29:59
@PaulHiemstra +1给你一个很好的'fortify'的使用方法。总会有一些水晶球凝视着这样的问题,但是我读到OP的意图是仅仅添加和清除一些相关的值。为此,出口越少越好 - 我仍然对将所有内容导出到Excel,编辑,然后重新读入以及使用其中任何一种解决方案的非重现性感到不安。... – 2013-04-28 20:58:04
我真的很推荐OP学习如何在R. – 2013-04-28 21:01:22