Skip to content

Instantly share code, notes, and snippets.

View jlehtoma's full-sized avatar

Joona Lehtomäki jlehtoma

  • Helsinki
View GitHub Profile
@jlehtoma
jlehtoma / count_raster_cells.R
Last active January 22, 2024 21:20
Count non-zero raster cells in R
library(raster)
# Määritä tulostiedosto
results_csv <- "original_areas.csv"
# Aseta työkansio
setwd("[TYÖKANSIO]")
# Listaa kaikki tif-tiedostot kansiossa
feature_files <- list.files(pattern = "*.tif$")
# Apufunktio solulukumäärien laskemiseen
@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 <joona.lehtomaki@gmail.com>
# 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 / print_warning.R
Created December 7, 2015 13:32
Print warnings as they happen with a for-loop in R
war <- function(x) {
suppressWarnings(w <- warning("There's a problem with: ", x))
message(w)
return(x)
}
for (i in 1:5) {
Sys.sleep(1)
war(i)
}
@jlehtoma
jlehtoma / generate_shx.py
Created January 14, 2015 14:08
Re-create missing .shx-file for ESRI shapefiles
# Based on solution given here: http://geospatialpython.com/2011/11/generating-shapefile-shx-files.html
# Depends on pyshp
# Build a new shx index file
import fnmatch
import os
import shapefile
# List all the shapefiles
def find(pattern, path):
@jlehtoma
jlehtoma / plot_funding.R
Last active March 13, 2019 20:03
Opetus- ja kulttuuriministeriön HY:lle myöntämän perusrahoituksen kehitys 2010–2020
library(hrbrthemes)
library(tidyverse)
dat <- tibble::tibble(year = factor(c(2010:2017, paste0("A", 2018:2020))),
funding = c(400, 410, 431, 423, 431, 446, 404, 395,
390, 383, 374))
p1 <- dat %>%
ggplot(aes(x = year, y = funding)) +
geom_bar(stat = "identity", width = 0.4) +
@jlehtoma
jlehtoma / hexfin.R
Created February 2, 2019 13:17
Create hexagon grid for Finland
library(magick)
library(raster)
library(rgeos)
library(sf)
library(sp)
library(tidyverse)
set.seed(1)
# Helper functions --------------------------------------------------------
@jlehtoma
jlehtoma / hki_aluejako.R
Created June 6, 2018 18:08
Get aluejakokartat data for Helsinki
library(sf)
# Define source from where the data is downloaded
source_url <- "https://kartta.hel.fi/avoindata/aineistot/HKI-aluejako-1995-2016-gpkg.zip"
target_dir <- "data"
# Make target dir if it doesn't exist
if (!dir.exists(target_dir)) {
dir.create(target_dir)
}
@jlehtoma
jlehtoma / ohjeet.md
Last active April 18, 2018 17:59
Ohjeet Zonation-rasterin visualisointiin QGIS-ohjelmalla

Nämä ohjeet on tehty QGIS:in 2.18 -versiolla. Uusin versio on 3.0, jossa komentojen nimet / paikat ovat saattaneet hieman muuttua. QGIS-ohjelman voit ladata täältä.

Analyysien taustat, aineistot ja toteutus on kuvattu raportissa Monimuotoisuudelle tärkeät metsäalueet Suomessa. Tutustu raporttiin ennen tulosaineistoihin perehtymistä!

  1. Lataa Monimuotoisuudelle tärkeät metsäalueet 2018 (Zonation) -aineisto koneellesi ja pura zip-tiedosto.
  2. Layer > Add Raster Layer > valitse joku Zonation-tulosrastereista
  3. Lataa värimäärittelytiedosto täältä koneellesi ( selaimessa Save As tms)
  4. Tuplaklikkaa Layer Panel:issa 2-kohdassa lisäämääsi Zonation-tulosraster
@jlehtoma
jlehtoma / zonation_RdYlBu.qml
Created April 18, 2018 17:06
RdYlBu color scheme for Zonation output rasters
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="2.1.0-Master" minimumScale="0" maximumScale="1e+08" hasScaleBasedVisibilityFlag="0">
<pipe>
<rasterrenderer opacity="1" alphaBand="0" classificationMax="1" classificationMinMaxOrigin="User" band="1" classificationMin="0" type="singlebandpseudocolor">
<rasterTransparency/>
<rastershader>
<colorrampshader colorRampType="DISCRETE" clip="0">
<item alpha="255" value="0.2" label="0.00 - 0.20" color="#4575B4"/>
<item alpha="255" value="0.5" label="0.20 - 0.50" color="#91BFDB"/>
<item alpha="255" value="0.75" label="0.50 - 0.75" color="#E0F3F8"/>
library(tidyverse)
# Helper functions --------------------------------------------------------
process_sp <- function(sp) {
message("Processing species: ", sp, "...")
# Seuraava käskyjoukko tuottaa kolmiosaisen kuvaajan, jossa ylin kuvaaja kertoo paikallismäärien
# vaihtelun vuoden aikana ja keskimmäinen muuttajien vastaavan vaihtelun. Kuvaajissa x-akselilla on