R example moving window script (focal convolution matrix) using the raster package #463

Merged
merged 0 commits into from May 13, 2014

Conversation

Projects
None yet
5 participants
Contributor

MatMatt commented Mar 7, 2013

The creation of Help files would be important, but it is somehow not working properly yet.
Maybe I can try to put my hands on this issue soon.

Contributor

volaya commented Mar 7, 2013

the script is great, but have you tested that the stop() call works and doesn't break QGIS? interrupting the QGIS/SEXTANTE flow with an external dialog might cause strange things, I am afraid. If so, maybe I could implement some way of communicating from R to SEXTANTE, so R can log warnings in SEXTANTE, and that can replace your solution.

Contributor

MatMatt commented Mar 7, 2013

Ok yes that would be great. Count on my support!
Am 07.03.2013 16:50 schrieb "volaya" notifications@github.com:

the script is great, but have you tested that the stop() call works and
doesn't break QGIS? interrupting the QGIS/SEXTANTE flow with an external
dialog might cause strange things, I am afraid. If so, maybe I could
implement some way of communicating from R to SEXTANTE, so R can log
warnings in SEXTANTE, and that can replace your solution.


Reply to this email directly or view it on GitHubhttps://github.com/qgis/Quantum-GIS/pull/463#issuecomment-14568169
.

Contributor

MatMatt commented Mar 7, 2013

And Ok I'll check the behavior on failure tomorrow, but I think SEXTANTE has to deal with such situations soner or later... As you are strating R in a batch job on stop() R will probably simply exit as it would do when it finishes, an idea could be to always insert an on.exit() function to communicate a failure... more tomorrow!

Contributor

MatMatt commented Mar 8, 2013

Ok I tried, SEXTANTE breaks with its typical error
Screenshot from 2013-03-08 09:05:35
But it would be important to have some communication between R messages and SEXTANTE, maybe the console output as written in an former mail could be a simple and efficient method, with the disadvantage that R-script writer need to take care of it in the code, and that the rest of the not explicitly coded messages (other interruptions and warnings) are not considered.

Hier the Rout:

R version 2.15.3 (2013-03-01) -- "Security Blanket"
Copyright (C) 2013 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

options("repos"="http://cran.at.r-project.org/")
deflibloc <- .libPaths()[1]
.libPaths(c(deflibloc,"/home/matteo/.qgis//sextante/rlibs"))
if (!require(rgdal)){

  • install.packages("rgdal", dependencies=TRUE, lib="/home/matteo/.qgis//sextante/rlibs")
  • }
    Loading required package: rgdal
    Loading required package: sp
    rgdal: version: 0.8-5, (SVN revision 449)
    Geospatial Data Abstraction Library extensions to R successfully loaded
    Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
    Path to GDAL shared files: /usr/share/gdal/1.9
    Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
    Path to PROJ.4 shared files: (autodetected)
    if (!require(raster)){
  • install.packages("raster", dependencies=TRUE, lib="/home/matteo/.qgis//sextante/rlibs")
  • }
    Loading required package: raster
    raster 2.1-4 (8-February-2013)
    RasterFile = "/home/matteo/ARC_GEO/LANDSAT_ARC/LS_timeSeries/Lehrforst/RESULTS/kNN/HAwise/LS90/kNN_k5_meanS_LS90_LT5_2011237_ref.tif"
    RasterLayer="all"
    w="3,1,2"
    Function="mean"

library(raster)

if(tolower(RasterLayer)=="all")

  • {
  • RasterFile <- brick(RasterFile)
  • outputRasterFile <- stack(RasterFile)
  • } else
  • {
  • RasterFile <- raster(RasterFile,band=as.numeric(RasterLayer))
  • outputRasterFile <- raster(RasterFile)
  • }

w <- as.numeric(strsplit(w,",")[[1]])

if(length(w)>1)

  • {
  • if (sqrt(length(w))%%2!=1)
    
  • {  
    
  •     stop("It is not possible to create a convolution matrix from your 'w', check the length of your 'w', it must be possible to create a 3x3, 5x5, 7x7... matrix\n")
    
  • }
    
  • w <- matrix(w, nrow=sqrt(length(w)))
  • Function <- NULL
  • }
    Error: It is not possible to create a convolution matrix from your 'w', check the length of your 'w', it must be possible to create a 3x3, 5x5, 7x7... matrix
    Execution halted
Contributor

MatMatt commented Mar 9, 2013

What do you think to catch every rout line that doesn't start with ">"? In rout the normal code is always anticipated with ">" warnings and errors have nothing, they are just printed.
but it would need to examin the rout for this before the script gathers the output (raster, vector...).
if non empty lines without ">" (and maybe "+") are found in rout it is always a message for the user. Thus will open a window like the consoleOutput you already have?

volaya was assigned Mar 12, 2013

Contributor

volaya commented Mar 23, 2013

Hey sorry for the delay. Hmmm, that sounds like a good idea. It would be great if the author of the script could create those outputs. Do you know anyway to print things from an R script, so they do not appear with the ">" symbol? We could separates outputs with a given symbol, so only explicit errors created by the script would be catched. It woul be more difficult for script authors, but safer. what do you think?

Owner

mach0 commented Mar 27, 2013

MatMatt on easter holiday - Will be back next tuesday .. :) FYI

Contributor

MatMatt commented Mar 28, 2013

Hi, thx Werner!!
Next Thuesday I'm back to office and I'll try to find some answer.

Cheers Matteo

2013/3/27 Werner Macho notifications@github.com

MatMatt on easter holiday - Will be back next tuesday .. :) FYI


Reply to this email directly or view it on GitHubhttps://github.com/qgis/Quantum-GIS/pull/463#issuecomment-15518419
.

Contributor

MatMatt commented Apr 2, 2013

Hi,

I had the idea to harvest rout for keywords (ie: "Error", "Warning"...), but the problem is that it is language dependent.
An other idea could be to enlower the alert level in R using: >options(warn=-1) so that only errors are printed. But I think it is important to communicate also warnings and somehow the errors that are more important need also a handling.
It exists also a R-server, for many aspects a very interesting option because R and SEXTANTE could be connected much more directly... but it may requires additional installations and it not supported or easy to install on all OS's.

I think the simplest and quite complete way ( a little bit dirty) is to collect all non empty lines in rout that do not starting with an ">" (or and "+". I'm not sure if this "+" is only a print interpretation or really existing in rout) in a similar manner how your Python scripts are already doing it now).
Any R print from: warning, error, print(), cat() do not have the ">" in rout. Probably warning and error are using cat() but I don't know.
It is important to collect all these prints as also errors and warnings from lower level functions (not explicitly written in the script, like the stop(), warnings(), cat() in the movingwindow.rxe example) contain important information.
So I'm not sure if it is good to collect only those prints defined by the script creator.
You could use the warn=-1 option ('>options(warn=-1)') in the SEXTANTE R options so if a user prefers less info it can be reduced

So
1: Sextante generates the R execution stript and executes it.
2: Sextante checks for relevant prints and prints them
3: Sextante prints an error (could not load Layer:) if it happens

2 and 3 could be merged to 1 pop-up message

Contributor

mhugent commented Sep 12, 2013

assigned to @volaya

Owner

mach0 commented Oct 15, 2013

@volaya : is this still a valid request? or is it already implemented? otherwise it could be closed (it's 7 month old)

Owner

mach0 commented Nov 1, 2013

@volaya bring that up again ..

Owner

mach0 commented Jan 17, 2014

@MatMatt @volaya still valid? it is the oldes pull request now :)

Contributor

MatMatt commented Jan 17, 2014

Hi,
I don't know what to do with it, I think it works, or at least it worked
when committed. Victor please decide if you want it or not, for me its not
a problem.
Cheers Matteo

2014/1/17 Werner Macho notifications@github.com

@MatMatt https://github.com/MatMatt @volaya https://github.com/volayastill valid? it is the oldes pull request now :)


Reply to this email directly or view it on GitHubhttps://github.com/qgis/QGIS/pull/463#issuecomment-32596891
.

Member

3nids commented Apr 28, 2014

@volaya can you have a look at this please?

@mhugent mhugent merged commit 1ed94d0 into qgis:master May 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment