首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R中将csv转换为shp

如何在R中将csv转换为shp
EN

Stack Overflow用户
提问于 2015-05-08 23:17:54
回答 2查看 13.3K关注 0票数 5

在过去的几天里,我一直在尝试将csv转换为shapefile。我知道我可以很容易地在QGIS或Arc中这样做,但我想将这个过程添加到我现有的R代码中。

这样我就可以毫无问题地在csv中读取

代码语言:javascript
复制
MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")

我在包Shapefile帮助指南中找到了下面的代码。然而,我似乎找不到一种方法让它在我的代码上工作。我的每一行都是一个点,因此我试图创建的shapefile将是所有的点。我没有Id列,但是我在两个单独的列中有x和y数据。

代码语言:javascript
复制
dd <- data.frame(Id=c(1,2),X=c(3,5),Y=c(9,6))
ddTable <- data.frame(Id=c(1,2),Name=c("Item1","Item2"))
ddShapefile <- convert.to.shapefile(dd, ddTable, "Id", 1)
write.shapefile(ddShapefile, "c:/test", arcgis=T)

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-09 00:13:19

我建议使用rgdal而不是shapefiles。为了使用rgdal,您必须查看http://cran.revolutionanalytics.com/web/packages/rgdal/的系统要求。

下面的代码应该会让你找到正确的方向:

代码语言:javascript
复制
install.packages(c("rgdal", "sp"))
library(rgdal)
library(sp)
MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")

下面的代码片段来自Mapping in R using the ggplot2 package

代码语言:javascript
复制
class(MyData) # data.frame
coordinates(MyData)<-~longitude+latitude # whatever the equivalent is in your 
# data.frame
class(MyData) # [1] "SpatialPointsDataFrame"
          # attr(,"package")
          # [1] "sp"

下面的代码片段来自How to write a shapefile with projection - problem solved

代码语言:javascript
复制
writeOGR(crest.sp, "c:/test", "layer name", driver = "ESRI Shapefile")
票数 13
EN

Stack Overflow用户

发布于 2021-06-25 09:41:22

我认为这可以使用sf包将CSV文件转换为shapefile:

代码语言:javascript
复制
setwd('C:/Users/mark_/Documents/ctmm/density_in_R')

library(sf)

# Create lat-long data
set.seed(1234)

# 21.4389° N, 158.0001° W
longitude <- rnorm(1000, mean = 0, sd = 1) + -158.0
latitude  <- rnorm(1000, mean = 0, sd = 1) +   21.4
timestamp <- as.POSIXct("2021-06-24 10:30:05", format="%Y-%m-%d %H:%M:%S", tz="UTC")
my.data <- data.frame(longitude, latitude, timestamp)
head(my.data)
write.csv(my.data, "fake_lat_long_points_oahu.csv", row.names = FALSE, quote = FALSE)

# fake lat-long data imported from CSV file
oahu.lat.long <- read.csv("fake_lat_long_points_oahu.csv", header = TRUE, 
                           stringsAsFactors = FALSE, na.strings = "NA")

head(oahu.lat.long)

# Code in the following section based on:
# https://www.gisnote.com/2020/11/23/csv-to-shapefile-in-r/
# https://erinbecker.github.io/r-raster-vector-geospatial/10-vector-csv-to-shapefile-in-r/index.html

# Define coordinate reference system
prj4string <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
my.projection <- st_crs(prj4string)

# Create sf object
oahu_lat_long_sf <- st_as_sf(oahu.lat.long, coords = c("longitude", "latitude"), crs = my.projection)
st_crs(oahu_lat_long_sf)

plot(oahu_lat_long_sf)

# Export shapefile
st_write(oahu_lat_long_sf, "C:/Users/mark_/Documents/ctmm/density_in_R/fake_oahu_lat_long_sf_June24_2021/oahu_lat_long_sf.shp", driver="ESRI Shapefile")

以下是将shapefile导入QGIS并添加底图后的截图:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30127293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档