Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
@geotheory
geotheory / kml_to_geojson.R
Last active October 10, 2021 01:36
KML to GeoJSON conversion in R
#########################################################
##### Script to load in KML and output to GeoJSON #####
##### Robin Edwards, geotheory.co.uk, @geotheory #####
#########################################################
# This script accepts a simple KML file (e.g. from Google Earth or Scribblemaps.com) and converts to GeoJSON.
# It will not accept KMLs with multiple geometries - they must be entirely of points, paths or polygons,
# or a primitive geometry (such as rectangle or circle)
#
# How to run:
@geotheory
geotheory / HiveR_demo.R
Created November 25, 2013 09:27
A HiveR package demo using the ggplot2 diamonds dataset
require(HiveR)
require(plyr)
require(colorspace)
require(classInt)
d = ggplot2::diamonds
d = d[,c(1:4,7)]
head(d); dim(d)
# separate carat-size data into equal interval groups
@geotheory
geotheory / urban_gender_analysis.R
Last active August 29, 2015 14:02
This script demo's how to process the 2011 UK Census shapefiles and data to produce graphics used in http://geotheory.co.uk/blog/2014/06/23/gender-in-urban-workplaces/
require(maptools)
require(fields)
require(ggplot2)
require(stringr)
bng = "+init=epsg:27700"
workdir = '[change to working directory path]'
setwd(workdir)
R.utils::mkdirs('wz')
@geotheory
geotheory / bng_to_latlons.R
Created June 28, 2014 16:01
Script to convert csv of British National Grid coordinates (OSBG36) to lat-longs (WGS84)
# read in csv file with headings labelled 'e' and 'n' and output latlons
# call script by command-line: 'RScript bng_to_latlon.R {bng_filename.csv}'
require(rgdal)
args=(commandArgs(TRUE))
datain = read.csv(args[1])
bng = datain
coordinates(bng) = ~ e + n
bng@proj4string = CRS("+init=epsg:27700")
latlon = spTransform(bng, CRS("+proj=longlat +datum=WGS84"))
@geotheory
geotheory / ArrayList.cpp
Last active December 6, 2019 08:53
ArrayList for Arduino, based on code by obedrios but split to working files and updated to include get_item() method. WARNING: code has a memory leak problem that needs addressing - see http://goo.gl/eAWT4R
/*
* Arduino Class for ArrayList
* Written: Obed Isai Rios
*/
#include "Arduino.h"
#include "ArrayList.h"
ArrayList::ArrayList(char* init){
@geotheory
geotheory / server.R
Created September 5, 2014 16:57
Basic Shiny app with Leaflet & ggplot
library(shiny)
library(rCharts)
library(ggplot2)
data(uspop2000, package = 'leaflet')
base_df <- head(uspop2000[which(uspop2000$State == "AL"), ])
shinyServer(function(input, output){
output$myChart <- renderMap({
@geotheory
geotheory / ggplot_quantile_colours.R
Last active October 26, 2021 13:46
Elegant code for quantile colour mapping in ggplot2, and a less elegant function to make pretty legend labels
require(ggplot2)
require(stringr)
d = as.vector(round(abs(10 * sapply(1:4, function(n)rnorm(20, mean=n, sd=.6)))))
ggplot(data=NULL, aes(x=1:length(d), y=d, col=cut(d,quantile(d)))) +
geom_point(size=5) + scale_colour_manual(values=rainbow(5))
quantile_labels = function(d, qtls, units, round=2, minzero=F, maxplus=F, space=T){
labs = levels(cut(d, qtls,include.lowest=T))
@geotheory
geotheory / alluvial.R
Last active April 2, 2016 02:15
A function to make alluvial-style plots of simple categorical time-series data
require(grid)
require(scales)
require(reshape2)
# Notes:
# dat should be a 3 column data.frame with fields in order as per example below
# wave wavyness of curves defined in terms of x axis data range - experiment to get this right
# ygap gap between items on each y axis
# col a single colour or vector of colours for categories when listed in alphabetical order
# leg.mode if true legend plotted in largest data observation, otherwise custom coordinates (leg.x/y [0,1])
@geotheory
geotheory / alluvial_ts_example.R
Last active August 29, 2015 14:10
Alluvial_ts example
require(alluvial)
require(reshape2)
d <- read.table(text="Class,1991,2003,2005,2009,2011,2013
1,818,604,601,570,563,556
2,183,147,145,143,142,150
3,40,55,55,55,55,50
4,48,70,81,76,85,99
5,126,140,142,148,155,153
6,396,566,568,566,525,508
@geotheory
geotheory / server.R
Created December 2, 2014 17:12
Shiny PDF output problem
require(shiny)
library(ggplot2)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})