How to create a geojson in R
[This article was first published on Jose Gonzalez » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This is a short but sweet post on how to create a geojson file in R . Remember that you can render geojson files in github.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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') |
The post How to create a geojson in R appeared first on Jose Gonzalez.
To leave a comment for the author, please follow the link and comment on their blog: Jose Gonzalez » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.