Created
February 12, 2016 22:00
-
-
Save ergo70/d04931951e8f853a59af to your computer and use it in GitHub Desktop.
Multicorn soilgrids_fdw
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
| from multicorn import ForeignDataWrapper | |
| import json | |
| import urllib2 | |
| class SoilGridsForeignDataWrapper(ForeignDataWrapper): | |
| def __init__(self, options, columns): | |
| super(SoilGridsForeignDataWrapper, self).__init__(options, columns) | |
| def execute(self, quals, columns): | |
| row = {} | |
| lat_ok = False | |
| long_ok = False | |
| if quals: | |
| for qual in quals: | |
| if qual.field_name.lower() == 'latitude' and qual.operator == '=': | |
| latitude = qual.value | |
| lat_ok = True | |
| if qual.field_name.lower() == 'longitude' and qual.operator == '=': | |
| longitude = qual.value | |
| long_ok = True | |
| if lat_ok and long_ok: | |
| req = urllib2.Request( | |
| 'http://rest.soilgrids.org/query?lon={0}&lat={1}&attributes=PHIHOX&confidence=L,M,U&depths=sd1'.format(longitude, latitude)) | |
| req.add_header('Content-Type', 'application/json;charset=UTF-8') | |
| resp = urllib2.urlopen(req) | |
| results = resp.read() | |
| results = json.loads(results) | |
| properties = results['properties'] | |
| row['longitude'] = longitude | |
| row['latitude'] = latitude | |
| row['ph_l'] = properties['PHIHOX']['L']['sd1'] / 10.0 | |
| row['ph_m'] = properties['PHIHOX']['M']['sd1'] / 10.0 | |
| row['ph_u'] = properties['PHIHOX']['U']['sd1'] / 10.0 | |
| row['depth'] = -0.025 | |
| row['publication_date'] = properties["publication_date"].encode("utf-8") | |
| yield row |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment