forked from virgesmith/UKCensusAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoquery.py
executable file
·43 lines (36 loc) · 1.61 KB
/
geoquery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import ukcensusapi.Nomisweb as Api
def main():
api = Api.Nomisweb("/tmp/UKCensusAPI")
print("Nomisweb census data geographical query example")
print("See README.md for details on how to use this package")
# In the previous example we had a predefined query using Leeds at MSOA resolution,
# but we want to expand the geographical area and refine the resolution
table = "KS401EW"
query_params = {}
query_params["CELL"] = "7...13"
query_params["date"] = "latest"
query_params["RURAL_URBAN"] = "0"
query_params["select"] = "GEOGRAPHY_CODE,CELL,OBS_VALUE"
query_params["geography"] = "1245710558...1245710660,1245714998...1245714998,1245715007...1245715007,1245715021...1245715022"
query_params["MEASURES"] = "20100"
# Define the new coverage area in terms of local authorities
coverage = ["Leeds", "Bradford"]
# Define the new resolution
resolution = Api.Nomisweb.GeoCodeLookup["OA11"]
# Convert the coverage area into nomis codes
coverage_codes = api.get_lad_codes(coverage)
# replace the geography value in the query
query_params["geography"] = api.get_geo_codes(coverage_codes, resolution)
# get the data
ks401fine = api.get_data(table, query_params)
print(ks401fine.head(5))
# Now widen the coverage to England & Wales and coarsen the resolution to LA
coverage_codes = [Api.Nomisweb.GeoCodeLookup["EnglandWales"]]
resolution = Api.Nomisweb.GeoCodeLookup["LAD"]
query_params["geography"] = api.get_geo_codes(coverage_codes, resolution)
# get the data
ks401broad = api.get_data(table, query_params)
print(ks401broad.head(5))
if __name__ == "__main__":
main()