1

Using Python I have loaded json data from the url http://environment.data.gov.uk/flood-monitoring/id/floods and have found that the flood areas encoded as geoJSON polygons are displayed as url links.

What code do I use to map these polygons in python?

Here is additional information on the API and layers: http://environment.data.gov.uk/flood-monitoring/doc/reference#api-structure

  • What sort of map do you want? – Ian Turton Feb 17 '16 at 16:36
  • Just a plain basemap for now, of the UK – cstone Feb 17 '16 at 18:22
0

Examining the first feature of the json data file:

import requests
r = requests.get('http://environment.data.gov.uk/flood-monitoring/id/floods')
data = r.json()
# the result is directly a dictionary and if we examine the keys
data.keys()
[u'@context', u'meta', u'items']
# look at the first item
data['items'][0]
{u'severityLevel': 3, u'isTidal': False, u'description': u'Upper Bristol Avon Area', u'timeSeverityChanged': u'2016-02-05T17:04:00', u'eaRegionName': u'South West', u'floodAreaID': u'112WAFTUBA', u'timeMessageChanged': u'2016-02-17T09:48:00', u'floodArea': {u'county': u'Wiltshire', u'riverOrSea': u'Bristol River Avon', u'@id': u'http://environment.data.gov.uk/flood-monitoring/id/floodAreas/112WAFTUBA', u'polygon': u'http://environment.data.gov.uk/flood-monitoring/id/floodAreas/112WAFTUBA/polygon', u'notation': u'112WAFTUBA'}, u'timeRaised': u'2016-02-17T09:48:00', u'message': u'Up to 20 mm of rain is forecast to fall on Wednesday 17th February which is expected to raise river levels as rain falls on saturated ground. River levels on the Tetbury Avon at Brokenborough, Sherston Avon at Fosseway, River Avon at Great Somerford and Dauntsey Brook at Dauntsey are currently stable but should rise in response to Wednesday\u2019s rain. We will continue to monitor the situation and update this message as the situation changes.', u'@id': u'http://environment.data.gov.uk/flood-monitoring/id/floods/95882', u'eaAreaName': u'North Wessex', u'severity': u'Flood Alert'}

We can see that the geometry is an url (u'polygon': u'http://environment.data.gov.uk/flood-monitoring/id/floodAreas/112WAFTUBA/polygon')

print data['items'][0]['floodArea']['polygon']
http://environment.data.gov.uk/flood-monitoring/id/floodAreas/112WAFTUBA/polygon
# name of the river
print data['items'][0]['floodArea']['riverOrSea']
Bristol River Avon
# County
print data['items'][0]['floodArea']['county']
Wiltshire

Therefore, the geometry

r2 = requests.get(data['items'][0]['floodArea']['polygon'])
geom = r2.json()
print geom.keys()
[u'crs', u'type', u'features']
print geom['type']
FeatureCollection
print geom['crs']
{u'type': u'name', u'properties': {u'name': u'urn:ogc:def:crs:OGC:1.3:CRS84'}}

But this is not a valid GeoJSON because the features are a list of dictionaries and not a dictionary of dictionaries (= first element of the list)

print geom['features'][0]['properties']
{u'FWIS_CODE': u'112WAFTUBA', u'W_DESCRIP': None, u'W_FWA_NAME': None, u'AREA': u'North Wessex', u'REGION': u'South West Region', u'W_AFON': None, u'COUNTY': u'Wiltshire', u'FWD_TACODE': u'112WAFTUBA', u'E_QDIAL': u'04423', u'W_REGION': u'DG Lloegr', u'DESCRIP': u'Upper River Avon and tributaries including Malmesbury and Chippenham', u'RIVER_SEA': u'Bristol River Avon', u'FWA_NAME': u'Upper Bristol Avon Area', u'W_QDIAL': None}
# and 
print geom['features'][0]['geometry']['type']
MultiPolygon
# GeoJSON geometry =  geom['features'][0]['geometry'] (very long)

Result (in red)

enter image description here

Apply the same procedure for all other items. In place of request, you can use urllib, urllib2 or others...

  • That's fantastic thank you! I am however still struggling to map the flood. I had a look at gis.stackexchange.com/questions/93136/… but the data.keys() were different to the ones here, so that didn't work. Yours looks great though, how did you achieve that? – cstone Feb 18 '16 at 10:23
  • Simply by examining the first json file and searching for geometry. – gene Feb 18 '16 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.