Skip to content

Instantly share code, notes, and snippets.

View barryrowlingson's full-sized avatar

Barry Rowlingson barryrowlingson

View GitHub Profile
@barryrowlingson
barryrowlingson / tach.R
Created June 30, 2012 21:37
Simple mechanism for attaching functions in a single .R file
###
### tach (c) Barry Rowlingson, 2012, pronounced like "tash"
###
### tach("file.R") will source that file into an environment and
### put it on the search path, thus including any function
### definitions without polluting your GlobalEnv
###
### retach() will scan your search path for any tach'ed files and
### if they have been modified since they were loaded they'll be
### re-tached.
@barryrowlingson
barryrowlingson / gist:3207893
Created July 30, 2012 15:39
Can you fit a rectangle in a polygon?
require(rgeos)
require(sp)
makeRect <- function(origin, l1, l2, angle){
sa = sin(angle)
ca = cos(angle)
x=origin[1] + c(0,l1*ca,l1*ca-l2*sa,-l2*sa,0)
y=origin[2] + c(0,l1*sa,l1*sa+l2*ca,l2*ca,0)
return(SpatialPolygons(list(Polygons(list(Polygon(cbind(x,y))),"Rectangle"))))
return(cbind(origin[1]+x,origin[2]+y))
@barryrowlingson
barryrowlingson / response times by language.sql
Created October 31, 2012 14:11 — forked from guilespi/response times by language.sql
StackOverflow response times by stats packages (and java)
select TagName,
avg(cast(ResponseTime as bigint)) as Average,
stdev(cast(ResponseTime as bigint)) as StandardDev
from
(SELECT
Questions.CreationDate,
Questions.Title,
Tags.TagName,
Answers.CreationDate as ResponseDate,
datediff(minute, Questions.CreationDate,
# Author: Joona Lehtomäki <[email protected]>
# Updated: 13.11.2011
# Version: 0.0.1
if (!require("rgdal")) {
install.packages("rgdal")
}
if (!require("raster")) {
install.packages("raster")
@barryrowlingson
barryrowlingson / dexys.R
Created May 19, 2014 16:32
Hiding mechanics from R plotting for dexy
# You want to show users the code to make a plot.
# BUT you dont want to show them the png() function call you used to make the plot
# for your documentation.
#
# dexy doesn't. You have to open/close the graphics device in your code:
### @export "graph"
png("plot.png")
plot(c(1,2,3), pch=19, col="purple")
dev.off()
@barryrowlingson
barryrowlingson / abuff.R
Created May 28, 2014 12:37
buffer feature to a fixed percentage area increase
abuff <- function(f, percent, precision, maxsteps = 20){
require(rgeos)
A0 = gArea(f)
targetA = A0 + A0 * (percent/100)
e = bbox(f)
w0 = 0
w2 = diff(e[1,])/10 # initial w
## initial expansion until we pass the solution:
###
### superclassing is nice
###
require(dplyr)
require(testthat)
set.seed(310366)
classes = c("thingy","data.frame")
@barryrowlingson
barryrowlingson / loadutils.R
Created August 20, 2014 15:45
Work with .RData files
# load an object 'name' from a .RData file 'file'.
# eg
# x1 = loadFrom("foo.RData","x")
# x2 = loadFrom("bar.RData","x")
#
# useful when you want to load from two or more files with objects with the same name and
# load(file) would stomp on the second one.
loadFrom=function(file, name){e=new.env();load(file,env=e);e[[name]]}
@barryrowlingson
barryrowlingson / dplyrclass.R
Created August 20, 2014 17:00
dplyr doesn't preserve data frame classes
## make a data frame with a new class
d=data.frame(x=1:10,y=rnorm(10))
class(d)=c("foo","data.frame")
class(d)
## [1] "foo" "data.frame"
## ordinary subsetting and subset preserve us:
##
@barryrowlingson
barryrowlingson / GlobalLandCover.xml
Created October 1, 2014 10:51
Global Land Cover WMS XML Spec for QGIS
<!DOCTYPE connections>
<qgsWMSConnections version="1.0">
<wms ignoreGetMapURI="true" smoothPixmapTransform="false" dpiMode="7" password="" ignoreGetFeatureInfoURI="false" referer="" username="" url="http
://218.244.250.80:8080/erdas-apollo/coverage/CGLC" invertAxisOrientation="false" ignoreAxisOrientation="false" name="China Land Cover"/>
</qgsWMSConnections>