Scripts

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
##input=raster
##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)
band.WriteArray(array)

# flush data to disk, set the NoData value
band.FlushCache()
band.SetNoDataValue(nodata)

# Set Geotransform
out.SetGeoTransform(raster.GetGeoTransform())
out.SetProjection(raster.GetProjection())
# Exports an attribute table as table
##[Vector scripts]=group
##Input_vector=vector
##Output_table=output table

import os
from osgeo import gdal, ogr, osr
from processing.core.TableWriter import TableWriter
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
from processing.tools.vector 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)
columns.append([fieldDefn.GetNameRef()])

layer.ResetReading()
feature = layer.GetNextFeature()
while feature is not None:
for i in xrange(featureDefn.GetFieldCount()):
fieldDefn = featureDefn.GetFieldDefn(i)
if fieldDefn.GetType() == ogr.OFTInteger:
columns[i].append(feature.GetFieldAsInteger(i))
elif fieldDefn.GetType() == ogr.OFTReal:
columns[i].append(feature.GetFieldAsDouble(i))
else:
columns[i].append(feature.GetFieldAsString(i))
feature = layer.GetNextFeature()

writer = TableWriter(Output_table, 'utf-8', [])
row = []
for i in xrange(len(columns[0])):
for col in columns:
row.append(col[i])
writer.addRecord(row)
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
##showplots
library(rasterVis)
myPal <- terrain.colors(20)
levelplot(layer,col.regions=myPal,contour=T)
# 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)}
library(rgdal)

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)

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

nexus

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

marionpfeifer

Environmental Change - Understand, Predict, Adapt

Dynamic Ecology

Multa novit vulpes

metvurst

METeorological Visualisation Utilities using R for Science and Teaching

A Birder´s Blog

"Everybody loves what they know"

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: