Python Scripts for QGIS
Place under  “./qgis2/processing/scripts"

#Makes a quick copy of a raster layer
#Definition of inputs and outputs
##[Raster scripts]=group
##b=number 1
##output=output raster

#Import body
from qgis.core import *
from qgis.gui import *
import numpy
from osgeo import gdal

#Algorithm body
# Get Raster input
ras = processing.getobject(input)

# Load in as GDAL Array
rasterPath = ras.source()
raster = gdal.Open(str(rasterPath))
rows = raster.RasterYSize
cols = raster.RasterXSize
nodata = raster.GetRasterBand(b).GetNoDataValue()
array = raster.GetRasterBand(b).ReadAsArray()

# Create new File
driver = gdal.GetDriverByName('GTiff')
out = driver.Create(output, cols, rows, b, gdal.GDT_Float32)

# Write array
band = out.GetRasterBand(b)

# flush data to disk, set the NoData value

# Set Geotransform
# Exports an attribute table as table
##[Vector scripts]=group
##Output_table=output table

import os
from osgeo import gdal, ogr, osr
from processing.core.TableWriter import TableWriter
from processing.core.GeoAlgorithmExecutionException import \
from import *

vector = ogr.Open(Input_vector, False)
layer = vector.GetLayer(0)
featureCount = layer.GetFeatureCount()
if featureCount == 0:
raise GeoAlgorithmExecutionException(
'There are no features in input vector.')

vectorCRS = layer.GetSpatialRef()

columns = []
featureDefn = layer.GetLayerDefn()
for i in xrange(featureDefn.GetFieldCount()):
fieldDefn = featureDefn.GetFieldDefn(i)

feature = layer.GetNextFeature()
while feature is not None:
for i in xrange(featureDefn.GetFieldCount()):
fieldDefn = featureDefn.GetFieldDefn(i)
if fieldDefn.GetType() == ogr.OFTInteger:
elif fieldDefn.GetType() == ogr.OFTReal:
feature = layer.GetNextFeature()

writer = TableWriter(Output_table, 'utf-8', [])
row = []
for i in xrange(len(columns[0])):
for col in columns:
row[:] = []

R Scripts for QGIS
Place under  “./qgis2/processing/rscripts" with file extension .rsx

# Displays a basic levelplot of an input raster using rasterVis
##[Own Scripts] = group
##layer = raster
myPal <- terrain.colors(20)
# GGmap routing script by Martin Jung
##Vector processing=group
##x = string Copenhagen, Denmark
##y = string Berlin, Germany
##type= string driving
##output = output vector
if (!require(ggmap)){print("ggmap not installed. Will install it now");install.packages(ggmap, dependencies = TRUE)}

r <- route(from=x,to=y,mode=type,structure="route",output="simple",alternatives=F,messaging=F) # get the route
cs <- CRS("+proj=longlat +datum=WGS84 +no_defs") # WGS84 projection

l <- Lines(list(Line(r[c("lon","lat")])),ID=paste0(type,"_track"))
sl <- SpatialLines(list(l),cs)

data <- data.frame(from=x,to=y,type=type,length_km=sum(r$km,na.rm=T),duration_h=sum(r$hours,na.rm=T))
output <- SpatialLinesDataFrame(sl,data=data,match.ID=F)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Sussex Research Hive

Supporting the research community at the University of Sussex

Small Pond Science

Research, teaching, and mentorship in the sciences

Landscape Ecology 2.0

intersecting landscape ecology, open science, and R


The Research Blog of IIASA

Jörg Steinkamps Blog

Mainly things about R, Linux and vegetation modeling

Amy Whitehead's Research

the ecological musings of a conservation biologist

Michael McCarthy's Research

School of BioSciences, The University of Melbourne

The Rostrum

science, statistics, policy and more


Environmental Change - Understand, Predict, Adapt

Dynamic Ecology

Multa novit vulpes


METeorological Visualisation Utilities using R for Science and Teaching

A Birder´s Blog

"Everybody loves what they know"


A new metric to quantify biodiversity response to fragmentation

Trust Me, I'm a Geographer

Using Technology to Explore Our World

Duncan Golicher's weblog

Research, scripts and life in Chiapas

%d bloggers like this: