@@ -371,7 +371,16 @@ split_poly <- function(sf_poly, n_areas) {
371371 equal_areas $ area <- sf :: st_area(equal_areas )
372372 if (wasTerra )
373373 equal_areas <- terra :: vect(equal_areas )
374- return (equal_areas )
374+
375+ # Put them in xmin to xmax, ymin to ymax order
376+ mins <- Map(ind = seq(NROW(equal_areas )), function (ind ) {
377+ cbind(xmin = terra :: xmin(equal_areas [ind , ]), ymin = terra :: ymin(equal_areas [ind , ]))
378+ }) | > do.call(args = _, rbind )
379+ ord <- order(mins [, " xmin" ], mins [, " ymin" ])
380+ equal_areas <- equal_areas [ord , ]
381+ equal_areas [, " id" ] <- seq(NROW(equal_areas ))
382+
383+ return (equal_areas [ord , ])
375384}
376385
377386mergeAndSplitRas <- function (ecopRseg , ecopLCC , maxArea = 2.4e+11 ,
@@ -426,6 +435,7 @@ mergeAndSplitRas <- function(ecopRseg, ecopLCC, maxArea = 2.4e+11,
426435
427436moveSliversToOtherELFs <- function (lostPixels , lp , ca , i , r ) {
428437
438+ message(" moving slivers to neighbouring ELF" )
429439 if (NROW(unlist(lostPixels ))) {
430440 if (is.null(names(lostPixels ))) {
431441 hasNames <- FALSE
@@ -486,7 +496,6 @@ moveSliversToOtherELFs <- function(lostPixels, lp, ca, i, r) {
486496 a [lostPixels [[lp ]]$ pixelID ] <- newVals
487497 # a[a[] == 0] <- NA
488498 a <- terra :: trim(a )
489- # if (is(a, "try-error")) browser()
490499 a <- terra :: project(a , terra :: crs(r [[addTo ]]), method = " near" )
491500 bb <- terra :: resample(a , r [[addTo ]], method = " near" )
492501 whVals <- which(terra :: values(bb ) > 0 )
0 commit comments