AustralianAntarcticDivision / SOmap

Southern Ocean round maps
https://australianantarcticdivision.github.io/SOmap/
24 stars 6 forks source link

SOauto_crop not working for park fronts #90

Closed Maschette closed 4 years ago

Maschette commented 4 years ago

Because SOauto_crop() uses this code internally:

 suppressWarnings(try(as(sf::st_crop(
sf::st_buffer(sf::st_transform(sf::st_as_sf(layr), x$projection), 0), 
xmin = raster::xmin(extobj), xmax = raster::xmax(extobj), ymin = raster::ymin(extobj), ymax = raster::ymax(extobj)), 
"Spatial"), silent = TRUE))

It falls over for the park fronts which are already an sf object. Unless others have a better suggestion I think the solution is adding an if statement to check if the layer is an sf class and then use:

 suppressWarnings(sf::st_crop(layr, 
xmin = raster::xmin(extobj), xmax = raster::xmax(extobj), ymin = raster::ymin(extobj), ymax = raster::ymax(extobj)))
mdsumner commented 4 years ago

what's the error? I'd expect this work work since this is ok:

sf::st_as_sf(SOmap::SOmap_data$fronts_park)

probably the fronts should be sp though, rather than add logic - I was hoping to refactor this stuff at some point soon

Maschette commented 4 years ago
[1] "Error in (function (classes, fdef, mtable)  : \n  unable to find an inherited method for function ‘xmin’ for signature ‘\"NULL\"’\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in (function (classes, fdef, mtable) {    methods <- .findInheritedMethods(classes, fdef, mtable)    if (length(methods) == 1L)         return(methods[[1L]])    else if (length(methods) == 0L) {        cnames <- paste0("\"", vapply(classes, as.character,             ""), "\"", collapse = ", ")        stop(gettextf("unable to find an inherited method for function %s for signature %s",             sQuote(fdef@generic), sQuote(cnames)), domain = NA)    }    else stop("Internal error in finding inherited methods; didn't return a unique method",         domain = NA)})(list("NULL"), new("standardGeneric", .Data = function (x) standardGeneric("xmin"), generic = structure("xmin", package = "raster"),     package = "raster", group = list(), valueClass = character(0),     signature = "x", default = NULL, skeleton = (function (x)     stop("invalid call in method dispatch to 'xmin' (no default method)",         domain = NA))(x)), <environment>): unable to find an inherited method for function ‘xmin’ for signature ‘"NULL"’>

for the specific case that I had to use it I used: fronts<-sf::st_crop(SOproj(SOmap_data$ACC_FRONTS,source = projection(SOmap_data$ACC_FRONTS), target=map$projection), xmin = raster::xmin(map$target), xmax = raster::xmax(map$target), ymin = raster::ymin(map$target), ymax = raster::ymax(map$target))

which worked

Maschette commented 4 years ago

Actually I think it is two different errors. The first is the one above where map is a SOgg automap and SOauto_crop can't get the extent out (looks at the wrong spot). The second is that even when you use a normal automap you get this error:

[1] "Error in h(simpleError(msg, call)) : \n  error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': list item(s) not of class sfg\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': list item(s) not of class sfg>

Personally I think having an option so it can just take sf polygons would be nice, people seem to send them to me more and more.