Skip to content

Instantly share code, notes, and snippets.

@josecarlosgonz
Last active May 17, 2021 10:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josecarlosgonz/8565908 to your computer and use it in GitHub Desktop.
Save josecarlosgonz/8565908 to your computer and use it in GitHub Desktop.
How to create a geojson file in R from a dataframe
#Write geojson
#====
#Load libraries
library(rgdal)
#dataMap is a dataframe with coordinates on cols 11 (LATITUDE) and 12 (LONGITUDE)
#Transfor coordinates to numeric
dataMap$LATITUDE <- as.numeric(dataMap$LATITUDE)
dataMap$LONGITUDE <- as.numeric(dataMap$LONGITUDE)
dataMap.SP <- SpatialPointsDataFrame(dataMap[,c(12,11)],dataMap[,-c(12,11)])
str(dataMap.SP) # Now is class SpatialPointsDataFrame
#Write as geojson
writeOGR(dataMap.SP, 'dataMap.geojson','dataMap', driver='GeoJSON')
@smakhtin
Copy link

Thanks for this great tip. I added check_exists to writeOGR in my fork, otherwise on my Ubuntu machine GDAL throw an "File not exist" error. https://gist.github.com/smakhtin/f06423ae23cb9d30ce75

@alfcrisci
Copy link

You are welcome!

@makis23
Copy link

makis23 commented Oct 10, 2017

What is the name of the geojson object you create? I try xx<-writeOGR(foo1_eqi.SP, 'foo1_eqi.geojson','foo1_eqi', driver='GeoJSON',check_exists = FALSE)
and take:

str(xx)
NULL

@fagnersutel
Copy link

This feature generate a unique polygon? There is a iterative mode to create 1, 2, 3 polygons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment