Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The following python example will read all the location info for a given filterId PI_JSON locations webservice call 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:

Code Block
languagepy
import requests
import json
import csv

url = 'http://localhost:8080/FewsWebServices/rest/fewspiservice/v1/locations?showAttributes=true'
params = dict(
    documentVersion='1.2425',
    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!')

...

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. Part : 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

locationIdshortNamelatlonxyz
locALocation A56.677-93.641-93.64156.6770
locBLocation B49.861-99.962-99.96249.8610
locCLocation C51.565-101.917-101.91751.5650
locDLocation D49.868-97.405-97.40549.8680

...

Note that the parsing of the main location information interprets all elements as attributes takes the 'type' text. (text, number, boolean) into account

attributes.csv

locationIdoperating_agencyprovincehydro_stationQ_softmaxQ_hardmaxQ_softmin
locAAgency X

AA

Y8010000.5
locBAgency YBBY


locCAgency XAAY


locDAgency XMMY

...