CSV to Shapefile in R


 

#####Code based on https://erinbecker.github.io/r-raster-vector-geospatial/10-vector-csv-to-shapefile-in-r/index.html

#####load libraries
library("sf")
library("raster")

#####Set working path, input text file name, output shapefile name
mypath<- "~/r-docker-tutorial/lIlk"
myfile<- "20201015_1130wetland8.TXT"
outfile<- "emi_20201015_1130wetland8.shp"

#####Read CSV into a data frame named mydat
mydat<- read.csv(file.path(mypath,myfile), na.strings = "NA")
nrow(mydat)

#####Remove all rows whose longitude value is NA
mydat<- subset(mydat, !is.na(mydat$WGS84_LON))
nrow(mydat1)

#####Define the WGS84 coordinate string (these data are in WGS84)
prj4string<- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
prj<- st_crs(prj4string)

#####Create the spatial data frame
plot_locations<- st_as_sf(mydat1, coords = c("WGS84_LON","WGS84_LAT"), crs=prj)
st_crs(plot_locations)

#####Plot the points
plot(plot_locations, main="Map of EMI Data")

#####Export the shapefile
st_write(plot_locations,file.path(mypath,outfile), driver="ESRI Shapefile")

#####
#####This might be an interesting followup
#####https://rspatial.org/raster/analysis/4-interpolation.html
#####

 

Print Friendly, PDF & Email