-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_wells_gpkg.py
127 lines (96 loc) · 3.77 KB
/
generate_wells_gpkg.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# ===============================================================================
# Copyright 2020 ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
from pprint import pprint, pformat
from geopandas import GeoDataFrame
import requests
from geojson import Feature, Point, FeatureCollection
from topo import get_state, get_huc8, get_place, get_county
def rget(url, callback=None, recursive=True):
items = []
def _get(u):
print('url={}'.format(u))
resp = requests.get(u)
print('resp={}'.format(resp))
j = resp.json()
if callback:
callback(items, j)
else:
try:
items.extend(j['value'])
except KeyError:
items.append(j)
try:
next = j['@iot.nextLink']
except KeyError:
return
if recursive:
_get(next)
_get(url)
return items
GEOCONNEX = 'https://geoconnex.us'
def nmbgmr_uri_factory(name):
return '{}/nmwdi/nmbgmr/wells/{}'.format(GEOCONNEX, name)
def ose_uri_factory(name):
return '{}/nmwdi/ose/wells/{}'.format(GEOCONNEX, name)
def nmbgmr_props_factory(loc, thing):
props = thing['properties']
# props['id'] = loc['name']
props['id'] = thing['name']
props['agency_id'] = thing['name']
props.pop('@nmbgmr.point_id', None)
try:
wd = float(props.pop('welldepth', 0))
except BaseException:
wd = 0
props['welldepth'] = float(wd)
return props
def ose_props_factory(loc, thing):
props = thing['properties']
# props['id'] = loc['name']
props['id'] = thing['name']
props['agency_id'] = thing['name']
return props
def get_geojson_features(url, factory, uri_factory):
def feature_factory(i, loc, thing):
props = factory(loc, thing)
props['sta'] = '{}?$expand=Datastreams/Observations'.format(thing['@iot.selfLink'])
props['state'] = get_state(loc)
props['huc8'] = get_huc8(loc)
props['place'] = get_place(loc)
props['county'] = get_county(loc)
props['uri'] = uri_factory(thing['name'])
print('construct:{:05n} {} properties={}'.format(i, thing['name'],
pformat(props)))
return Feature(properties=props,
geometry=Point(loc['location']['coordinates']))
items = rget(url, recursive=True)
return FeatureCollection([feature_factory(i, loc, thing) for i, loc in enumerate(items) for thing in loc['Things']])
def write_gpkg(fc, name='nmbgmr_wells'):
# convert obj to a GeoDataFrame
gdf = GeoDataFrame.from_features(fc['features'])
# write to gpkg
gdf.to_file('{}.gpkg'.format(name), driver='GPKG')
def main():
# write nmbgmr wells
url = 'https://st.newmexicowaterdata.org/FROST-Server/v1.1/Locations?$expand=Things'
fs = get_geojson_features(url, nmbgmr_props_factory, nmbgmr_uri_factory)
write_gpkg(fs)
url = 'https://ose.newmexicowaterdata.org/FROST-Server/v1.1/Locations?$expand=Things'
fs = get_geojson_features(url, ose_props_factory, ose_uri_factory)
write_gpkg(fs, 'ose_wells')
if __name__ == '__main__':
main()
# ============= EOF =============================================