Last active
September 14, 2016 19:16
-
-
Save etiennebr/929987dd53301a35dff8 to your computer and use it in GitHub Desktop.
Transfrom raster extent to a data.frame or a geometry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Transform extent coordinates to data.frame | |
#' @param e Raster or Extent object | |
#' @param loop Should first and last coordinates be the same ? (defautl TRUE, required to create a polygon) | |
extent_as_data_frame_poly = function (e, loop = TRUE) { | |
e = extent(e) | |
e_poly = data.frame(x = c(e@xmin, e@xmax, e@xmax, e@xmin), | |
y = c(e@ymin, e@ymin, e@ymax, e@ymax)) | |
if (loop) e_poly = rbind(e_poly, e_poly[1, ]) | |
e_poly | |
} | |
#' Transform extent to SpatialPolygon | |
#' @param r Raster or Extent object | |
#' @param p4s proj4string data, default is taken from r if it is of class Raster | |
extent_as_polygon = function(r, p4s = CRS(proj4string(r))) { | |
SpatialPolygons( | |
list(Polygons(list(Polygon(extent_as_data_frame_poly(r))), 1)), | |
proj4string = p4s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment