0

I have several scripts written in R language with package "sp" for Qgis. However I am stuck in the old versions (2.18) because whenever I use this same package in current versions 3.14 the script has an error. What alternatives could i do to work around this problem? I didn't write the scripts, I only use them. I've been researching and the "sp" package has been replaced by "sf" but I don't have the cognitive ability to rewrite all the scripts.

This is 1 of the 12 scripts I use in version 2.18. When I try to run in version 3.14 I get this error.

##Determina_zonas_de_manejo=group
##Raster_de_Entrada=raster
##Poligono_mascara=vector
##Quantidade_de_Celulas_para_Remover_das_Bordas=number 0
##Raster_Corte_por_Mascara=output raster

library(raster)
library(sp)
require(rgdal)
library(rgeos)

raster_in  <- Raster_de_Entrada
pol_mask <- Poligono_mascara
rm_bord <- Quantidade_de_Celulas_para_Remover_das_Bordas


#raster_in <- brick('C:/Dados/ManejoPrecisao/UGD_geodata/ndvi.tif')[[1]]
#pol_file <- 'C:/Dados/ManejoPrecisao/UGD_geodata/talhao01.shp'
#pol_mask <- readOGR(dirname(pol_file), strsplit(basename(pol_file), '[.]')[[1]][1] )
#rm_bord = 1

cel_tam <- res(raster_in)[1]
rm_bord_tam <- rm_bord * cel_tam

rm_bord_buf <- gBuffer(pol_mask, width= -rm_bord_tam)

if(rm_bord == 0){
  crop_in <- crop(raster_in, extent(pol_mask), snap = 'out')
  raster_out <- mask(crop_in, pol_mask)
  Raster_Corte_por_Mascara = raster_out
} else{
  crop_in <- crop(raster_in, extent(rm_bord_buf), snap = 'out')
  raster_out <- mask(crop_in, rm_bord_buf)
  Raster_Corte_por_Mascara = raster_out
}

enter image description here

3
  • The sp package has not been replaced by sf so, that is not the problem. Unfortunately you do not provide enough information for us to troubleshoot anything. We need access to the scripts, version information on QGIS, R and the required packages. Also, knowing what errors are being produced would be helpful as well. Apr 20 at 2:13
  • Sorry for the mistake, I updated the question with the information. Thanks. @JeffreyEvans
    – glenda
    Apr 20 at 2:59
  • To go from sf to sp use as(x, "Spatial") and sp to sf as(x, "sf") but, be aware of multipart geometries. You can nest as coercion directly into a function without creating a new object first eg., gBuffer(as(x, "Spatial"), width=1000) Apr 20 at 19:57
0

The error appears to be coming from this line:

rm_bord_buf <- gBuffer(pol_mask, width= -rm_bord_tam)

and is basically saying it doesn't know what to do with pol_mask because it is an sf object (that's the "unable to find an inherited method..." message).

gBuffer comes from the rgeos package and that expects sp objects. I suspect that QGIS is providing vector layers as sf objects and not sp objects, and that breaks the script.

To fix, attach the sf package and convert vector layers from sp to sf:

library(raster)
library(sp)
require(rgdal)
library(rgeos)
library(sf) # QGIS is giving us `sf` objects

raster_in  <- Raster_de_Entrada
pol_mask <- as(Poligono_mascara,"Spatial") # convert to `sp` from `sf`
rm_bord <- Quantidade_de_Celulas_para_Remover_das_Bordas

That might fix the scripts, or it might only get it as far as the next error message. Try it and see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.