10

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

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"
8

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

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

See ?writeSpatialShape for options.

  • 2
    This function does not retain the proj4string information. – Jeffrey Evans Oct 3 '16 at 23:34
  • @JeffreyEvans: so is there a way to pass that? perhaps reassign it in the GIS package it's going to. – val Nov 21 '18 at 17:15
  • @val just use writeOGR – MichaelChirico Nov 21 '18 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 Nov 21 '18 at 19:27
6

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.

0
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, privacy policy and cookie policy

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