Skip to content

Instantly share code, notes, and snippets.

@mbacou
mbacou / CRP11_ActionSites.geojson
Created March 19, 2014 18:02 — forked from anonymous/CRP11_ActionSites.geojson
Layer exported from QGIS
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbacou
mbacou / CRP11_ActionSites.geojson
Created March 19, 2014 18:05 — forked from anonymous/CRP11_ActionSites.geojson
Layer exported from QGIS
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbacou
mbacou / snippets.R
Last active August 29, 2015 14:01
Very simply get admin boundaries from GADM
# Voila
require(raster)
can <- getData("GADM", country="CAN", level=0)
# And then simplify boundaries for plotting
can <- gSimplify(can, tol=0.01, topologyPreserve=TRUE)
# Plot raster with full control
par(oma=c(0,0,0,4))
plot(r, col=cc, xlim=xlim, ylim=ylim, main=NULL, legend.width=1.2,
@mbacou
mbacou / Rperf.sh
Last active August 29, 2015 14:09
Benchmarking OpenCPU with Rserve socket
## Improved timings of OpenCPU stack using persistent Rserve session
# Summary by district
$ time curl http://127.0.0.1/ocpu/library/hcapi3/R/getLayer \
-d '{"var" : ["whea_h", "AEZ16_CLAS"], "by" : "ADM2_NAME_ALT"}' \
-X POST -H "Content-Type:application/json"
/ocpu/tmp/x010a92e97a/R/.val
/ocpu/tmp/x010a92e97a/source
/ocpu/tmp/x010a92e97a/console
## Spatial Targeting Tool
# Summarize wheat area and household wealth index over AEZ class "Tropic - warm / semiarid",
# pop density between 60-100 pp/sq.km. and low market access (under 5hrs)
$ time curl http://hcapi.harvestchoice.org/ocpu/library/hcapi3/R/reClassify/json -d '{"var" : ["whea_h", "wealth"], "by" : {"AEZ16_CLAS": "Tropic - warm / semiarid", "PD05_RUR" : [60, 100], "TT_20K" : [0, 5]}}' -X POST -H "Content-Type:application/json"
[
{
"TT_20K": "(0,5]",
"whea_h": 111336,
"wealth": 2
},
#####################################################################################
# Title: Nigeria Urban Mask
# Date: Sep 2015
# Project: HarvestChoice
# Author: Bacou, Melanie <mel@mbacou.com>
#####################################################################################
library(raster)
library(tmap)
setwd("~/Projects/hc-cell5m")
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:24
R: Find unmatched observations
# Which are the rows in dt1 that aren't in dt2.
which(is.na(dt2[dt1, which=TRUE, mult="first"]))
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:25
R: Read head/tail of large files
# Read head/tail of large files
readLines("./2010 BMGF/data/Segments/cell5m_d14.txt", n=10)
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:27
R: Export multiple graphs to PNG or SVG in one step
# Export multiple graphs to PNG or SVG at once
png("/out/YieldTarget_%d.png")
dotplot(cropSPAM~value,
plot[!plot$cropSPAM %in% c("BANP", "SUGC"),],
group=variable,
main=list("Mean GAEZ Low and High Input Yield Potentials \nagainst Mean SPAM Yield", cex=.9),
xlab="yield (kg/ha)",
pch=c(1,1,3),
cex=1.2,
col=c("green", "red", "orange"),
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:29
R: Draw random dot-density maps
# Random dot density maps
dots.rand <- dotsInPolys(gha, gha@data$total, f="random")
plot(dots.rand,
pch=19, cex=0.4, col="red",
add=TRUE)
text(coordinates(gha.reg),
labels=gha.reg@data$REGION,
adj=c(0, 0), cex=.9, col="black", xpd=NA)