Reputation: 497
I have been trying for the past couple of days to convert a csv to shapefile. I know I can easily do in QGIS or Arc but would like to add this process into my existing R code.
So i can read in the csv with no issues
MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")
I found the code below from the Packages Shapefile help guide. However I can't seem to find a way for it to work on my code. My rows are each a point, therefore my shapefile I am trying to create will be all points. I don't have an Id column, however I do have x and y data in two separate columns.
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)
Any help would be greatly appreciated.
Upvotes: 5
Views: 17015
Reputation: 13103
I think this converts a CSV
file into a shapefile using the sf
package:
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")
Here is a screenshot after importing the shapefile
into QGIS
and adding a basemap:
Upvotes: 2
Reputation: 944
I would suggest using rgdal
rather than shapefiles
. In order to use rgdal
, you'll have to check out the system requirements from http://cran.revolutionanalytics.com/web/packages/rgdal/.
The following code should get you in the right direction:
install.packages(c("rgdal", "sp"))
library(rgdal)
library(sp)
MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")
The following code snippet comes from Mapping in R using the ggplot2 package.
class(MyData) # data.frame
coordinates(MyData)<-~longitude+latitude # whatever the equivalent is in your
# data.frame
class(MyData) # [1] "SpatialPointsDataFrame"
# attr(,"package")
# [1] "sp"
The following code snippet comes How to write a shapefile with projection - problem solved
writeOGR(crest.sp, "c:/test", "layer name", driver = "ESRI Shapefile")
Upvotes: 13