Skip to content

Instantly share code, notes, and snippets.

View Robinlovelace's full-sized avatar
💭
Traffic modelling

Robin Lovelace Robinlovelace

💭
Traffic modelling
View GitHub Profile
@Robinlovelace
Robinlovelace / install-ncl.R
Created May 11, 2015 16:00
Install packages from NCL course
install.packages("drat")
drat::addRepo("rcourses")
install.packages("nclRadvanced", type="source")
@Robinlovelace
Robinlovelace / R-demo.R
Last active August 29, 2015 14:21
Demo of R tutorial
x <- 1:99
y = x^2 / exp(x)
plot(x, y)
pkgs <- c("downloader", "readxl")
install.packages(pkgs)
lapply(pkgs, library, character.only = T)
dir.create("big-data")
download("http://tinyurl.com/r-for-bd-8",
@Robinlovelace
Robinlovelace / tresis-households.R
Created September 4, 2015 20:12
Code chunk that is part of the TRESIS approach to modelling transport behaviour for the book 'Spatial microsimulation with R'
# Households with a car and no professionals
synhhlds <- c(synhhlds,
sample(
filter(hhlds,
numprof == 0,
numcars > 0
)$hhld,
popdist['Car','Other'])
)
@Robinlovelace
Robinlovelace / mapshaper.R
Created January 7, 2016 16:58
Demonstrates the use of mapshaper called from R to simplify spatial objects: https://github.com/ropensci/stplanr
# # # # # # # # # # # # # # # # # # # #
# Simplify sp objects w. mapshaper #
# Needs https://nodejs.org installed #
# Tested on Windows and Linux #
# # # # # # # # # # # # # # # # # # # #
# # # # # #
# R setup #
# # # # # #
@Robinlovelace
Robinlovelace / inverse-spatial-subset.R
Created April 27, 2016 07:20
Generate the inverse of a spatial subset
# Aim: illustrate how to invert a spatial subset
# load the sp library and data
library(sp)
data("meuse")
coordinates(meuse) = ~x+y
data("meuse.riv")
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
# plot the basics
download.file("https://www.geofabrik.de/data/shapefiles_toulouse.zip", "shapefiles_toulouse.zip")
unzip("shapefiles_toulouse.zip")
system.time({
shp = rgdal::readOGR(dsn = ".", layer = "gis.osm_buildings_v06")
})
f = list.files(pattern = "gis.osm")
file.remove(f)
file.remove("shapefiles_toulouse.zip")
@Robinlovelace
Robinlovelace / magick-image_average.R
Created September 22, 2016 13:42
Quick demo of averaging two images with the magick R package
library(magick)
library(magrittr)
# create raster layers
oldlogo <- image_read("https://developer.r-project.org/Logo/Rlogo-2.png")
newlogo <- image_read("https://www.r-project.org/logo/Rlogo.png")
oldlogo_blue = image_colorize(image = oldlogo, opacity = 99, color = "blue") %>%
image_blur(radius = 10, sigma = 10)
newlogo_red = image_colorize(image = newlogo, opacity = 99, color = "red") %>%
image_blur(radius = 10, sigma = 10) %>%
image_rotate(degrees = 90)
@Robinlovelace
Robinlovelace / nearest-side.R
Created February 23, 2017 10:40
Find the box edge a point is nearest to
library(stplanr)
d = SpatialPoints(coords = matrix(rnorm(100), ncol = 2))
b = bb2poly(bb = d)
p = raster::geom(b)
for(i in 1:4){
if(i == 1)
l = raster::spLines(rbind(p[i, c("x", "y")], p[i + 1, c("x", "y")])) else
l = raster::bind(
l,
raster::spLines(rbind(p[i, c("x", "y")], p[i + 1, c("x", "y")]))
@Robinlovelace
Robinlovelace / geocompr-reproduce.R
Created May 18, 2017 10:03
Reproduce Geocomputation with R book
# Aim: reproduce Geocomputation with R book
if(!require(devtools)) {
install.packages("devtools")
}
devtools::install_github("robinlovelace/geocompr")
download.file("https://github.com/Robinlovelace/geocompr/archive/master.zip", "master.zip")
unzip("master.zip")
old_dir = setwd("geocompr-master/")
bookdown::render_book("index.Rmd") # to build the book
browseURL("_book/index.html") # to view it
@Robinlovelace
Robinlovelace / test.R
Created May 19, 2017 10:43
Correlation example
x = 1:9
y = x^2
cor(x, y)