-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
executable file
·685 lines (627 loc) · 26.6 KB
/
app.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import json
import requests
from flask import jsonify, request, make_response
from flask_migrate import Migrate
from dbms import app, db
from geoid_list.geoid_list import list_bp
from s2_service import S2Service
from utils import Utils
from dotenv import load_dotenv
from shapely.geometry import Point
from shapely.wkt import loads as load_wkt
from flask_wtf.csrf import generate_csrf
from localStoragePy import localStoragePy
load_dotenv()
localStorage = localStoragePy('asset-registry', 'text')
from dbms.models import geoIdsModel, s2CellTokensModel, cellsGeosMiddleModel
migrate = Migrate(app, db)
app.register_blueprint(list_bp) # url_prefix='/geoid-lists'
@app.route('/', methods=["GET"])
@Utils.fetch_token
def index(token, refresh_token):
"""
Endpoint to receive tokens from user-registry and return a response
"""
try:
to_return = {'access_token': token, 'refresh_token': refresh_token}
localStorage.setItem('access_token', token)
localStorage.setItem('refresh_token', refresh_token)
return jsonify(to_return)
except Exception as e:
return jsonify({
'message': 'Asset Registry Error',
'error': f'{e}'
}), 400
@app.route('/logout', methods=['GET'])
@Utils.token_required
def logout():
try:
refresh_token = Utils.get_bearer_token()
if not refresh_token:
return jsonify({
'message': 'Asset Registry Logout Error',
'error': 'No token.'
}), 400
tokens = {'Authorization': 'Bearer' + refresh_token, 'X-FROM-ASSET-REGISTRY': "True"}
requests.get(app.config['USER_REGISTRY_BASE_URL'] + '/logout', headers=tokens)
resp_fe = make_response(jsonify({"message": "Successfully logged out"}), 200)
localStorage.removeItem('access_token')
localStorage.removeItem('refresh_token')
resp_fe.set_cookie('access_token_cookie', '', expires=0)
resp_fe.set_cookie('refresh_token_cookie', '', expires=0)
return resp_fe
except Exception as e:
return jsonify({
'message': 'Asset Registry Logout Error',
'error': f'{e}'
}), 400
@app.route('/login', methods=['POST'])
def login():
if request.is_json:
data = request.get_json()
email = data.get('email')
password = data.get('password')
if email is None or password is None:
return jsonify({'message': 'Missing arguments'}), 400
data['asset_registry'] = True
try:
headers = {'X-EMAIL': email, 'X-PASSWORD': password, 'X-ASSET-REGISTRY': "True"}
res = requests.get(app.config['USER_REGISTRY_BASE_URL'], headers=headers)
json_res = json.loads(res.content.decode())
except Exception as e:
return jsonify({
'message': 'User Registry Error',
'error': f'{e}'
}), 400
if res.status_code == 200:
response_fe = make_response(jsonify(json_res), 200)
response_fe.set_cookie('refresh_token_cookie', json_res.get('refresh_token'))
response_fe.set_cookie('access_token_cookie', json_res.get('access_token'))
return response_fe
else:
response_fe = make_response(jsonify(json_res), 400)
return response_fe
return jsonify({'message': 'Missing JSON in request'}), 400
# @app.route('/kml-to-wkt', methods=['POST'])
# def convert_kml_to_wkt():
# kml_file = request.files.get('kml')
#
# fiona.supported_drivers['KML'] = 'rw'
# f = fiona.BytesCollection(bytes(kml_file.content))
# df = gpd.GeoDataFrame()
#
# gdf = gpd.read_file(kml_file, driver='KML')
# poly = gdf.geometry.iloc[0] # shapely polygon
# wkt = poly.wkt
@app.route('/register-field-boundary', methods=['POST'])
@Utils.token_required
def register_field_boundary():
"""
Registering a field boundary against a Geo Id
"""
try:
data = json.loads(request.data.decode('utf-8'))
field_wkt = data.get('wkt')
threshold = data.get('threshold') or 95
resolution_level = 20
field_boundary_geo_json = Utils.get_geo_json(field_wkt)
boundary_type = "manual"
# check if request from automated system
if request.headers.get('AUTOMATED-FIELD') is not None:
automated_field = bool(int(request.headers.get('AUTOMATED-FIELD')))
if automated_field:
boundary_type = "automated"
# set lat lng from geoJson first coordinate.
lat = field_boundary_geo_json['geometry']['coordinates'][0][0][1]
lng = field_boundary_geo_json['geometry']['coordinates'][0][0][0]
p = Point([lng, lat])
country = Utils.get_country_from_point(p)
are_in_acres = Utils.get_are_in_acres(field_wkt)
if are_in_acres > 1000:
return make_response(jsonify({
"message": f"Cannot register a field with Area greater than 1000 acres",
"Field area (acres)": are_in_acres
}), 200)
s2_index = data.get('s2_index')
if s2_index:
s2_index_to_fetch = [int(i) for i in (data.get('s2_index')).split(',')]
s2_indexes_to_remove = Utils.get_s2_indexes_to_remove(s2_index_to_fetch)
# get the Different resolution level indices
# list against a key (e.g. 13) is a list of tokens(hex encoded version of the cell id)
indices = {8: S2Service.wkt_to_cell_tokens(field_wkt, 8),
13: S2Service.wkt_to_cell_tokens(field_wkt, 13),
15: S2Service.wkt_to_cell_tokens(field_wkt, 15),
18: S2Service.wkt_to_cell_tokens(field_wkt, 18),
19: S2Service.wkt_to_cell_tokens(field_wkt, 19),
20: S2Service.wkt_to_cell_tokens(field_wkt, 20),
}
# fetching the new s2 cell tokens records for different Resolution Levels, to be added in the database
records_list_s2_cell_tokens_middle_table_dict = Utils.records_s2_cell_tokens(indices)
# generate the geo_id only for `s2_index__l13_list`
geo_id = Utils.generate_geo_id(indices[13])
geo_id_l20 = Utils.generate_geo_id(indices[20])
# lookup the database to see if geo id already exists
geo_id_exists_wkt = Utils.lookup_geo_ids(geo_id)
# if geo id not registered, register it in the database
if not geo_id_exists_wkt:
geo_data_to_return = None
geo_data = Utils.register_field_boundary(geo_id, indices, records_list_s2_cell_tokens_middle_table_dict,
field_wkt, country, boundary_type)
if s2_index and s2_indexes_to_remove != -1:
geo_data_to_return = Utils.get_specific_s2_index_geo_data(geo_data, s2_indexes_to_remove)
return jsonify({
"message": "Field Boundary registered successfully.",
"Geo Id": geo_id,
"S2 Cell Tokens": geo_data_to_return,
"Geo JSON": field_boundary_geo_json
})
else:
# check for the percentage match for the given threshold for L20
# get the L20 indices
# s2_index__L20_list is a list of tokens(hex encoded version of the cell id)
s2_index_to_check = indices[20]
# fetch geo ids for tokens and checking for the percentage match
matched_geo_ids = Utils.fetch_geo_ids_for_cell_tokens(s2_index_to_check, "")
percentage_matched_geo_ids = Utils.check_percentage_match(matched_geo_ids, s2_index_to_check,
resolution_level,
threshold)
if len(percentage_matched_geo_ids) > 0:
return jsonify({
'message': 'Threshold matched for already registered Field Boundary(ies)',
'matched geo ids': percentage_matched_geo_ids
}), 400
geo_id_exists_wkt_l20 = Utils.lookup_geo_ids(geo_id_l20)
if not geo_id_exists_wkt_l20:
geo_data_to_return = None
geo_data = Utils.register_field_boundary(geo_id_l20, indices,
records_list_s2_cell_tokens_middle_table_dict,
field_wkt, country, boundary_type)
if s2_index and s2_indexes_to_remove != -1:
geo_data_to_return = Utils.get_specific_s2_index_geo_data(geo_data, s2_indexes_to_remove)
return jsonify({
"message": "Field Boundary registered successfully.",
"Geo Id": geo_id_l20,
"S2 Cell Tokens": geo_data_to_return,
"Geo JSON": field_boundary_geo_json
})
else:
return make_response(jsonify({
"message": f"Field Boundary already registered.",
"Geo Id": geo_id_l20,
"Geo JSON requested": field_boundary_geo_json,
"Geo JSON registered": Utils.get_geo_json(geo_id_exists_wkt_l20)
}), 200)
except Exception as e:
# noinspection PyPackageRequirements
return jsonify({
'message': 'Register Field Boundary Error',
'error': f'{e}'
}), 400
@app.route('/register-point', methods=['POST'])
@Utils.token_required
def register_point():
"""
Registering a point against a Geo Id
"""
try:
data = json.loads(request.data.decode('utf-8'))
point_wkt = data.get('wkt')
resolution_level = 30
point_geo_json = Utils.get_geo_json(point_wkt)
boundary_type = "manual"
# check if request from automated system
if request.headers.get('AUTOMATED-FIELD') is not None:
automated_field = bool(int(request.headers.get('AUTOMATED-FIELD')))
if automated_field:
boundary_type = "automated"
# set lat lng from geoJson first coordinate.
lat = point_geo_json['geometry']['coordinates'][1]
lng = point_geo_json['geometry']['coordinates'][0]
p = Point([lng, lat])
country = Utils.get_country_from_point(p)
s2_index = data.get('s2_index')
if s2_index:
s2_index_to_fetch = [int(i) for i in (data.get('s2_index')).split(',')]
s2_indexes_to_remove = Utils.get_s2_indexes_to_remove(s2_index_to_fetch)
# get the Different resolution level indices
# list against a key (e.g. 13) is a list of tokens(hex encoded version of the cell id)
indices = {8: S2Service.wkt_to_cell_tokens(point_wkt, 8, point=True),
13: S2Service.wkt_to_cell_tokens(point_wkt, 13, point=True),
15: S2Service.wkt_to_cell_tokens(point_wkt, 15, point=True),
18: S2Service.wkt_to_cell_tokens(point_wkt, 18, point=True),
19: S2Service.wkt_to_cell_tokens(point_wkt, 19, point=True),
20: S2Service.wkt_to_cell_tokens(point_wkt, 20, point=True),
30: S2Service.wkt_to_cell_tokens(point_wkt, 30, point=True),
}
# fetching the new s2 cell tokens records for different Resolution Levels, to be added in the database
records_list_s2_cell_tokens_middle_table_dict = Utils.records_s2_cell_tokens(indices)
# generate the geo_id only for `s2_index__l13_list`
geo_id = Utils.generate_geo_id(indices[30])
geo_id_l20 = Utils.generate_geo_id(indices[20])
# lookup the database to see if geo id already exists
geo_id_exists_wkt = Utils.lookup_geo_ids(geo_id)
# if geo id not registered, register it in the database
if not geo_id_exists_wkt:
geo_data_to_return = None
geo_data = Utils.register_field_boundary(geo_id, indices, records_list_s2_cell_tokens_middle_table_dict,
point_wkt, country, boundary_type)
if s2_index and s2_indexes_to_remove != -1:
geo_data_to_return = Utils.get_specific_s2_index_geo_data(geo_data, s2_indexes_to_remove)
return jsonify({
"message": "Point registered successfully.",
"Geo Id": geo_id,
"S2 Cell Tokens": geo_data_to_return,
"Geo JSON": point_geo_json
})
else:
return make_response(jsonify({
"message": f"Point already registered.",
"Geo Id": geo_id_l20,
"Geo JSON requested": point_geo_json,
"Geo JSON registered": Utils.get_geo_json(geo_id_exists_wkt)
}), 400)
except Exception as e:
# noinspection PyPackageRequirements
return jsonify({
'message': 'Register Point Error',
'error': f'{e}'
}), 400
# Deprecated!!!
# @app.route('/fetch-overlapping-fields', methods=['POST'])
# @Utils.token_required
# def fetch_overlapping_fields():
# """
# Fetch the overlapping fields for a certain threshold
# Overlap is being checked for L13 Resolution Level
# Optional domain parameter for filtering fields based on associated domain
# Returning the fields Geo Ids
# """
# try:
# data = json.loads(request.data.decode('utf-8'))
# field_wkt = data.get('wkt')
# resolution_level = data.get('resolution_level') or 13
# threshold = data.get('threshold') or 95
# s2_index = data.get('s2_index')
# domain = data.get('domain') or ""
# boundary_type = data.get('boundary_type') or ""
#
# # get the L13 indices
# # s2_index__L13_list is a list of tokens(hex encoded version of the cell id)
# s2_index__l13_list = S2Service.wkt_to_cell_tokens(field_wkt, resolution_level)
#
# # fetch geo ids for tokens and checking for the percentage match
# matched_geo_ids = Utils.fetch_geo_ids_for_cell_tokens(s2_index__l13_list, domain, boundary_type)
# percentage_matched_geo_ids = Utils.check_percentage_match(matched_geo_ids, s2_index__l13_list, resolution_level,
# threshold)
# percentage_matched_fields = Utils.fetch_fields_for_geo_ids(percentage_matched_geo_ids, s2_index)
#
# return make_response(jsonify({
# "message": "The field Geo Ids with percentage match of the given threshold.",
# "Matched Fields": percentage_matched_fields
# }), 200)
# except Exception as e:
# return jsonify({
# 'message': 'Fetch Overlapping Fields Error',
# 'error': f'{e}'
# }), 400
@app.route('/fetch-field/<geo_id>', methods=['GET'])
def fetch_field(geo_id):
"""
Fetch a Field (S2 cell tokens) for the provided Geo Id
:param geo_id:
:return:
"""
try:
s2_index_to_fetch = None
args = request.args
if args.getlist('s2_index') and args.getlist('s2_index')[0]:
s2_index_to_fetch = [int(i) for i in (args.getlist('s2_index')[0]).split(',')]
if s2_index_to_fetch:
s2_indexes_to_remove = Utils.get_s2_indexes_to_remove(s2_index_to_fetch)
field = geoIdsModel.GeoIds.query \
.filter_by(geo_id=geo_id) \
.first()
if not field:
return make_response(jsonify({
"message": "Field not found, invalid Geo Id."
}), 404)
field_boundary_geo_json = Utils.get_geo_json(json.loads(field.geo_data)['wkt'])
geo_data = None
if s2_index_to_fetch and s2_indexes_to_remove != -1:
geo_data = Utils.get_specific_s2_index_geo_data(field.geo_data, s2_indexes_to_remove)
return make_response(jsonify({
"message": "Field fetched successfully.",
"GEO Id": geo_id,
"Geo Data": geo_data,
"Geo JSON": field_boundary_geo_json
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch Field Error',
'error': f'{e}'
}), 400
@app.route('/fetch-field-wkt/<geo_id>', methods=['GET'])
def fetch_field_wkt(geo_id):
"""
Fetch a Field WKT for the provided Geo Id
:param geo_id:
:return:
"""
try:
field = Utils.fetch_field_by_geoid(geo_id)
return make_response(jsonify({
"message": "WKT fetched successfully.",
"GEO Id": geo_id,
"WKT": json.loads(field.geo_data)['wkt']
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch Field WKT Error',
'error': f'{e}'
}), 400
@app.route('/get-percentage-overlap-two-fields', methods=['POST'])
def get_percentage_overlap_two_fields():
"""
Passed in 2 GeoIDs, determine what is the % overlap of the 2 fields
:return:
"""
try:
try:
data = json.loads(request.data.decode('utf-8'))
geo_id_field_1 = data.get('geo_id_field_1')
geo_id_field_2 = data.get('geo_id_field_2')
if not geo_id_field_1 or not geo_id_field_2:
return make_response(jsonify({
"message": "Two Geo Ids are required."
}), 400)
percentage_overlap = Utils.get_percentage_overlap_two_fields(geo_id_field_1, geo_id_field_2)
except AttributeError as error:
return make_response(jsonify({
"message": str(error)
}), 404)
return make_response(jsonify({
"Percentage Overlap": str(percentage_overlap) + ' %'
}), 200)
except Exception as e:
return jsonify({
'message': 'Get Percentage Overlap two Fields Error',
'error': f'{e}'
}), 400
@app.route('/fetch-fields-for-a-point', methods=['POST'])
@Utils.token_required
def fetch_fields_for_a_point():
"""
Fetch all the fields containing the point
Latitude and Longitude provided
Check for L13 and L20
Two stage search
Optional domain parameter for filtering fields based on associated domain
:return:
"""
try:
data = json.loads(request.data.decode('utf-8'))
lat = data.get('latitude')
long = data.get('longitude')
domain = data.get('domain')
boundary_type = data.get('boundary_type')
s2_index = data.get('s2_index')
if not lat or not long:
return make_response(jsonify({
"message": "Latitude and Longitude are required."
}), 400)
s2_cell_token_13, s2_cell_token_20 = S2Service.get_cell_token_for_lat_long(lat, long)
fetched_fields = Utils.fetch_fields_for_a_point_two_way(s2_cell_token_13, s2_cell_token_20, domain, s2_index,
boundary_type)
return make_response(jsonify({
"Fetched fields": fetched_fields
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch Fields for a Point Error',
'error': f'{e}'
}), 400
# Deprecated!!!
# @app.route('/fetch-bounding-box-fields', methods=['POST'])
# @Utils.token_required
# def fetch_bounding_box_fields():
# """
# Fetch the fields intersecting the Bounding Box
# 4 vertices are provided
# :return:
# """
# try:
# data = json.loads(request.data.decode('utf-8'))
# latitudes = list(map(float, data.get('latitudes').split(' ')))
# longitudes = list(map(float, data.get('longitudes').split(' ')))
# s2_index = data.get('s2_index')
# if not latitudes or not longitudes:
# return make_response(jsonify({
# "message": "Latitudes and Longitudes are required."
# }), 400)
# s2_cell_tokens_13 = S2Service.get_cell_tokens_for_bounding_box(latitudes, longitudes)
# s2_cell_tokens_20 = S2Service.get_cell_tokens_for_bounding_box(latitudes, longitudes, 20)
# fields = Utils.fetch_fields_for_cell_tokens(s2_cell_tokens_13, s2_cell_tokens_20, s2_index)
# return make_response(jsonify({
# "message": fields
# }), 200)
# except Exception as e:
# return jsonify({
# 'message': 'Fetch Bounding Box Fields Error',
# 'error': f'{e}'
# }), 400
@app.route("/domains", methods=['GET'])
def fetch_all_domains():
"""
Fetching all the domains from the User Registry
:return:
"""
res = requests.get(app.config['USER_REGISTRY_BASE_URL'] + '/domains', timeout=2)
return jsonify({
"message": "All domains",
"Domains": res.json()['Domains']
}), 200
@app.route("/domains", methods=['POST'])
def authorize_a_domain():
"""
Authorize a domain, will have an authority token
:return:
"""
data = json.loads(request.data.decode('utf-8'))
domain = data.get('domain')
if not domain:
return make_response(jsonify({
"message": "Domain is required."
}), 400)
req_body = {'domain': domain}
res = requests.post(app.config['USER_REGISTRY_BASE_URL'] + '/domains', json=req_body, timeout=2)
return jsonify({
"message": res.json()["Message"]
}), 200
@app.route('/fetch-registered-field-count', methods=['GET'])
@Utils.token_required
def fetch_registered_field_count():
"""
Fetch a total registered field count
:return:
"""
try:
count = geoIdsModel.GeoIds.query \
.count()
return make_response(jsonify({
"message": "Total count fetched successfully.",
"count": count,
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch registered field count error!',
'error': f'{e}'
}), 400
@app.route('/fetch-field-count-by-month', methods=['GET'])
@Utils.token_required
def fetch_field_count_by_month():
"""
Fetch Registered Field count by Month, last 12 month
:return:
"""
try:
count = Utils.get_row_count_by_month()
return make_response(jsonify({
"message": "fetched Count By Month successfully.",
"count": count,
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch field counts by month error!',
'error': f'{e}'
}), 400
@app.route('/fetch-field-count-by-country', methods=['GET'])
@Utils.token_required
def fetch_field_count_by_country():
"""
Fetch Registered Field count by Country
:return:
"""
try:
count = Utils.get_row_count_by_country()
return make_response(jsonify({
"message": "Fetched count by country successfully.",
"count": count,
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch field counts by country error!',
'error': f'{e}'
}), 400
@app.route('/fetch-field-count-by-domain', methods=['GET'])
@Utils.token_required
def fetch_field_count_by_domains():
"""
Fetch Fields count by the Domains(authorized)
:return:
"""
try:
count_by_authority_tokens = Utils.get_fields_count_by_domain()
authority_tokens = [count_by_authority_token['authority_token'] for count_by_authority_token in
count_by_authority_tokens]
# getting the domains against the authority tokens from User Registry
csrf_token = generate_csrf()
headers = {'X-CSRFToken': csrf_token, 'Authority-Tokens': str(authority_tokens)}
res = requests.get(app.config['USER_REGISTRY_BASE_URL'] + '/fields-count-by-domain', json=authority_tokens,
timeout=2, headers=headers)
if res.json().get("error") is not None:
return jsonify({
'message': 'Fetch field counts by domain error',
'error': f'{res.json()["error"]}'
}), 400
authority_token_dict = res.json()["authority_token_dict"]
field_count_by_domain = [{'domain': authority_token_dict[count_by_authority_token["authority_token"]],
'count': count_by_authority_token["count"]} for count_by_authority_token in
count_by_authority_tokens]
return make_response(jsonify({
"message": "Fetched count by domains successfully.",
"count": field_count_by_domain,
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch field counts by domain error!',
'error': f'{e}'
}), 400
@app.route('/populate-country-in-geo-ids', methods=['POST'])
def populate_country_in_geo_ids():
# Get all rows where country is empty or None
rows = db.session.query(geoIdsModel.GeoIds).filter((geoIdsModel.GeoIds.country == '') | (
geoIdsModel.GeoIds.country == None)).all() # don't replace with is None
print(rows)
# Loop through each row and update the country column
for row in rows:
# Parse the geo_data JSON string and extract the WKT string
json_data = json.loads(row.geo_data)
wkt_string = json_data['wkt']
polygon = load_wkt(wkt_string)
p = Point(polygon.exterior.coords[0])
country = Utils.get_country_from_point(p)
# Update the row in the database with the new country value
row.country = country
db.session.commit()
return jsonify({'message': 'Countries updated successfully'}), 200
@app.route('/fetch-session-cookies', methods=['GET'])
def fetch_session_cookies():
"""
Fetch the Session Cookies from User Registry
:return:
"""
try:
access_token = localStorage.getItem('access_token')
refresh_token = localStorage.getItem('refresh_token')
return make_response(jsonify({"access_token": access_token, "refresh_token": refresh_token}))
except Exception as e:
return jsonify({
'message': 'Fetch Session Cookies Error!',
'error': f'{e}'
}), 400
@app.route('/fetch-field-centroid/<geo_id>', methods=['GET'])
def fetch_field_centroid(geo_id):
"""
Fetch a Field Centroid for the provided Geo Id
:param geo_id:
:return:
"""
try:
field = Utils.fetch_field_by_geoid(geo_id)
field_wkt = json.loads(field.geo_data)['wkt']
if not field_wkt:
return make_response(jsonify({
"message": "Field WKT not found, fetch Field Centroid Error."
}), 400)
centroid = Utils.fetch_field_centroid_by_wkt(field_wkt)
return make_response(jsonify({
"message": "Centroid fetched successfully.",
"Centroid": centroid,
}), 200)
except Exception as e:
return jsonify({
'message': 'Fetch Field Centroid Error',
'error': f'{e}'
}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0')