15

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".
3
  • 1
    rgdal::writeOGR
    – hrbrmstr
    Commented Sep 7, 2015 at 1:28
  • What does Forced mean here?
    – mdsumner
    Commented Sep 7, 2015 at 5:07
  • I am not sure what "Forced" means but I found it in "GISTools" documentation under the tornado data type.
    – Mokhless
    Commented Sep 7, 2015 at 5:23

4 Answers 4

24

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"
0
9

Note there is also a function in the maptools package to accomplish this, namely writeSpatialShape:

library(maptools)
writeSpatialShape(torn, "torn").

See ?writeSpatialShape for options.

4
  • 2
    This function does not retain the proj4string information. Commented Oct 3, 2016 at 23:34
  • @JeffreyEvans: so is there a way to pass that? perhaps reassign it in the GIS package it's going to.
    – val
    Commented Nov 21, 2018 at 17:15
  • @val just use writeOGR Commented Nov 21, 2018 at 18:26
  • @MichaelChirico: thx but thats where I began and the issue is that it truncates to an Error... duplicate fields. There are posts about it.
    – val
    Commented Nov 21, 2018 at 19:27
8

You should look at the rgdal package, specifically the writeOGR function.

For example, I first downloaded the 110m-resolution "States and Provinces" shapefiles from Natural Earth, unpacking them to ~/scratch/ne_110m_admin_1_states_provinces. I read them into R via:

library(rgdal)
setwd("~/scratch")
states <- readOGR(dsn="ne_110m_admin_1_states_provinces",
    layer="ne_110m_admin_1_states_provinces")

At this point, the object states is a SpatialPolygonsDataFrame. Now I create a new directory ("tempdir") and save the shapefiles there:

dir.create("tempdir")
writeOGR(obj=states, dsn="tempdir", layer="states", driver="ESRI Shapefile")

The shapes are now saved to the directory ~/scratch/tempdir, as files states.shp, etc. You can do the same thing with a SpatialPointsDataFrame, just see the first example in the documentation of the writeOGR function.

6
library(raster)
shapefile(tornados, filename='path/to/file.shp')

Make sure you have (or have given yourself) permissions to write to the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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