Skip to content

Instantly share code, notes, and snippets.

@ergo70
Created March 9, 2016 20:27
Show Gist options
  • Select an option

  • Save ergo70/dab1bac308e4b0f0efee to your computer and use it in GitHub Desktop.

Select an option

Save ergo70/dab1bac308e4b0f0efee to your computer and use it in GitHub Desktop.
Multicorn soilgrids_fdw_json
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