In this short blog post, I will show three ways to convert a raster (gridded) data into polygons. In the course of my research, I faced the need of this type of conversion on multiple occasions. As far as I am aware, there are two options to achieve this operation with (significantly) differing computation time.
library(raster)
library(rgdal)
library(sp)
library(sf)
We use PRISM () data to demonstrate how we can get raster-to-polygons conversions done. Let’s download maximum temperature data for 4th of July, 2017.
#--- load the prism package ---#
library(prism)
#--- set the path ---#
options(prism.path = '~/Box Sync/Website/websites/data')
#--- download the data ---#
get_prism_dailys(type='tmax',minDate='2017-07-04',maxDate='2017-07-04',keepZip=F)
After you confirm the data was successfully downloaded, it’s time to import the data. Now, the dataset covers the entire U.S., which is a little too big to work on. So, let’s just import the part that covers Nebraska using the offset and region.dim options.
#--- import the data that covers Nebraska ---#
max_temp <- readGDAL('~/Box Sync/Website/data/PRISM_tmax_stable_4kmD1_20170704_bil/PRISM_tmax_stable_4kmD1_20170704_bil.bil',offset=c(188,497),region.dim=c(55,183))
#--- summarize the data ---#
summary(max_temp)
## Object of class SpatialGridDataFrame
## Coordinates:
## min max
## x -104.3125 -96.68750
## y 39.8125 42.10417
## Is projected: FALSE
## proj4string :
## [+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0]
## Grid attributes:
## cellcentre.offset cellsize cells.dim
## x -104.29167 0.04166667 183
## y 39.83333 0.04166667 55
## Data attributes:
## band1
## Min. :28.11
## 1st Qu.:30.29
## Median :31.22
## Mean :31.31
## 3rd Qu.:32.38
## Max. :34.96
#--- visualize the data ---#
plot(max_temp)
If you search, say, “grid to polygon in r”, the first function you will see would be the Grid2Polygons() function, which is in the Grid2Polygons package. If you library the package and try to use it, you will get a warning message that says the function is deprecated, and you should use the Grid2Polygons() function in the inlmisc package. So, let’s do that.
library(inlmisc)
spdf_1 <- Grid2Polygons(max_temp)
The above process should have taken you some time to run. Now, we have a much faster way from the sp package. Here is how you do it:
spdf_2 <- as(max_temp,'SpatialPolygonsDataFrame')
This should have taken you much less time. Basically, just use the latter, not the Grid2Polygons() function. There is basically no point in using the Grid2Polygons() function. I had one of my students using the Grid2Polygons() function, instead of the other option. I believe many who seek to convert a grid data to polygon data can fall in the same trap.
Once we have a SpatialPolygonsDataFrame object, we can simply do the following to convert it into an sf object.
#--- convert to an sf object ---#
sf_data <- st_as_sf(spdf_2)
#--- take a look ---#
sf_data
## Simple feature collection with 10065 features and 1 field
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -104.3125 ymin: 39.8125 xmax: -96.6875 ymax: 42.10417
## epsg (SRID): 4269
## proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
## First 10 features:
## band1 geometry
## g1 33.946 POLYGON ((-104.3125 42.0625...
## g2 34.078 POLYGON ((-104.2708 42.0625...
## g3 34.095 POLYGON ((-104.2292 42.0625...
## g4 34.143 POLYGON ((-104.1875 42.0625...
## g5 34.146 POLYGON ((-104.1458 42.0625...
## g6 34.108 POLYGON ((-104.1042 42.0625...
## g7 34.146 POLYGON ((-104.0625 42.0625...
## g8 33.974 POLYGON ((-104.0208 42.0625...
## g9 33.846 POLYGON ((-103.9792 42.0625...
## g10 33.718 POLYGON ((-103.9375 42.0625...
Session Information
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS 10.14.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] prism_0.0.7 sf_0.7-1 rgdal_1.3-6 raster_2.8-4 sp_1.3-1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.0 bindr_0.1.1 compiler_3.5.1 pillar_1.3.0
## [5] plyr_1.8.4 class_7.3-14 tools_3.5.1 digest_0.6.18
## [9] evaluate_0.12 tibble_1.4.2 gtable_0.2.0 lattice_0.20-35
## [13] pkgconfig_2.0.2 rlang_0.3.0.1 DBI_1.0.0 curl_3.2
## [17] yaml_2.2.0 blogdown_0.9 xfun_0.4 bindrcpp_0.2.2
## [21] spData_0.2.9.4 e1071_1.7-0 httr_1.3.1 dplyr_0.7.8
## [25] stringr_1.3.1 knitr_1.20 tidyselect_0.2.5 classInt_0.2-3
## [29] rprojroot_1.3-2 grid_3.5.1 glue_1.3.0 R6_2.3.0
## [33] rmarkdown_1.10 bookdown_0.7 purrr_0.2.5 ggplot2_3.1.0
## [37] magrittr_1.5 backports_1.1.2 scales_1.0.0 codetools_0.2-15
## [41] htmltools_0.3.6 units_0.6-1 assertthat_0.2.0 colorspace_1.3-2
## [45] stringi_1.2.4 lazyeval_0.2.1 munsell_0.5.0 crayon_1.3.4