saving shapefiles with R that can be re-read as a shapefile

Im using leaflet to make maps with layers including the very large Great Barrier Reef zoning shapefile (i.e., a large SpatialPolygonsDataFrame);
https://eatlas.org.au/data/uuid/92e4a530-6cbc-456b-918d-55d97c610e01

I worked out how to edit the shapefile by adding columns representing zoning colours to fill polygons (fillColor) and after a number of nights staring at the ceiling (from about 3am) I found a way to subset the data down to a region of interest (and reduce the file size and therefore, html loading time).

However, I have not found a way to save the new shapefile in a format that can be read by shapefile() then sptransform()? If I save this in .R format it is not then recognised as a shapefile, or am I missing something?

You could try converting to sf, then using st_write.

Something like...

library(sf)
my_sf <- st_as_sf(my_spdf)
st_write(dsn = 'whatever', layer = 'something', driver = 'ESRI Shapefile')

Note that I haven't tested that and it may be completely wrong. The st_drivers() routine will give a list of available drivers if I've got that wrong.

Hope this helps.

Ron.

Thanks Ron, before I try your suggestion I have a further question for the group. After consulting the ceiling oracle again last night, it appears that the data.frame that is set up in R environment (after running shapefile()) is composed of the polygon shapes (.shp) and the descriptions of the polygons (.dbf) i.e., two files are opened and combined when shapefile() is used (both files are in the same folder). This would mean that the process of saving in RStudio would need to separate the data.frame and save each part as their original types (meaning *.shp and *.dbf). I suppose this is the same process that is used by ArcGIS etc., to manipulate shapefiles. This might be one step too far for current packages, but would be helpful in this situation?

Thanks again for your input.

I'm not totally clear on what format you need to save your spatial data frame in, but the simplest thing to do if you just want to save it disk as a file that R can read back in is to use the native .rds format, which can store any R object.

Here is an example where I read in a .shp file, add a new column, save it to disk, and then read it back in. (As an aside, I'm using the sf package, which I highly recommend for working with spatial data in R instead of sp. But this same pattern would work just as well for an sp object too!)

library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3

# read in .shp file to sf object
nc <- read_sf(system.file("shape/nc.shp", package = "sf"))

# add column to sf object
nc_new <- nc %>% 
  mutate(new_data = runif(nrow(nc)))

# save new object as .rds file
write_rds(nc_new, path = file.path(tempdir(), "nc_new.rds"))

# read in saved .rds
nc_saved <- read_rds(file.path(tempdir(), "nc_new.rds"))

# they match!
identical(nc_new, nc_saved)
#> [1] TRUE

Created on 2019-06-08 by the reprex package (v0.3.0)

2 Likes

Ok, great! - many thanks mfherman, much appreciated

If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

Ok, I wasn't able to get your solution to work using read_sf or sp_read. Not sure what I needed to fix the issue of "Cannot open data source". However, I was able to save the shapefile set (auto saved a copy of one each of .shp, .prj, .shx and .dbf in the wd) using writeOGR - once I got the required syntax right. Although, the new file came up with a number of warnings for one vector, SHAPE_Area,

" Warning 1: Value 898180869.274000049 of field SHAPE_Area of feature 15 not successfully written. Possibly due to too larger number with respect to field width"

This meant that the identical() test was FALSE, but since I am not using SHAPE_Area is not an issue for my current results.

So, some more work needed, but at least I can save and re-open edited shapefiles now.

Thanks again to ron and mfherman for suggestions and encouragement, and to get a result!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.