Skip to content

Instantly share code, notes, and snippets.

View jlehtoma's full-sized avatar

Joona Lehtomäki jlehtoma

View GitHub Profile
@jlehtoma
jlehtoma / dissolve_polygons.R
Created February 22, 2012 19:12
Dissolve polygons
library(rgeos)
library(maptools)
library(gpclib)
if (!rgeosStatus()) gpclibPermit()
gpclibPermit()
# Read in the data
fi.kunnat <- readShapePoly("fi_kunnat.shp")
@jlehtoma
jlehtoma / kuntajako.R
Created February 23, 2012 05:47 — forked from antagomir/kuntajako.R
uusi kuntajako kehitelmää
library(sorvi)
library(rgeos)
library(rgdal)
library(maptools)
library(gpclib)
if (!gpclibPermit()) { gpclibPermit() }
##################################
# Lue uusi kuntajako
@jlehtoma
jlehtoma / Install_Postgis_Ubuntu_11.10.sh
Created March 14, 2012 14:05 — forked from Tab3r/Install_Postgis_Ubuntu_11.10.sh
Install Postgres 9.1, PostGIS and create PostGIS template on Ubuntu 11.10 Oneiric Ocelot
#!/bin/bash
#
# Install Postgres 9.1, PostGIS and create PostGIS template on a clean Ubuntu 11.10 Oneiric Ocelot box
# http://wildfish.com
# add the ubuntu gis ppa
sudo apt-get -y install python-software-properties
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get update
@jlehtoma
jlehtoma / test_crs.R
Created April 3, 2012 14:56
Plotting sp-objects to Google Maps with different CRSs
library(sorvi)
library(rgdal)
library(plotGoogleMaps)
data(MML)
shp <- MML[["1_milj_Shape_etrs_shape"]][["kunta1_p"]]
proj4string(shp)<-("+init=epsg:3047")
shp2 <- spTransform(shp, CRS("+proj=longlat +datum=WGS84"))
@jlehtoma
jlehtoma / r_market.R
Created April 28, 2012 17:22
R's market share
library(ggplot2)
library(reshape2)
library(scales)
Scholar <- read.csv("scholarly_impact_2012.4.9.csv")
Little6 <- c("JMP","Minitab","Stata","Statistica","Systat","R")
Subset <- Scholar[ , Little6]
Year <- rep(Scholar$Year, length(Subset))
ScholarLong <- melt(Subset)
@jlehtoma
jlehtoma / rli_db.R
Created May 30, 2012 18:28
RLI postgresql connection from R
library("RPostgreSQL")
# For examples, see https://code.google.com/p/rpostgresql/
connect.rli <- function(config.file="config.R") {
source(config.file)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname=DBNAME, user=USER, password=PASSWORD,
@jlehtoma
jlehtoma / jaccard.R
Created August 16, 2012 12:24
Calculate Jaccard index between 2 rasters in R
# jaccard.R
# Written in 2012 by Joona Lehtomäki <[email protected]>
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to
# the public domain worldwide. This software is distributed without any warranty.
# You should have received a copy of the CC0 Public Domain Dedication along with
# this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
@jlehtoma
jlehtoma / kendall_raster.R
Created August 16, 2012 12:45
Calculate Kendall rank-correlation coefficietn between 2 rasters
library(raster)
library(dismo)
# Sample both raster with the same points
sample.points <- randomPoints(raster1, 500000)
# Extract raster values at sample point locations
sample.raster1 <- extract(raster1, sample.points,method='simple', na.rm=TRUE)
sample.raster2 <- extract(raste2, sample.points,method='simple', na.rm=TRUE)
@jlehtoma
jlehtoma / rank_stand.R
Created August 22, 2012 12:59
Standardize value ranks between [0, 1]
# Based on:
# http://stackoverflow.com/questions/4401498/how-to-rank-values-in-a-vector-and-give-them-corresponding-values
# http://stackoverflow.com/questions/5468280/scale-a-series-between-two-points-in-r/5468527#5468527
values <- runif(1000000)
# replace runif with getValues(raster)
ranks <- as.numeric(factor(values))
# Transform variables to a scale between 0 and 1, while retaining rank order and
@jlehtoma
jlehtoma / majority_aggregate.R
Created August 27, 2012 10:55
Aggreagate with majority
library(raster)
# HUOM! Kaikissa hakemistopoluissa pitää Windows-kenoviiva korvata normikenoviivalla
# esim. C:\Kansio\tiedosto.txt -> C:/Kansio/tiedosto.txt
workspace <- "[KANSIO-JOSSA-LAHTORASTERIT]"
# Oletus: rasterit ovat yllä mainitussa kansiossa
org.raster.file <- "[ALKUPERAISEN-RASTERIN-NIMI]"
agg.raster.file <- "[AGGREGOIDUN-RASTERIN-NIMI]"