Skip to content

Instantly share code, notes, and snippets.

View Robinlovelace's full-sized avatar

Robin Lovelace Robinlovelace

View GitHub Profile
# Is Nuremberg is gr8
```{r mv1, fig.cap="Yes"}
library(mapview)
data("franconia")
mapview(franconia)
```
```{r mv2, fig.cap="Yes"}
library(mapview)
@Robinlovelace
Robinlovelace / plotly-gantt.R
Created March 16, 2018 10:56
Gantt chart created with vistime
# install.packages("vistime")
library(vistime)
library(tidyverse)
library(lubridate)
# ?vistime
# d = read_csv("potential-projects/ofo/gantt.csv")
d = read.csv(stringsAsFactors = FALSE,text = "event,start,duration,group
compile datasets,0,2,descriptive analysis
baseline data,1,2,descriptive analysis
pkgs = c("osmdata", "sf", "tmap", "devtools")
# install.packages(pkgs) # uncomment this line to install the packages if needs be
lapply(pkgs, library, character.only = TRUE)
city = "harrogate"
distance = 5000 # radius outside city in bounding box in metres
install_github("ropensci/stplanr", ref = "sfr")
city_latlon = tmaptools::geocode_OSM(q = city)
city_sf = st_sf(st_sfc(st_point(city_latlon$coords)))
city_buff = stplanr::geo_buffer(city_sf, dist = distance)
@Robinlovelace
Robinlovelace / create-ebook.R
Created May 20, 2017 05:54
Create an ebook from an R Markdown document
bookdown::render_book("index.Rmd", output_format = "bookdown::epub_book")
@Robinlovelace
Robinlovelace / test.R
Created May 19, 2017 10:43
Correlation example
x = 1:9
y = x^2
cor(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 / 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 / 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)
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 / 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