Created
October 14, 2014 18:04
-
-
Save robinkraft/3a56b865557e5a49f62f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A mill with less than 5% UMD loss from 2005-2012 is low risk, 5-28% is medium risk, and over 28% is high risk. | |
# One within concessions and one within entire radius | |
# Use the area of the entire radius or concession as the denominator (effectively assume 100% forested) | |
import json | |
import requests # sudo pip install requests | |
APIURL = "http://wri-01.cartodb.com/api/v2/sql" | |
def parse_concession_json(j): | |
'''format json response for use with other functions''' | |
return | |
def get_concession_geoms(mill_id): | |
'''Make cartodb request to get concession info for a given mill. Parse response | |
into proper format for the rest of the data workflow. | |
SELECT mill_entity_id, palm_geom, palm_id FROM mills_concessions_50 mills | |
Sample URL: | |
http://wri-01.cartodb.com/api/v2/sql?api_key=<APIKEY>&q=SELECT mill_entity_id, palm_geom, palm_id FROM mills_concessions_50 mills | |
''' | |
query = 'SELECT mill_entity_id, palm_geom, palm_id FROM mills_concessions_50 mills' | |
r = requests.get(APIURL, params=dict(api_key='', q=query)) | |
concession_info = parse_concession_json(response.json()) | |
return concession_info | |
def gfw_api_request(lat, lon, radius=None, geom=None): | |
'''Make GFW API request for a given latlon and radius to calculate the | |
loss in a given area. Use the radius to generate a geometry suitable for GFW | |
API, or just supply a geometry.''' | |
return | |
def calc_loss(mill_id, lat, lon, radius): | |
'''For a single mill, get geometries for nearby concessions, then use GFW API to | |
calculate the UMD loss in each concession. Also gets the loss inside a given radius. | |
We may want to add up the loss if there are multiple concessions. | |
''' | |
geoms = get_concession_geoms(mill_id) | |
loss_radius = gfw_api_request(lat, lon, radius=radius) | |
loss_dict = {} | |
for geom, id in geoms.items(): | |
loss = gfw_api_request(lat, lon, geom) | |
loss_dict[id] = loss | |
# {10: 50, 11, 900} id: loss | |
return loss_dict, loss_radius | |
def get_mills(table): | |
'''Make request to CartoDB to get ids and latlons for all the mills | |
we're working with. | |
Something like this might work: SELECT id, lat, lon FROM mills_table''' | |
return | |
def update_mill_table(mill, loss_dict, loss_radius): | |
'''Update cartodb mills table with loss figures for concessions and the radius | |
around the mill.''' | |
return | |
def main(): | |
'''Gets a list of mills, loop through them to get loss statistics for their nearby | |
concessions and the hardcoded radius (likely 50km).''' | |
ids_latlons = get_mills(table) | |
for mill in ids_latlons: | |
loss_dict, loss_radius = calc_loss(mill['id'], mill['lat'], mill['lon'], radius) | |
update_mill_table(mill, loss_dict, loss_radius) | |
return loss_dict, loss_radius |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment