2013-04-28 146 views
1

我想將兩個.shp文件轉換成一個數據庫,這樣我就可以將地圖繪製在一起。如何將.shp文件轉換爲R的.csv文件?

此外,有沒有辦法將.shp文件轉換爲.csv文件?我希望能夠在.csv格式下個性化並添加一些對我來說更簡單的數據。我想到的是如果在地圖上添加疊加產量數據和降水量數據。

以下是MoroccoWestern 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() 

Morocco ​​

尋找到從@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() 

結果:

MoroccoMAp

新問題:

1 - 如何消除儘管削減一半的地圖邊界數據?

2-如何在.shp文件中結合不同的區域?

謝謝大家。

P.S:在stackoverflow.com的社區是美好的和非常有幫助的,特別是像初學者:)只是想強調它。

回答

8

將空間文件加載到Spatial {Lines/Polygons} DataFrames(classes從sp-package),您可以使用通用函數fortify將它們轉換爲平坦的data.frame格式。 ggplot2軟件包中包含fortify通用軟件的具體功能,因此您需要首先加載。一個代碼示例:

library(ggplot2) 
polygon_dataframe = fortify(polygon_spdf) 

其中polygon_spdfSpatialPolygonsDataFrame。類似的方法適用於SpatialLinesDataFrame

我的解決方案與@AriBFriedman的解決方案之間的區別在於,除了與這些polgons /行關聯的數據之外,我還包括多邊形/行的xy座標。我真的很喜歡用ggplot2軟件包來顯示我的空間數據。

將數據保存在正常的data.frame中後,只需使用write.csv即可在磁盤上生成csv文件。

5

我想你的意思是你想要每個關聯的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進行合併,而不是對其進行破壞性編輯。

+0

+1,儘管這不包括多邊形/線的空間座標。爲此,您可以在加載'ggplot2'包後使用'fortify',查看我的答案以獲取更多詳細信息。 – 2013-04-28 18:29:59

+2

@PaulHiemstra +1給你一個很好的'fortify'的使用方法。總會有一些水晶球凝視着這樣的問題,但是我讀到OP的意圖是僅僅添加和清除一些相關的值。爲此,出口越少越好 - 我仍然對將所有內容導出到Excel,編輯,然後重新讀入以及使用其中任何一種解決方案的非重現性感到不安。... – 2013-04-28 20:58:04

+2

我真的很推薦OP學習如何在R. – 2013-04-28 21:01:22