The following python example will read all the location info for a given filterId and write it to a locations.csv and attributes.csv. The script has been tested using python 3.7 and requires the requests, csv and json modules:
import requests import json import csv url = 'http://localhost:8080/FewsWebServices/rest/fewspiservice/v1/locations?showAttributes=true' params = dict( documentVersion='1.24', documentFormat='PI_JSON' ) response = requests.get(url=url, params=params) data = response.json() # alle data in JSON (dictionary) formaat locations = data['locations'] # haalt alle parameters uit de dictionary # collect all possibile location keys in a list location_keys_list = [] for location in locations: keys = location.keys() for key in keys: if key not in location_keys_list: location_keys_list.append(key) # remove 'attributes' element as these will be handled seperately if 'attributes' in location_keys_list: location_keys_list.remove('attributes') # collect all possibile attributes in a list attribute_list = [] for location in locations: for attribute in location['attributes']: attr_id = str(attribute.get('id')) if attr_id not in attribute_list: attribute_list.append(attr_id) # insert locationId as the first element in the attribute list attribute_list.insert(0,'locationId') with open('locations.csv', 'w', newline='', encoding='utf-8') as loc_csv: l = csv.writer(loc_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) l.writerow(location_keys_list) for location in locations: temp_dict = {} for key in location_keys_list: if key in location: temp_dict[key] = str(location[key]) else: temp_dict[key] = '' loc_writerow = [temp_dict[k] for k in location_keys_list] l.writerow(loc_writerow) with open('attributes.csv', 'w', newline='', encoding='utf-8') as attr_csv: a = csv.writer(attr_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) a.writerow(attribute_list) for location in locations: temp_dict = {} for attribute in location['attributes']: for attr in attribute_list: if str(attribute.get('id')) == attr: if attribute.get('type') == 'text': temp_dict[attr] = str(attribute.get('value')) if attribute.get('type') == 'number': temp_dict[attr] = float(attribute.get('value')) if attribute.get('type') == 'boolean': temp_dict[attr] = bool(attribute.get('value')) for attr in attribute_list: if not attr in temp_dict.keys(): temp_dict[attr] = '' temp_dict['locationId'] = str(location['locationId']) attr_writerow = [temp_dict[a] for a in attribute_list] a.writerow(attr_writerow) print('Finished!')
The PI_JSON response can for example look like this:
{ "version": "1.25", "geoDatum": "WGS 1984", "locations": [{ "locationId": "locX", "shortName": "Location X", "lat": "54.63", "lon": "-113.38", "x": "-113.38", "y": "54.63", "z": "0.0", "attributes": [{ "name": "operating_agency", "type": "text", "id": "operating_agency", "value": "GovAA" }, { "name": "meteo_station", "type": "boolean", "id": "meteo_station", "value": "TRUE" }, { "name": "test_ext_id", "type": "text", "id": "test_ext_id", "value": "CPAF" }] ...more locations... }] }
To obtain all the metadata for all locations and their attributes, the locations element in the JSON response will need to be parsed. Running the python script will produce two csv files: locations.csv and attributes.csv.
Note that the parsing of the main location information interprets all elements as the 'type' text. Part of an example output is presented below.
locations.csv
locationId | shortName | lat | lon | x | y | z |
locA | Location A | 56.677 | -93.641 | -93.641 | 56.677 | 0 |
locB | Location B | 49.861 | -99.962 | -99.962 | 49.861 | 0 |
locC | Location C | 51.565 | -101.917 | -101.917 | 51.565 | 0 |
locD | Location D | 49.868 | -97.405 | -97.405 | 49.868 | 0 |
Note that the parsing of the attributes takes the 'type' (text, number, boolean) into account
attributes.csv
locationId | operating_agency | province | hydro_station | Q_softmax | Q_hardmax | Q_softmin |
locA | Agency X | AA | Y | 80 | 1000 | 0.5 |
locB | Agency Y | BB | Y | |||
locC | Agency X | AA | Y | |||
locD | Agency X | MM | Y |