Monday, 8 July 2019

r - How to export a dataset with "SpatialPolygonsDataFrame" as a shapefile


How to export spatial datasets in R, such as tornados dataset in GISTools package, as a shapefile with the exact same spatial reference and the exact values?


library(GISTools)

data(tornados)


Desired:



  • "SpatialPolygonsDataFrame" to "shapefile"

  • "(Forced) SpatialPointsDataFrame" to "shapefile"

  • "(Non-Forced) SpatialPointsDataFrame" to "shapefile"

  • "SpatialLinesDataFrame" to "shapefile".



Answer



Wesley's answer is correct. So to specifically export the tornado data you need to do:



library(GISTools)
library(rgdal)
data(tornados)

writeOGR(obj=torn, dsn="tempdir", layer="torn", driver="ESRI Shapefile") # this is in geographical projection

writeOGR(obj=torn2, dsn="tempdir", layer="torn2", driver="ESRI Shapefile") # this is in equal area projection

For R both of these datasets are simply treated as SpatialPointsDataFrames.


> class(torn)

[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

> class(torn2)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...