Friday, August 31, 2012

Writing a QGIS Plugin

Such an easy title... such a smart idea... HARD WORK!!!

Anyway... after stumbling around for a bit with the PluginBuilder plugin... QGIS Cookbook etc... I finally settled on grinding through a tutorial... Which has proven the most rewarding.

I have learnt a few things about python and compiling and using the linux terminal that have proven useful.


  • The shortcut for opening the terminal window is Ctrl +Alt + t   (Yaay...)
  • Some compiling (the deadly c-word!) needs to be done.  Fortunately the pluginbuilder produces a super special file called a makefile which kind of knows what needs to be compiled and how.  All that I need to do after changing the user interface or editing the UI dialog code is to open the terminal :) select the directory in which the plugin is situated (cd ...) and type "make". The compiling is done!
  • To get the directory string (to type after the cd command) I simply open it in a window and right click on the relevant directoy tab and copy it and paste as file name into the terminal window.
  • at first the compiling di not work properly, I found that by simply typing pyrc4 or whatever package is necessary for the compiling, I was given instructions in the terminal as to how to go about installing it.
Those are just somelessons... as for writing the plugin... I suggest that anyone who wants to do this and is not a knowledgeable programmer finds a mentor and grinds throught the tutorial!

to be continued...!

Ramblings...

OK so what's been happening...


  • I've tried to get some work done meanwhile the fish have gone hungry...
  • I've switched from Mac to Windows to Linux Ubuntu 12 and I am finally happy... except I miss an MSOffice that works properly.
  • I've got (more) clarity on the direction of my research, visited the study site, a beautiful pine plantation not too far away...
  • I've been figuring out R... modelling relationships between various tree parameters... or at least trying to!
  • I read an good book last night... The Go-Giver.  The principle being that we will be more successful if we give... stuff, of ourselves, of what we know, etc.... Hence the public sharing of my blog... Maybe someone could get something somewhere in it!
  • I'm currently working on developing a plugin for QGIS that would contribute a useful tool as part of my research "package" that I hope to deliver by the end of next year.
  • I found a great song for motivation :) ... The Script - Hall of Fame


Happy weekend!

Monday, July 16, 2012

Getting the SDA2PP plugin to work in QGIS. RPy2

Installing this plugin has proved to be a prolonged process.  I want to do Krigin and other interpolations in QGIS and have tried to install this plugin for these purposes.

To start off with the plugin was not even available on the list under Plugins>Fetch Python Plugins

After some searching and experimeting i found the plugin by selecting show all plugins, including experimental ones, although it does not appear to be experimental.
After selecting this option it became clear that much previous packages etc are required. These include rgdal, sp, RPy2, pywin32, numpy and an update of the python framework on my machine.  I am using a 64bit machine but it is not easy to work within a 64 bit pytho framework as many of the additional packages and moodules are not easily obtainable for 64 bit but only in 32 bit.  I therefore have kept to 32 bit to save headaches.

Most of the installs are not too complicated and can be found easily.  However I have struggled to install RPy2.

There is no precompiled self installing package for Rpy2 so it has to be done in another way.  The recommended way that i have had the most success with is using easyinstall, part of a package that is called setup tools.  I was able to install this package without any problems but could not get easy install to work.  Easy install downloads and installs packages etc from PyPI with the simple command
>easy_install rpy2
It is not all that simple however.  For this command to work the following is necessary after installation:

  • Ensure that the Command Prompt is being used and not the Python command line.
  • The Path variable needs to contain the directory in which easy_install.exe is... C:\Python27\Scripts\
  • The path also needs to contain a directory for the R executable.
Changing the Path variable is done as shown in the following figure


http://code.google.com/p/sda4pp/downloads/detail?name=SDA4PP-System-0.9.0.exe&can=2&q=#makechanges

Installing Rpy2 on windows does not appear to be possible at this stage.  I am switching to linux.


Thursday, July 5, 2012

Modelling individual tree in R using R Commander

And so I experiment modelling my tree data to get some kind of correlation between the coordinates and the tree size...  Also just to get a feel of the R commander GUI and the possibilities that R presents.  What I actually want to do is get an equation for the regression graph that was obtained in the previous post:




First it is necessary to create a model... tree size as a function of x and y coordinate!


Plotting Individual tree data in R. Windows,R Commander

I have switched to a 64-bit windows operating system and have also collected some of my own individual tree data using a surveyors total station.

For analysis of this data I started using  the steps as explained here.  However, I was unused to the windows interfadce and could not locate the command history window.  Doing some research to find the solution I came across an interesting GUI for R called R commander. It offer a variety of the analysis tools in drop down list form to from the commands that are then used as command lines.  This helps beginners like myself to get quicker results and also helps me to see what can be done in R.


Being a lover of 3d representations, plotting my data in 3d is one of the first things that I tried.

Importing the .csv data:


Name the dataset, ensure that "commas" is selected and the correct decimal -point character
On the tpop of the interface the active dataset is now displayed and one can view the data or edit the data directly in cells as in a spreadheet which is very useful.


One the dataset is active any of the manipulations from the above menus can be carried out. My selection: "3D Scatterplot"


The options are as follows:

I selected DBH as response variable and the x and y coordinates of the tree positions as explanatory variable and I wanted to see the smooth regression.  The result, a nice representation of the spatial distribution of tree size:
Saving the graph to file is an option in the R Commander GUI in the "Graphs" menu.

Now to get some kind of equation for this regression.... Modeeling in R using R Commander

Collecting Individual tree data Using a Surveyor's Total Station

Thursday, April 26, 2012

Raster Analysis Using R

The raster package in R contains a number of interesting tools for raster analysis.  It can be used within QGIS to make for easier importing of data etc. but I have been using R as standalone because of the manageR problem that necessitates a reboot.  It is however not too difficult to do the work in R directly. Loading the gdal library...

require(rgdal) 
>data = readGDAL(file.choose())
/Users/andrewise/Documents/Stellies/FS 884/Shapefiles/GIS/Correct/Grabouw/30deg100.asc has GDAL driver GTiff
and has 294 rows and 250 columns
> data
Object of class SpatialGridDataFrame
Object of class SpatialGrid
Grid topology:
  cellcentre.offset cellsize cells.dim
x         -6561.105      100       250
y      -3798028.105      100       294
SpatialPoints:
                   x        y
    [1,] -6561.10486 -3768728
    [2,] -6461.10486 -3768728 ...etc

It is possible to extract the cell coordinates. However this does not produce values. The following function brings up the help file for the "raster" library.

>??raster

This library contains a number of useful analysis functions.

I assigned the variable "r" to a coarse 200x200m raster of my data.

 > r <-raster(file.choose())
Checking what my variable represents:
> r
class       : RasterLayer 
dimensions  : 147, 125, 18375  (nrow, ncol, ncell)
resolution  : 200, 200  (x, y)
extent      : -6611.105, 18388.9, -3798078, -3768678  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=19 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs 
values      : /Users/andrewise/Documents/Stellies/FS 884/Shapefiles/GIS/Correct/Grabouw/30deg200.asc 
layer name  : X30deg200
The unique values of the raster:
> unique(r)
 [1]   0   1   2   3   5   7   8  10  11  12  16  20  22  30  39  40  49  56  78 997 998 
A list of all the raster values (many 0s, important to specify null values in the rasterization process):
> values(r)
    [1]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1
   [36]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   [71]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  [106]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  [141]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1 998   1   0   0   0   0   0   2   0   0   0   0   0   0   0   0
  [176]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  [211]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  [246]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  [281]   5  22   1   5 998   1   1   0   0   0   5   2   2   2   7   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
And finally, what I have been searching for, a summarized frequency table of the raster:
> freq(r)
      value count
 [1,]     0 15383
 [2,]     1    25
 [3,]     2   923
 [4,]     3     1
 [5,]     5   367
 [6,]     7    31
 [7,]     8    11
 [8,]    10   159
 [9,]    11    14
[10,]    12   362
[11,]    16     1
[12,]    20    80
[13,]    22   105
[14,]    30    26
[15,]    39     1
[16,]    40    22
[17,]    49     1
[18,]    56     2
[19,]    78    19
[20,]   997    38
[21,]   998   804
It seems strange that there is no plugin or similar to obtain this data.  However, I would now like to somehow relate this data to the area which each value covers, to then evaluate the accuracy of the different raster resolution renderings and also with the original vector shapefile.

To visualize the matrix in R

as.matrix(r)
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
  [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
  [2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
  [3,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
  [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
  [5,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
  [6,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA    NA    NA  

Defining:

>a <- area(r)
And using the zonal function presents the following area output with area per zone which I can then use for comparison when looking at the different resolution rasters... SUCCESS!!!
> zonal(a,r,sum)
      zone      sum
 [1,]    1  1000000
 [2,]    2 36920000
 [3,]    3    40000
 [4,]    5 14680000
 [5,]    7  1240000
 [6,]    8   440000
 [7,]   10  6360000
 [8,]   11   560000
 [9,]   12 14480000
[10,]   16    40000
[11,]   20  3200000
[12,]   22  4200000
[13,]   30  1040000
[14,]   39    40000
[15,]   40   880000
[16,]   49    40000
[17,]   56    80000
[18,]   78   760000
[19,]  997  1520000
[20,]  998 32160000

> freq(r)
      value count
 [1,]     1    25
 [2,]     2   923
 [3,]     3     1
 [4,]     5   367
 [5,]     7    31
 [6,]     8    11
 [7,]    10   159
 [8,]    11    14
 [9,]    12   362
[10,]    16     1
[11,]    20    80
[12,]    22   105
[13,]    30    26
[14,]    39     1
[15,]    40    22
[16,]    49     1
[17,]    56     2
[18,]    78    19
[19,]   997    38
[20,]   998   804
[21,]    NA 15383

 LONG WEEKEND!


# multiple layers
zonal(stack(r, r*10), z, 'sum')

Ok so I have tried multiple layers and have not been completely successful.

My steps:

  1. Put all raster files into one folder
  2. Identify the location of the rasters
    >locat
    [1] "~/Documents/Stellies/FS 884/Shapefiles/GIS/Correct/Grabouw/AllRasters"
  3. Identify the class of each raster as "raster" individually:
    >d<-raster(rastfiles[4])
    f<-raster(rastfiles[6])
    e<-raster(rastfiles[5])
    h<-raster(rastfiles[8])
    g<-raster(rastfiles[7])
  4. Combine the frequency table of all the rasters in one matrix
    freqs<-cbind(freq(a),freq(b),freq(c),freq(d),freq(e),freq(f),freq(g),freq(h),freq(i),freq(j),freq(k),freq(l))
  5. Convert the matrix to a data frame for easier manipulation
    freqsdf<-data.frame(freqs)
  6. Rename the columns for easier identification
    > colnames(freqs) <- c(1:24)
  7. Create data frame again with clearer column headings
    freqsdf<-data.frame(freqs)
  8. Extract the column names as variables
    > attach(freqsdf)
  9. Create new data frame with only one column for the types and all the other columns of the matrix values
    > sumfreq<-cbind(X1,X2,X4,X6,X8,X10,X12,X14,X16,X18,X20,X22,X24)
  10.  Change the column names to desired
    > colnames(sumfreqs) <- c("Type","A","B","C","D","E","F","G","H","I","J","K","L")
  11. Export raster as comma delimited
    locatout <- "~/Documents/Stellies/FS 884/Shapefiles/GIS/Correct/Grabouw/outputfreqs.xls"
    > write.table(sumfreqs,locatout, sep="\t")
  12. Continue analysis as spreadsheet using pixel size to calculate the area.

Wednesday, April 25, 2012

Plotting Individual Tree Data in QGIS

So today I will try to do in QGIS what I did yesterday in R... Plotting the different trees in a space with some indication of their relative sizes. And then try to draw their "growing area" i.e the space which each tree takes up.

OK so inporting the data to qgis... here.

The output...
Graduating by colour and size...

To change the scale of the symbol graduation it is necessary to adjust the individual symbol size, adjusting until the relative size is reasonable:

A useful visualization using tree height:
And the dbhs of the same dataset...
Overlaid, the data shows a nice correlation between dbh and height
And now i would like to calculate the individual growing area for each tree...
Vector>Geometry Tools>Voronoi Polygons
This plugin creates a polygon for each tree where the lines are drawn at a distance halfway to each neighbouring tree.

Or we could do a Delaunay triangulation and sum the area of all the triangles touching each tree and compare that...

 And then do some kind of statistical analysis verifying whether the "growing area" as defined in these two representations have a significant relationship with the height or dbh... (Or perhaps a shortest line relationship??) Just looking at these representations it seems that dbh has a wider range of values and might be more interesting to work with.

A quick inverse distance wighted interpolation with a a distance coefficient of 4,5 renders the following...
Which is not very informative yet but could probably be done at a higher distance for more conclusive interpolation... But we probably require a larger sample to have environmental factors represented in the interpolation.... And so tomorrow we will be doing an analysis of the areas and tree sizes using R in QGIS via the ManageR interface... here!

Just a quick update...
An interpolation with a distance coefficient of 2 seems more reasonable. It would be interesting to see the soil data for the site.

Tuesday, April 24, 2012

Plotting Individual tree data in R


>plotvals=read.csv(file.choose())

>plotvals
    comp plot DBH.     xco     yco     zco    height
1    B83    A 11.3  1.1715  2.5170  0.0740  9.447108
2    B83    A 15.9  0.7335  6.5940  0.0020 11.781058
3    B83    A 11.0  0.8530 10.5740 -0.1420  9.294894
4    B83    A 19.1  0.5455 12.8140 -0.1710 13.404675
5    B83    A 11.1  0.7155 14.5720 -0.2460  9.345632

> plot(xco~yco,data=plotvals)


New tutorial here.  
>plot3d(plotvals$xco, plotvals$yco, plotvals$height, pch=16, highlight.3d=TRUE,type="h", main="3D Scatterplot")
My forest is drawni in R! More or less...

And now... using an interesting library called ggplot...

Error: object 'ggplot' not found

First load the library...

> library(ggplot2)
And to extract the column headings as variables


> attach(plotvals)
>df=data.frame(xco,yco,height)
> df
        xco     yco    height
1    1.1715  2.5170  9.447108
2    0.7335  6.5940 11.781058
3    0.8530 10.5740  9.294894
4    0.5455 12.8140 13.404675
p <- ggplot(df, aes(x,y,size=z)) +geom_point()
> p + geom_point() 
Nice!





To do list!

  1. Build plugin for qgis raster table. Not necessary. Use R -> freq()
  2. Analyze raster data in manageR for all different rasters generated... Multiple dataset manipulation R
  3. Plot individual tree data in R
  4. Plot individual tree data in qgis and interpolate heights and dbh - compare
  5. Plot individual tree data in qgis and divide into polygons (growing area) - Analyze
  6. Develop inventory simulator plugin for qgis
  7. Develop way of calculating "what's on the edge" - Plugin qgis??
  8. Take topography into account

Monday, April 23, 2012

Could the Real Modern Education Please Stand Up?

And so this morning I had to mark a first year test... Although it stole time from my normal work it was a bit of a thought-provoking experience... As I shake my head in dismay at the answers in front of me I think of these smartphone-toting highly-informed youngsters and realise that they probably know much more about current affairs and pop culture than what I will ever know regardless of how many novels and magazines I read... And they could probably google a question quicker than I could prise it from the misty recesses of my memory.
I realise that obtaining the knowledge for the subject they are trying to master is not a priority for them.  As a matter of fact it is clear that mastery is not what they are trying to achieve.  They are trying to obtain a qualification that is required by an old-minded world which it sees as an indication of mastery.  However this belief is not shared by these free-minded twitter-brained youngsters.  They do not accept the necessity of mastering a fact, concept or equation that could be clearly explained or summarized in the first paragraph of a wikipedia entry.  And they are right. As far as the concept itself goes. But how does one develop a thought that we do not understand?  What do we hang things on in our minds when there are no hooks to hang them on?  The answer to a single question is easy to find. But the answer to problem is not. Problems are not solved by straight fact but by reason, and for reason to work requires understanding. And something is only understood when an acceptable framework is available in our minds in which to manipulate a thought or problem.  Education creates these frameworks.

The question is how to create these frameworks in this new kind of young, stimulation craving, concentration deficient mind?  This is the challenge that educational institutions, and society in general, face when looking to meet the grave necessity of education for our sustainable future. With institutions themselves being (and rightfully so) the stalwart defenders of tradition, history and the status quo in general, they are the most unlikely candidates for instituting the change in the system that seems to be so urgent.  It is rather a sticky situation. But if anyone's got the answer it's probably the students who wrote this test... And if they dont have it... they'll just google it!

Some more interesting discussion on the topic in the post education place... For Millennials, Chat Rooms Are the New Conference Rooms

Saturday, April 21, 2012

Manually Installing a QGIS Plugin

See this website.
How to install python plugins in QGIS
Method 1 - manual install:
  1. Close QGIS if it is open.
  2. Unzip the file downloaded to your home/.qgis/python/plugins folder. Few other plugins folder should already be there.
          Under windows, 'home' is something like: C:\Documents and Settings\<user name>; where <user name> is your login name.
  3. Restart QGIS, the plugin should now be available in the plugin list under the 'Plugins' menu 'Manage plugin...'.
  4. In this list select the plugin (e.g. bcccoltbl) and click OK.
  5. The plugin is now available in the 'Plugins' menu and on the plugin toolbar.
To open the qgis plugins folder right click on the qgis application and show package contents.

Update: In windows this folder is C:\Program Files (x86)\Quantum GIS Wroclaw\apps\qgis\python\plugins

Friday, April 20, 2012

The Mystery of Compiling on a Mac

During all my stuggles I have had to do something called "compiling"... now this basically means getting some open source component installed in the right place so that it would work well together with the others in the python framework... This was like a huge black pit for me...  I finally figured out that I need something called gcc... which translates to a program called Xcode on the mac.  This is basically a package for developing apps and software for the mac. But more importantly it includes the magic gcc...
However... as is usually the case... even after a costly (for my internet account) and timely (you have to register as a mac developer) download and install gcc wouldn't work from the terminal... even after reboots and such. Ok.. so I open this Xcode, a very friendly interface... tempting for developing software for mac! Anyway, without getting distracted i realise that this has to go through a further install. I run that.
Still no gcc... I'll call gcc "the unicorn" for now... and go to the gym to work out whats been building up inside!


UPDATE! While trying to install another package called qwt, which requires qt, which in turn requires the proper functioning of Xcode for the install I find this link which says that we need to install the command line tools within xcode to get it all to work. It's like this XCode->Preferences->Downloads. Another costly install later... I think this is going to work! It does!

Open Source - Money vs Time vs Capability

I think the heading just about says it all... and its probably a bit of an open question.

There are however some certain facts:

  • Open source costs less but takes longer.
  • Non opensource is generally better rounded off - better interface, help files etc
  • In open source the possibilities are endless... where the imagination ends, open source carries on!
There are probably more to this and I will add as I go along, but its also probably safe to say that if we are going to do open source we need to go all out open source... Learn python, use open source OS, go to open source conferences, believe in open source, have patience in open source, share... It is probably all equal in the end... with open source having slightly more pure motives from a moral point of view, but all things have their time and place.

QGIS - Raster Analysis using R - Installation

I have managed to get R interfaced tentatively with qgis using the manageR plugin. It still crashes on using certain functions and then manageR does not work in qgis until a complete reboot.

I have installed the sp and raster packages for R and there appears to be many other packages that could be applicable.  The big problems with the correct functioning and installation of the R plugin was with the installation of matplotlib.  Installing all the newest packages did not work so in some cases the previous versions had to be used.  This is a trial and error experience.  Also the question of 64bit vs 32bit on the mac (which runs 64bit OS X) is still unclear.  I believe that QGIS runs in 32bit and therefore interfaces with 32bit R.  And the matplotlib and other packages need to be 32bit.  I'm not completely certain about this yet.
R needed the language to be set to english to remove the  ASCII problem. I also had to install xcode for mac to get the gcc compiler working to install *.tar.gz packages.

For manageR to work the sp and rgdal packages are necessary. These can be installed using the GUI of R. under the packages menu>install packages. It's necessary to select install to local host and also install dependencies.  For rgdal to function the gdal installation is required. When i tried to install rgdal throuhg the R GUI it could not find the gdal config file. I think it is a path issue but I could not resolve it. I downloaded the rgdal package from the kyngchaos website and followed the instructions in the readme. This seems to have worked.

I have had to learn a bit of python command prompt commands... most importantly, setting the directory of the folder containing the package:

  • cd ~/downloads/
  • open .bash_profile
  • R CMD INSTALL packagename.tar.gz
  • echo $PATH
  • ls -a (to see all terminal related files)

It's case sensitive for commands.



Anyway, the manageR plugin seems to be working. Now how to use it!?

Doing raster analysis in R requires the "maptools" and "sp" packages.

The "maptools" installation requires various packages, amongst which is the "rgeos" package, which can be installed directly in the R interface.  This install requires the GEOS framework which is available (with most of the solutions to my Mac OSX installation solutions) on the Kyngchaos website. I installed both the GEOS framework and the "rgeos" package that was available on the website. The GEOS framework is self installing but the "rgeos" package needs to be downloaded and then installed within the R interface selecting Packages & Data > Package Installer and then selecting Local Source Package, At User Level, Clicking Install and selecting the unzipped "rgeos" folder.

Raster Analyis in QGIS - 1 Band Color Table Data

Today I have tried various options for analysing my rasters in QGIS. As said before My objective is to do raster analysis on qgis of rasters representing the same forested area and the different land use types at differing resolutions. I have started doing it through R but have decided that I first would like some kind of output for the data that I can understand.  I started with the 1-band colour table plugin but that crashes qgis every time and requires a reboot of the system after which it crashes the system again.  I have tried to reinstall the plugin but that has not been successful as it has been removed from the repository.  I have therefore had to download the plugin form the website of the creators here and follow the installation instructions.


Running this plugin for a raster creates an *.xml file in the same directory as the raster.  I have opened these without too much success before but upon opening it now, it automatically opened in Xcode... and mostly it makes no sense but there is one pretty interesting thing that I saw...
Now this is exciting because it seems that those are the raster value counts... Starting with one, all the way to 998, the highest value in my raster, with each "cell" representing a value and a count of the times in which that value appears in the raster. Now the question is how can i get that in some useful format to analyse in R or perhaps even write a short plugin for this...


It would appear that the function that collects these data is GetRasterBand or GetStatistics. These can both be called in a python code.

Raster Rendering in Qgis - Rasterize Function

Rasterize create smaller rasters where the no daata values are excluded from the raster.  To do this include the -a_nodata value=0 function in the command line of the rasterrize tool, where 0 is the nodata value. The process is as follows:


Trying the -at function. Rasterize based on first value encountered in processing... Top left, bottom right? Seems random...




But does not do a better job than simply using the defaults and taking the centre point...

Except in the case of lone-standing, small featues where the centrepoint would be easily missed as in the following case

Versus this case, where the - at function is used and no feature is missed in the rasterization process


Using R in Qgis - ManageR - Basic Functions

My objective is to do raster analysis on qgis of rasters representing the same forested area and the different land use types at differing resolutions.  There does not seem to be any way of doing this directly in QGIS and the best suggested method seems to be through R, An open source data manipulation and representation software. QGIS interfaces with R via a plugin called manageR developed by Peter Carson.

The interface is not very useful in itself... I can now import a raster to the interface after installing the sp and rgdal packages. I would now like to get an analysis of the different values of the raster. An idea of the relative values of the raster, be it via a count, a histogram, a percentage. I have tried the various menus and not much seems to appear. I have the name of the raster visible in the  workspace manager window.

It is possible to export the variables of the band as a table *csv and html by right clicking on the band. The resultant table does not however provide me with much information that I find useful.

Ok so after a bit of messing around I have decided to just do some basic R learning... I plot my first graph from a tutorial here. I feel powerful :) I have just created a graph in R... inside QGIS. HOwever it has nothing to do with my spatial data...
How do I reference my imported raster??
I dont know... I try more stuff from the tutorial...
Another tutorial on the basics of R: R Tutorial... I am starting to see the light!
I load a csv table that I created above... using:
  • rast = read.csv(file.choose(), header=T)
The table is stored with the variable "rast". To access this table i just type in that variable.  The table is also accessible from the workspace browser.  That is where the raster was imported to in manageR!

I realise that to do an analysis of data... it needs to be in a format that I at least understand... Which makes sense. I redirect my thoughts to defining the different values of the raster... where does the data get stored with the values for each cell of the raster? I do some looking around... I'm going to try to create a colour table for the raster... I have tried this before without much success... but I have found a good tutorial

After some R practice using the plotvals dataset I understand the interface a bit better and was quickly able to make an R plot within GIS using manageR.
However the original question was the analysis of raster data.

I found an interesting post and tried to load the "maptools" library. it was not installed so I did that.
>library(maptools)

To write the data to a tab delimited excel file:

> locat <- "~/Documents/Stellies/FS 884/Shapefiles/GIS/Correct/Grabouw/output.xls"
> write.table(za1,locat, sep="\t")