Created
March 9, 2016 20:27
-
-
Save ergo70/dab1bac308e4b0f0efee to your computer and use it in GitHub Desktop.
Multicorn soilgrids_fdw_json
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 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}'.format(longitude, latitude)) | |
| req.add_header('Content-Type', 'application/json;charset=UTF-8') | |
| resp = urllib2.urlopen(req) | |
| results = resp.read() | |
| row['longitude'] = longitude | |
| row['latitude'] = latitude | |
| row['response'] = results | |
| yield row |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment