SCM

[#6577] Make crop work on sf dataframes

Date:
2018-04-11 14:56
Priority:
3
State:
Open
Submitted by:
Colin Caine (cmcaine)
Assigned to:
Nobody (None)
Product:
None
Operating System:
None
Component:
None
Summary:
Make crop work on sf dataframes

Detailed description
This works:

# Crop a Simple Features Data Frame to the extent of a raster
crop.sf = function(sfdf, rasta) {
st_intersection(sfdf, st_set_crs(st_as_sf(as(extent(rasta), "SpatialPolygons")), st_crs(sfdf)))
}

Though it throws away the crs information on the raster. I don't know how to convert a raster crs() into an st_crs().

Comments:

Message  ↓
Date: 2018-04-11 15:41
Sender: Colin Caine

This one keeps the crs of `rasta`

# Crop a Simple Features Data Frame to the extent of a raster
crop.sf3 <- function(sfdf, rasta) {
# Get extent and crs
ext.sp <- as(extent(rasta), "SpatialPolygons")
crs(ext.sp) <- crs(rasta)

# crop
st_intersection(sfdf, st_as_sf(ext.sp))
}

Attached Files:

Changes

No Changes Have Been Made to This Item

Thanks to:
Vienna University of Economics and Business Powered By FusionForge