2014-03-12 27 views
0

我是R地圖的新手,我想以圖形方式將植物物種分佈顯示爲地圖上的多邊形。我有一個UTM座標的csv文件。我正在使用R中的maps和maptools程序,並且我已經能夠讀取現有的ESRI shapefile文件。我所看到的討論帖似乎都處理現有形狀文件中的閱讀內容,並對其進行一些編輯並將其「重寫」。是否有一種簡單的方法將我的csv文件轉換爲用於映射的shapefile?感謝您的幫助,埃裏克如何從R中現有的GPS座標寫入新的形狀文件?

+0

你更容易得到幫助,如果你提供至少你的數據的代表性樣本,以表明你已經嘗試過,並說明爲什麼/怎麼沒有奏效。 – jlhoward

+0

另外,「地圖上的多邊形」是什麼意思?如果你有UTM座標,你可以將它們繪製成點。 – jlhoward

回答

0

您應該有關於您的座標參考系統的更多信息。我不確定你的'多邊形'是什麼意思:我認爲.csv只能包含座標點。

下面是一個工作流的例子:創建一個簡單的data.frame;設置座標;寫shapefile;讀回形狀文件;並繪製簡單的地圖。希望能幫助到你。而不是創建一個data.frame,你可以導入你的.csv。

# Load libraries 
library(sp) 
library(rgdal) 
# set CRS . For epsg code, see http://spatialreference.org/ 
projTest<-CRS("+init=epsg:28992") 
# create a temp dir for shapefiles 
trashMap<-tempdir() 
# create dumb data: 
occData<-data.frame(x=c(12,13,14,20),y=c(0,1,4,9),obs=c("a","b","c","d")) 
# set x and y as coordinates 
coordinates(occData)<-c('x','y') 
# assign CRS 
proj4string(occData)<-projTest 
# write simple shapefile in trashMap dir 
writeOGR(occData, trashMap, layer='testLayer', driver="ESRI Shapefile") 
# get list of Layers 
layers<-ogrListLayers(trashMap) 
# read shapefile and selected layer 
occData2<-readOGR(trashMap, layers) 
# Modify data 
occData2$newColumn<-c('no','stress','','dude') 
occData2 
# verify that is projected 
is.projected(occData2) 
# display projection type 
proj4string(occData2) 
# print maps before and after export in shapefile. 
print(spplot(occData, auto.key=F, col.regions='black', scales=list(draw=T), pch=2, cex=1)) 
print(spplot(occData2, auto.key=F, col.regions='black', scales=list(draw=T), pch=2, cex=1))