【问题标题】:How to convert .shp file into .csv in R?如何在 R 中将 .shp 文件转换为 .csv?
【发布时间】:2013-04-28 17:56:07
【问题描述】:

我想将两个 .shp 文件转换成一个数据库,这样我就可以一起绘制地图了。

另外,有没有办法将 .shp 文件转换为 .csv 文件?我希望能够在 .csv 格式下个性化和添加一些对我来说更容易的数据。如果在地图上添加叠加产量数据和降水数据,我有什么想法。

这里是 MoroccoWestern Sahara 的 shapefile。

绘制两个文件的代码:

# 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(Mor@data,Sah@data) # First, 'stack' the attribute list rows using rbind() 
MoroccoPolys <- c(Mor@polygons,Sah@polygons) # 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.

Morocco@data$id <- rownames(Morocco@data)
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 中的社区非常棒,非常有帮助,尤其是对像 :) 这样的初学者来说,只是想强调一下。

【问题讨论】:

    标签: r maps shape spatial


    【解决方案1】:

    将 shapefile 加载到 Spatial{Lines/Polygons}DataFrame(来自 sp-package 的类)后,您可以使用 fortify 通用函数将它们转换为平面 data.frame 格式。 fortify 泛型的特定函数包含在 ggplot2 包中,因此您需要先加载它。代码示例:

    library(ggplot2)
    polygon_dataframe = fortify(polygon_spdf)
    

    其中polygon_spdfSpatialPolygonsDataFrame。类似的方法适用于SpatialLinesDataFrame's。

    我的解决方案与@AriBFriedman 的解决方案之间的区别在于,我的解决方案包括多边形/线的xy 坐标,以及与这些多边形/线关联的数据。我真的很喜欢使用 ggplot2 包来可视化我的空间数据。

    将数据保存在普通的 data.frame 中后,您可以简单地使用 write.csv 在磁盘上生成 csv 文件。

    【讨论】:

      【解决方案2】:

      我想你的意思是你想要每个相关的 data.frame?

      如果是这样,可以使用@插槽访问功能进行访问。该插槽称为data

      write.csv( WesternSahara@data, file="/home/wherever/myWesternSahara.csv")
      

      然后当你用read.csv读回它时,你可以尝试分配:

      myEdits <- read.csv("/home/wherever/myWesternSahara_modified.csv")
      WesternSahara@data <- myEdits
      

      您可能需要对行名等进行一些按摩,以使其接受新的 data.frame 为有效。我可能会尝试将现有的 data.frame 与您在 R 中读取的 csv 合并,而不是破坏性地进行编辑....

      【讨论】:

      • +1,虽然这不包括多边形/线的空间坐标。为此,您可以在加载ggplot2 包后使用fortify,有关详细信息,请参阅我的答案。
      • @PaulHiemstra +1 给你一个很好的使用fortify。像这样的问题总是有一些水晶球凝视,但我对 OP 的阅读意图是添加和清理一些相关的值。为此,导出的越少越好——我仍然担心使用任何一种解决方案将所有内容导出到 Excel、编辑然后读回的不可重现性......
      • 我真的建议 OP 学习如何在 R 中进行编辑。
      猜你喜欢
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 2019-02-22
      • 1970-01-01
      • 2021-11-25
      • 2021-07-05
      相关资源
      最近更新 更多