如何在循环中加载多个点的多个点文件并将其转换为CSV文件?

发布于 2025-02-11 08:42:31 字数 790 浏览 4 评论 0原文

我想阅读并将多个Shapefile转换为CSV。该链接包含Shapefile的样本。以下是我正在尝试的代码。

  library(raster)
  setwd("D:/share_1/PAPER_regimes/22_06_22/hp/firepoint_12_veg")
  all.files <- list.files(pattern="\\.shp$")
  out.files <- gsub("\\.shp$", ".csv")
  crs <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
  for(i in 1:length(all.files)) {
   d <- readOGR(all.files[i], stringsAsFactors = FALSE)
   sp <- SpatialPointsDataFrame(coords = d[c("Longitude", "Latitude")], d, proj4string = crs) 
   utm <- spTransform(sp, CRS("+proj=utm +zone=16 +datum=WGS84"))
   shapefile(utm, out.files[i])
    }

I want to read and convert multiple shapefile into csv. The link contain sample of shapefile. Following is the code I was trying.

  library(raster)
  setwd("D:/share_1/PAPER_regimes/22_06_22/hp/firepoint_12_veg")
  all.files <- list.files(pattern="\\.shp
quot;)
  out.files <- gsub("\\.shp
quot;, ".csv")
  crs <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
  for(i in 1:length(all.files)) {
   d <- readOGR(all.files[i], stringsAsFactors = FALSE)
   sp <- SpatialPointsDataFrame(coords = d[c("Longitude", "Latitude")], d, proj4string = crs) 
   utm <- spTransform(sp, CRS("+proj=utm +zone=16 +datum=WGS84"))
   shapefile(utm, out.files[i])
    }

link contain few shapefile and dataset which are in point

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2025-02-18 08:42:31

您可以这样做,

library(terra)
all.files <- list.files(pattern="\\.shp$", full=TRUE)
out.files <- gsub("\\.shp$", ".csv", all.files)

for(i in 1:length(all.files)) {
   d <- vect(all.files[i])
   utm <- project(d, "+proj=utm +zone=16 +datum=WGS84")
   xy <- crds(utm)
   write.csv(xy, out.files[i], row.names=FALSE)
}

我不知道为什么您去了属性中的LON/LAT,但是如果您需要的话,您可以做:

for(i in 1:length(all.files)) {
   d <- vect(all.files[i])
   geo <- vect(data.frame(d), c("LONGITUDE", "LATITUDE"), crs="+proj=longlat")
   utm <- project(d, "+proj=utm +zone=16 +datum=WGS84")
   xy <- crds(utm)
   write.csv(xy, out.files[i], row.names=FALSE)
}

You can do that like this

library(terra)
all.files <- list.files(pattern="\\.shp
quot;, full=TRUE)
out.files <- gsub("\\.shp
quot;, ".csv", all.files)

for(i in 1:length(all.files)) {
   d <- vect(all.files[i])
   utm <- project(d, "+proj=utm +zone=16 +datum=WGS84")
   xy <- crds(utm)
   write.csv(xy, out.files[i], row.names=FALSE)
}

I do not know why you went to the lon/lat in the attributes, but if that is what you need you can do:

for(i in 1:length(all.files)) {
   d <- vect(all.files[i])
   geo <- vect(data.frame(d), c("LONGITUDE", "LATITUDE"), crs="+proj=longlat")
   utm <- project(d, "+proj=utm +zone=16 +datum=WGS84")
   xy <- crds(utm)
   write.csv(xy, out.files[i], row.names=FALSE)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文