1

I just started using RQGIS. I ran the example code using qgis:polygoncentroids shown on https://github.com/jannes-m/RQGIS with no problem.

I tried then to create a vector grid using qgis:vectorgrid but I get an error, which seems to indicate that the command expects a raster format output, when the output in QGIS for this plugin is a shapefile.

If I run run_qgis without the load_output argument I don't get an error, but no file is created on the disk either (I am on Ubuntu 16.04).

My code is as follows:

library(RQGIS)
library(rgdal)
library(rgeos)
library(raster)
##PARAMETERS
#working directory
work.dir <- "/media/olivier/olivier_ext/gedata_current/jde_coffee/data/ES"
#Location and name of the grid to output
out <- c("Grids","ES_grid_test")
#Resolution of the grid in km
res <- 2
#Location and name of shapefile with the extent and outline of the grid
shp.ext <- c("Boundaries","Espirito_Santo_geo")
#Projection to use
project <- CRS("+init=epsg:32723")
#Set working directory
setwd(work.dir)
#Import boundary and extent
bound <- readOGR(dsn=shp.ext[1], layer=shp.ext[2], stringsAsFactors=F)
bound <- spTransform(bound, project)
#Set a QGIS environment
env <- set_env()
#retrieve the function arguments
args <- get_args_man(alg = "qgis:vectorgrid", options = TRUE, qgis_env = env)
#Fill arguments
args$EXTENT <- as.vector(extent(bound))
args$STEP_X <- res*1000 #In meters if UTM
args$STEP_Y <- res*1000
args$TYPE <- 0
args$OUTPUT <- paste0(work.dir,"/",out[1],"/",out[2],".shp")
grid.shp <- run_qgis(alg = "qgis:vectorgrid", params = args, load_output = args$OUTPUT, qgis_env = env)

I get the following error:

Error in .local(.Object, ...) :
`/media/olivier/olivier_ext/gedata_current/jde_coffee/data/ES/Grids/ES_grid_test.shp' does not exist in the file system, and is not recognised as a supported dataset name.

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. (file does not exist)

It makes me believe that run_qgis expects a raster format... But in QGIS it's definitely a shapefile output.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 26, 2016 at 16:04

1 Answer 1

2
import os, sys
from qgis.core import *
from osgeo import ogr
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.gui import *
QgsApplication.setPrefixPath('C:/OSGeo4W64/apps/qgis-ltr', True)
app = QgsApplication([], True)
QgsApplication.initQgis()
sys.path.append(r'C:/OSGeo4W64/apps/qgis-ltr/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing
processing.runalg("qgis:vectorgrid", "0, 1, 0, 1", "0.5", "0.5", "0", None)

Running the code above on the command line produces following message:

'NoneType' object has no attribute 'mapCanvas'
 See log for more details
{'OUTPUT': u'C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing06a684c24f1141349bfaca47bf57b853\095円a1096a941493c9fc04826c0d5c115\\OUTPUT.shp'}

Unfortunately, the specified output folder is empty, i.e. QGIS did not produce the vector grid. I am not sure what is going on here, and I think we should ask the QGIS community for further advice.

The R error output is misleading, I am sorry. The load_output argument first tries to load a shapefile into R, and if that is not working, it secondly tries to load a raster file. However, if no output has been created (as in our case), the function fails with the error message you mentioned. I have already fixed this by checking if the file to be loaded actually exists (https://github.com/jannes-m/RQGIS). If not, the function will now say so and stop. Until we get further feedback from the QGIS community, you can try to use the create grid function:

library("RQGIS")
qgis_env <- set_env()
args <- get_args_man("qgis:creategrid", options = TRUE, qgis_env = qgis_env)
args$TYPE <- 1
args$EXTENT <- "0, 1, 0, 1"
args$HSPACING <- "0.5"
args$VSPACING <- "0.5"
args$CRS <- "EPSG:4326"
args$OUTPUT <- "grid.shp"
run_qgis("qgis:creategrid", params = args, load_output = args$OUTPUT, qgis_env = qgis_env)
answered Aug 27, 2016 at 16:41
1
  • Thanks for fixing the error message. I will try with the create grid function then. Vector grid works fine in QGIS itself though. Commented Aug 31, 2016 at 6:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.