1

I am trying to create a convex hull for a set of spatial points and then later crop a raster by the extent of this polygon.

coords<-as.data.frame(cbind(dnlocs_1949$decimalLongitude,dnlocs_1949$decimalLatitude))

dn_1949 <- SpatialPointsDataFrame(coords,
              dnlocs_1949,
              coords.nrs = numeric(0),
              proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

pts_1949<-as(dn_1949,"sf")

hull_1949<- st_convex_hull(st_union(pts_1949))

##this created a visual convex hull but does not have the information to save as a polygon
##it saves hull_1949 has a list 

#then when i try crop my raster

temp_1940<-crop(x=temp_1940, y=hull_1949)

#i get the error message 
#Error in (function (classes, fdef, mtable) :
#unable to find an inherited method for function ‘extract’ for signature ‘"RasterLayer", "sfc_POLYGON"’
  • 2
    You should use fake data, generated in the code you post. We don't have access to your `dnlocs_1949` variable. See https://stackoverflow.com/q/5963269/2554330 . – user2554330 May 07 '23 at 12:11
  • 1
    Please list **all** libraries you have loaded so we know exactly which functions are being called. – Carl Witthoft May 07 '23 at 16:37

1 Answers1

0

This is a guess since I don't know the details of the data, but I think you need to convert the convex hull sfc_polygon object back to the spatial class eg. using use sf::as_Spatial() :

temp_1940<-crop(x=temp_1940, y=sf::as_Spatial(hull_1949))

from ?crop:

y
Extent object, or any object from which an Extent object can be extracted (see Details) (...) include RasterLayer, RasterStack, RasterBrick and objects of the Spatial classes from the sp package. You can check this with the extent function

Lars
  • 16
  • 2