Database Reference
In-Depth Information
postgis_cookbook=# CREATE EXTENSION
plpythonu;
How to do it...
Carry out the following steps:
1. In this recipe, as with a previous one, you will use an openweathermap.org
Web service to get the temperature for a point from the closest weather
station. The request you need to run (test it in a browser) is ht-
tp://api.openweathermap.org/data/2.1/find/sta-
tion?long=100.49&lat=13.74&cnt=1 . YoushouldgetthefollowingJSONout-
put(theclosestweatherstation'sdatafromwhichyouwillreadthetemperat-
ure to the point, with the coordinates of the given longitude and latitude):
{message: "",cod: "200",calctime: "",cnt:
1,list: [{id: 9191,dt: 1369343192,name:
"100704-1",type: 2,coord: {lat: 13.7408,
lon: 100.5478},distance:
6.244,main: {temp: 300.37},wind: {speed:
0,deg: 141},rang: 30,rain: {1h: 0,24h:
3.302,today: 0}}]
}
2. CreatethefollowingPostgreSQLfunctioninPythonusingthePL/Pythonlan-
guage:
CREATE OR REPLACE FUNCTION
chp08.GetWeather(lon float, lat
float)RETURNS floatAS $$import
urllib2import simplejson as jsondata =
urllib2.urlopen('http://api.openweathermap.org/
data/2.1/find/
station?lat=%s&lon=%s&cnt=1'% (lat,
lon))js_data = json.load(data)if
js_data['cod'] == '200': # only if cod is
200 we got some effective resultsif
int(js_data['cnt'])>0: # check if we have
Search WWH ::




Custom Search