簡體   English   中英

如何從 R 中的現有 shapefile 轉換 crs 后保存 shapefile?

[英]How to save a shapefile after transform the crs from the existing shapefile in R?

我正在嘗試使用此代碼,但它不起作用。 請幫我解決這個問題。 謝謝你。

readshp <- readOGR(file.path(nhgisdir, “file1.shp”))`enter code here`
trshape <- spTransform(readshp, "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0”)
writeOGR(trshape, file.path(workdir,”file1_tr.shp"), driver="ESRI Shapefile”) # This one goes  wrong!!

確保指定所有必需的 arguments 函數,以使它們工作。 還要確保只使用一種引號來打開和關閉字符串。 您的問題使用兩種不同的類型" ,這可能會導致錯誤,因為 r 不知道您的命令在哪里開始和結束。

現在讀取 shapefile 的函數readOGR需要一些 arguments 指定:即dsn ,您的 shapefile 和layer的目錄,實際的 shapefile 但沒有文件擴展名。 function writeOGR的工作方式類似,但根據您使用的驅動程序具有不同的行為。 對於ESRI Shapefile ,參數dsn需要是您希望將 shapefile 保存為包含擴展名的文件名。 layer 將是不帶擴展名的文件名。

所有這些信息,以及需要為哪個驅動程序指定哪種類型的 arguments,都在rgdal 的文檔中 那里有很多教程,例如Emily Burchfield 博士的這個

最后,這里是一些示例代碼:

library(rgdal)
in_shape <- readOGR(dsn="/path/to/shape/", layer="shapefile") 
transformed_shape <- spTransform(in_shape, CRS("+proj=longlat +ellps=GRS80"))
writeOGR(transformed_shape, dsn="/path/to/shape/exported_shapefile.shp", layer="exported_shapefile", driver="ESRI Shapefile")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM