Skip to content

Instantly share code, notes, and snippets.

#####################################################################################
# Title: Nigeria Urban Mask
# Date: Sep 2015
# Project: HarvestChoice
# Author: Bacou, Melanie <mel@mbacou.com>
#####################################################################################
library(raster)
library(tmap)
setwd("~/Projects/hc-cell5m")
# Example #1 (takes 2 calls to request a download file)
# Returns wheat harvested area by district in Ghana in STATA format
$ curl http://hcapi.harvestchoice.org/ocpu/library/hcapi3/R/hcapi3 -d "{var: 'whea_h', iso3: 'GHA', by: 'ADM2_NAME_ALT', format: 'dta'}" -X POST
/ocpu/tmp/x0a3355977c/R/.val
/ocpu/tmp/x0a3355977c/messages
/ocpu/tmp/x0a3355977c/stdout
/ocpu/tmp/x0a3355977c/source
/ocpu/tmp/x0a3355977c/console
/ocpu/tmp/x0a3355977c/info
/ocpu/tmp/x0a3355977c/files/DESCRIPTION
## 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
},
@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
@mbacou
mbacou / Rperf.sh
Last active January 2, 2017 15:45
Benchmarking FastRWeb and OpenCPU
## Comparing execution times between OpenCPU (RApache) and FastRWeb (Rserve and HTTP).
## Same requests and same code. Note that FastRWeb is up to 13 times faster on the 2nd call.
# OpenCPU: summarize 2 layers across districts for the whole of SSA, return json of summary table
# and then json of all pixel values for mapping
$ time curl http://127.0.0.1/ocpu/library/hcapi3/R/getLayer/json \
-d '{"var" : ["whea_h", "AEZ16_CLAS"], "by" : "ADM2_NAME_ALT"}' -X POST -H "Content-Type:application/json"
[...]
{
@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 / 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 / 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 / ethLivelihoodZones.R
Created January 17, 2014 03:12
R: More complex ggmap examples
# Using ggmap to make more complex maps with inset, etc..
ethMap <- qmap("Ethiopia", zoom=6, color="color", source="google", maptype="terrain",
extent="panel", padding=0.02)
addisMap <- qmap("Addis Ababa", zoom=12, color="color", source="google", maptype="terrain",
extent="panel", padding=0)
layer.dhs <- geom_point(aes(x=lon, y=lat), data=loc.eth@data,
size=1.5, colour="blue", fill="blue", shape=2)
@mbacou
mbacou / 2013-Logbook.org
Created January 17, 2014 03:07
R: Use sp2tmap() with ggmap

tza.stata <- sp2tmap(tza) names(tza.stata) <- c(“ID”, “X”, “Y”)

tza.stata <- data.table(tza.stata) tza@data <- data.table(tza@data) tza@data[, rn := 1:dim(tza@data)[1]] setkey(tza.stata, ID) setkey(tza@data, rn) tza.stata$ADM2_CODE <- tza.stata[tza@data][, ADM2_CODE]