Skip to content

Instantly share code, notes, and snippets.

@josecarlosgonz
Created August 7, 2014 17:26
Show Gist options
  • Save josecarlosgonz/029c5ce586beae723cfb to your computer and use it in GitHub Desktop.
Save josecarlosgonz/029c5ce586beae723cfb to your computer and use it in GitHub Desktop.
Create a shapefile from a csv with latitude and longitude coordinates
# Create a shapefile from a csv with latitude and longitude coordinates in
# decimal degrees
# Install and load rdgal
install.packages(rdgal)
require(rgdal)
# Load data
data <- read.csv("Coordinates.csv")
head(data)
# Add id so the shapefile is not empty
data$id <- rep(1:length(data$Longitude))
head(data)
# Convert to spatial object longitude goes first
data.SP <- SpatialPointsDataFrame(data[,c(2,1)],data[,-c(2,1)])
str(dataMap.SP) # Now is class SpatialPointsDataFrame
# Write as shapefile inside the folder "coordinates"
writeOGR(data.SP, "coordinates", "coordinates", driver="ESRI Shapefile", layer_options= c(encoding= "UTF-8"),
overwrite_layer=T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment