1

Below is so far I've done to convert .csv file into shapefile

Mydata <- read.csv("londonburglary201402.csv", header = T, sep = ",")
class(Mydata)
coordinates(Mydata) <- ~Longitude + Latitude
class(Mydata)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

but I don't know how to write the shapefile with projection. I am trying to use rgdal packages to do.

2
  • Please edit into your question the output of class(Mydata), also mention the projection your data uses, and the library you are using to create a shape file. Make sure to take the tour which will provide some guidelines for Q&A in GIS SE
    – dof1985
    Aug 13 '16 at 14:04
  • I've edited now. Please let me know if the format now is okay. Thanks.
    – Wan Yu Lo
    Aug 13 '16 at 14:15
3

Easiest is probably with the raster package:

require(raster)
projection(Mydata) = "+init=espg:4326" # WGS84 coords
shapefile(Mydata, "Mydata.shp")

But with sp and rgdal:

proj4string(Mydata) = CRS("+init=epsg:4326")
writeOGR(Mydata, ".","Mydata", "ESRI Shapefile")

The "." is the current directory, and "Mydata" is the root name of the files that make up the shapefile.

8
  • How does your answer handle projection?
    – dof1985
    Aug 13 '16 at 14:05
  • Does now. check edit.
    – Spacedman
    Aug 13 '16 at 14:08
  • Thank you @ Spacedman. However it doesn't solve my prob. Below is the error appear Warning message: In .newCRS(value) : +init=espg:4326 is not a valid PROJ.4 CRS string
    – Wan Yu Lo
    Aug 13 '16 at 14:16
  • Why not? Why do you need to use rgdal? Okay, I'll add that solution too. But seriously, use the raster package it makes life easier.
    – Spacedman
    Aug 13 '16 at 14:17
  • Sorry. I am very new for R code. But I tried to use your code, it doesn't work. :(
    – Wan Yu Lo
    Aug 13 '16 at 14:19
0

I think this converts a CSV file to 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")

Screenshot after importing shapefile into QGIS and adding a background map:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.