-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
73 lines (55 loc) · 2.39 KB
/
run.py
File metadata and controls
73 lines (55 loc) · 2.39 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import requests, configparser, json, io, zipfile
import geopandas as gpd
from os import path
from functions import process
def read_config():
"""
Read in config file with API url, username and password
"""
cfg = configparser.ConfigParser()
cfg.read('api_config.ini')
return {'url': cfg['API']['url'],
'username': cfg['API']['username'],
'password': cfg['API']['password']}
def get_data(area_scale='oa', area_code='E00042673', zip=False, file_name='data.geojson', output_dir='./'):
"""
Pull data from NISMOD-DB API and save as geojson file.
"""
# fetch login details for API call
api_details = read_config()
# pre-set classification codes for MasterMap polygons
classification_codes = '10123, 10172, 10183'
if not zip:
response = requests.get('%s/data/mastermap/areas?export_format=geojson&scale=%s&area_codes=%s&classification_codes=%s' %(api_details['url'], area_scale, area_code, classification_codes), auth=(api_details['username'], api_details['password']))
else:
response = requests.get('%s/data/mastermap/areas?export_format=geojson-zip&scale=%s&area_codes=%s&classification_codes=%s' % (api_details['url'], area_scale, area_code, classification_codes), auth=(api_details['username'], api_details['password']))
# if an error from API return to user
if response.status_code != 200:
print('API returned status code %s, failing in the process. Error: %s' %(response.status_code, response.text))
exit()
if zip:
z = zipfile.ZipFile(io.BytesIO(response.content))
z.extractall(path.join(output_dir, file_name))
else:
data = json.loads(response.text)
#print('Number of features:', len(data['features']))
with open(path.join(output_dir, file_name), 'w') as data_file:
json.dump(data, data_file)
# return file path to saved data
return path.join(output_dir, file_name)
def import_file(file_name):
"""
Convert a .geojson file, or similar, into a geo-dataframe
"""
df = gpd.read_file(file_name, encoding='UTF-8')
return df
def create_df(json_data):
"""
Convert raw geojson into a geo-dataframe
"""
return gpd.read_geojson(json_data)
##############################################
# fetch data and save to file
data_path = get_data()
# run path width calculations
df_1, df_2 = process(import_file(data_path))