-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_movement.py
1092 lines (919 loc) · 43.1 KB
/
update_movement.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Convert CSV format market reports to records in the roundup_report, movement and address tables."""
import csv
import argparse
import json
import re
import sys
import traceback
import dateutil.parser
from datetime import date, timedelta
from time import sleep
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from pathlib import Path
from sqlalchemy import and_, not_, or_, text, func
from sqlalchemy.sql.expression import null
from sqlalchemy.orm import aliased
from db_class import (
CtyODp4, Address, Geoname, Premises,
Roundup, Movement, RoundupReport, Association, RoundupMarket,
Market, RoundupWebsite
)
from db_util import create_session, state_abbr, state_full, geoname_query_field
CHARSET = 'utf-8'
# Header strings, must match Premises, Geoname objects
address_prefix = ['sale_', 'consignor_', 'buyer_']
address_field = ['name', 'address', 'po', 'city', 'state', 'zip', 'zip_ext']
nameless_address_field = address_field.copy()
nameless_address_field.remove('name')
# Thresholds for market de-duplication
FT_NAME_THRESHOLD = 5
FT_ADDRESS_THRESHOLD = 4
FT_NAME_ONLY_THRESHOLD = 10
# Lag for Market News Service report date match
MNS_LAG_MAX = 3
class BadReportLogic(Exception):
"""Custom error for screwy CSV files."""
def read_csv(row):
"""Extract list of potential holdings (nodes) in a row, supplied as a dictionary, from a csv file."""
node = []
for this_address_prefix in address_prefix:
this_node = {
k.replace(this_address_prefix, ''): v for k, v in row.items()
if this_address_prefix in k
}
this_node = {
k: v for k, v in this_node.items()
if k in address_field and v
}
if this_node:
node.append(this_node)
else:
node.append(None)
return tuple(node)
class Insert(object):
"""An instance of Insert can handle Premises and Movement insertions,
creating Geoname entries as necessary."""
def __init__(self, session):
self.session = session
def report(self, row, this_csv_file):
report = None
file_name = this_csv_file.name
result = self.session.query(RoundupReport).filter_by(reference=file_name).first()
if result:
print("Report {} already entered.".format(file_name))
elif row:
row = {k: v.strip() for k, v in row.items() if v}
report = {
'reference': file_name,
'date': date(
int(row['sale_year']),
int(row['sale_month']),
int(row['sale_day'])
),
}
report.update({
k.replace('sale_',''): v for k, v in row.items() if k in ['sale_title', 'sale_head']
})
report = RoundupReport(**report)
return report
def address(self, address):
"""Add address (if new) and return inserted (or found)
objects of class Roundup.
"""
def get_address(address):
where = {k: address.get(k) for k in address_field}
query = self.session.query(RoundupMarket).filter_by(**where)
result = query.first()
if not result:
query = self.session.query(Roundup).filter_by(**where)
result = query.first()
if result:
address = result
else:
address = Roundup(**address)
self.session.add(address)
return address
if not isinstance(address, Address):
address = get_address(address)
return address
def movement(self, from_address, to_address, row, report):
"""Insert a new Movement."""
cattle = {k.replace('cattle_',''): v for k, v in row.items() if v and 'cattle_' in k}
if len(set(cattle.values())) < len(cattle):
print(report.reference)
print("Identical values in {}".format(cattle))
if input("Proceed anyway? y/(n)")!='y':
raise Exception("Aborted import to check duplicate values.")
movement = Movement(
report=report,
from_address=from_address,
to_address=to_address,
**cattle
)
self.session.add(movement)
def premises(self, from_address, to_address):
def get_geoname(address):
def create_geoname(address):
def use_geoname_api(address):
def query_geoname_api(address, fuzzy, city=None):
def get_geoname_url(address, fuzzy, city):
if not city:
city = address.city
if state_abbr.get(address.state):
state = address.state
else:
state = state_full.get(address.state)
full_query = {
'adminCode1': state,
'style': 'full',
'username': 'roundup',
}
if address.zip:
postalcode = None
if len(address.zip)==5:
postalcode = address.zip
elif address.zip_ext and len(address.zip)==3:
postalcode = address.zip
if postalcode:
base_url = 'http://api.geonames.org/postalCodeSearchJSON?'
full_query.update({
'postalcode': address.zip,
'placename': city,
})
else:
base_url = 'http://api.geonames.org/searchJSON?'
full_query.update({
'name_equals': city,
'fuzzy': "{:.1f}".format(0.1*(10 - fuzzy)),
'featureClass': 'P',
'continentCode': 'NA',
})
query = {k: v for k, v in full_query.items() if v}
geoname_url = base_url + urlencode(query)
return geoname_url
geoname = []
geoname_url = get_geoname_url(address, fuzzy, city)
request = Request(geoname_url)
sleep(0.5)
with urlopen(request) as io:
response = json.loads(io.read().decode())
if response.get('status'):
print(response['status']['message'])
msg = "Geonames returned a status message on address {}. Continue? y/(n) "
rsp = input(msg.format(address.id))
if 'y'==rsp:
return geoname
else:
self.session.rollback()
self.session.close()
sys.exit()
if response.get('geonames'):
geoname = response['geonames']
elif response.get('postalCodes'):
geoname = response['postalCodes']
if len(geoname) > 1:
print('Multiple responses from postalCode search.')
geoname = [
this_geoname for this_geoname in geoname
if this_geoname.get('adminCode2') and (this_geoname['adminCode1'] in state_abbr.keys())
]
return geoname
def fix_city(address):
ask = "Okay to replace {} with \'{}\' in geocode query? y/(n) "
new_city = None
old_city = address.city
ask_if_correct = None
correction = [
{
'regex': r'^wh?i?t?e?\.? *su?l?p?h?u?r?\.?',
'correct': 'White Sulphur Springs',
},
{
'regex': r'pompey\'?s +pill?a?r?',
'correct': 'Pompey\'s Pillar',
},
{
'regex': r'heber city',
'correct': 'Heber',
},
{
'regex': r'miles +ci?t?y?',
'correct': 'Miles City',
},
{
'regex': r'^l(\.| ) *falls$',
'correct':'Little Falls',
},
{
'regex': r'^l(\.| ) *prairie$',
'correct':'Long Prairie',
},
{
'regex': r'^ff$',
'correct':'Fergus Falls',
},
{
'regex': r'(^|\b)(?P<sub>ft(\.| ) *).+',
'sub':'Fort ',
},
{
'regex': r'(^|\b)(?P<sub>pt\.?)($|\b)',
'sub':'Point',
},
{
'regex': r'(^|\b)(?P<sub>st(\.| ) *).+',
'sub':'Saint ',
},
{
'regex': r'(^|\b)(?P<sub>mt(\.| ) *).+',
'sub':'Mount ',
},
{
'regex': r'(^|\b)(?P<sub>w(\.| ) *).+',
'sub':'West ',
},
{
'regex': r'(^|\b)(?P<sub>s(\.| ) *).+',
'sub':'South ',
},
]
for this_correction in correction:
match = re.search(this_correction['regex'], old_city, re.IGNORECASE)
if match and this_correction.get('correct'):
ask_if_correct = this_correction['correct']
break
elif match and this_correction.get('sub'):
old_city = old_city.replace(match.group('sub'), this_correction['sub'])
ask_if_correct = old_city
if ask_if_correct:
replace = "\'{}\'".format(address.city)
if address.state:
replace += " in {}".format(address.state)
input_prompt = ask.format(replace, ask_if_correct)
confirm = bool('y' == input(input_prompt))
if confirm:
new_city = ask_if_correct
return new_city
fuzzy = 0
geoname = query_geoname_api(address, fuzzy)
if not geoname:
new_city = fix_city(address)
if new_city:
geoname = query_geoname_api(address, fuzzy, new_city)
while fuzzy < 10 and not geoname:
fuzzy += 1
geoname = query_geoname_api(address, fuzzy, new_city)
return geoname, fuzzy
geoname = None
if address.city:
geoname, fuzzy = use_geoname_api(address)
if geoname:
fuzzy = "{:.1f}".format(0.1*(10 - fuzzy))
for idx, this_geoname in enumerate(geoname):
this_geoname = {k: v for k, v in this_geoname.items() if k in geoname_query_field}
geoname[idx] = Geoname(fuzzy=fuzzy, **this_geoname)
else:
# this empty geoname will prevent futile searches on the same address data
geoname = [Geoname()]
return geoname
if isinstance(address, RoundupMarket):
query = self.session.query(Premises).join(Association).filter_by(address_id=address.id)
geoname = [query.one().geoname]
else:
# check for geonames already searched for given identical location information
where = {k: getattr(address, k) for k in nameless_address_field}
query = self.session.query(Address).filter_by(**where
).join(Geoname, Address.id==Geoname.address_id
)
result = query.all()
if len(result)==1:
geoname = result[0].geoname_cache
elif len(result)==0:
geoname = None
else:
print('Unexpected.')
raise(Exception)
if not geoname:
geoname = create_geoname(address)
address.geoname_cache = geoname
return geoname
def minimize_distance(from_geoname, to_geoname):
from_geoname = [this_geoname for this_geoname in from_geoname if this_geoname.adminCode2]
to_geoname = [this_geoname for this_geoname in to_geoname if this_geoname.adminCode2]
geoname = None
distance_min = float("inf")
for this_to_geoname in to_geoname:
# Destination FIPS
destination = state_abbr[this_to_geoname.adminCode1] + this_to_geoname.adminCode2
for this_from_geoname in from_geoname:
# Origin FIPS
origin = state_abbr[this_from_geoname.adminCode1] + this_from_geoname.adminCode2
where = {'org': origin, 'dest': destination}
distance = self.ctyod.query(CtyODp4.GCD).filter_by(**where).scalar()
if distance!=None and distance<distance_min:
distance_min = distance
geoname = {'from': this_from_geoname, 'to': this_to_geoname}
return geoname
def get_premises(geoname, address):
if len(geoname)==1:
geoname = geoname[0]
else:
geoname = next(
(item for item in geoname if item.geonameId==None),
Geoname()
)
geoname.address = address
premises = Premises(geoname=geoname)
return premises
from_geoname = get_geoname(from_address)
to_geoname = get_geoname(to_address)
geoname = minimize_distance(from_geoname, to_geoname)
if geoname:
from_premises = Premises(geoname=geoname['from'])
to_premises = Premises(geoname=geoname['to'])
else:
from_premises = get_premises(from_geoname, from_address)
to_premises = get_premises(to_geoname, to_address)
if not isinstance(from_address, RoundupMarket):
self.session.add(Association(premises=from_premises, address=from_address, to_address=to_address))
if not isinstance(to_address, RoundupMarket):
self.session.add(Association(premises=to_premises, address=to_address, from_address=from_address))
def main(args):
def import_roundup_report(path, glob):
# list of csv files to import
csv_file = Path(path).glob(glob)
Session = create_session(check=True, **session_args)
for this_csv_file in csv_file:
insert = Insert(Session())
try:
# Establish a new session to communicate with the mysql server.
# Each csv_file can be rolled back individually.
with this_csv_file.open('r') as io:
reader = csv.DictReader(io)
row = next(reader, {})
report = insert.report(row, this_csv_file)
if report:
io.seek(0)
io.readline()
for row in reader:
row = {k: v.strip() for k, v in row.items() if v}
sale, consignor, buyer = read_csv(row)
if consignor and sale:
from_address = insert.address(consignor)
to_address = insert.address(sale)
insert.movement(from_address, to_address, row, report)
if buyer:
from_address = to_address
to_address = insert.address(buyer)
insert.movement(from_address, to_address, row, report)
elif consignor and buyer:
from_address = insert.address(consignor)
to_address = insert.address(buyer)
insert.movement(from_address, to_address, row, report)
elif sale and buyer:
from_address = insert.address(sale)
to_address = insert.address(buyer)
insert.movement(from_address, to_address, row, report)
elif sale:
# no consignor or buyer information was extracted for a sale
continue
else:
# This should not happen, but would if only a consignor or buyer is listed in the CSV file.
msg = (
"A csv file appears to include incorrectly extracted data.\n"
"File: {}\nConsignor: {}\nSale: {}\nBuyer: {}".format(this_csv_file, consignor, sale, buyer)
)
raise BadReportLogic(msg)
insert.session.commit()
except BadReportLogic as err:
print(err)
insert.session.rollback()
except:
msg = "While importing report {}:".format(this_csv_file)
print(msg)
print(traceback.format_exc())
insert.session.rollback()
else:
archived = this_csv_file.parent / archive_path / this_csv_file.name
if not archived.exists():
this_csv_file.rename(archived)
finally:
insert.session.close()
def deduplicate_market():
def next_match(session, market, market_chain):
def get_query(exclude, city=True):
# Search against markets beyond the existing chain of matches
# or anything else in exclude, added from non-null mis-matches.
query = session.query(Market).filter(not_(Market.id.in_(exclude)))
# State filter applies to all remaining matches.
query = query.filter_by(state=market.state)
if city:
# City filter often applies
query = query.filter_by(city=market.city)
return query
def also_exclude(exclude):
query = session.query(Association
).join(Market, Market.id==Association.address_id
)
return query.filter(Market.id.in_(exclude))
result = None
exclude = set(this_market.id for this_market in market_chain)
# During import, rows with multiple versions of an attribute were split.
# These are certainly duplicates.
row = getattr(market, 'row', None)
ThisMarket = type(market)
if row:
result = session.query(ThisMarket
).filter(not_(ThisMarket.id.in_(exclude))
).filter_by(row=row).first()
# Gather null features, as needed and if available, from match_chain
c = {k: None for k in ['name', 'address', 'po']}
for k in c:
c[k] = [getattr(this_market, k, None) for this_market in market_chain]
c[k] = [v for v in c[k] if v]
# The following matches are all valid, but their order implies precedence.
if not result and c['po'] and market.city:
query = get_query(exclude)
po_filter = [Market.po==v for v in c['po']]
next_query = query.filter(or_(*po_filter))
result = next_query.first()
next_query = query.filter(Market.po!=None)
exclude |= set(r.id for r in next_query)
exclude |= set(r.address_id for r in also_exclude(exclude))
if not result and c['address'] and market.city:
query = get_query(exclude)
for address in c['address']:
full_text = Market.address.match(address)
next_query = query.filter(full_text > FT_ADDRESS_THRESHOLD).order_by(full_text.desc())
result = next_query.first()
if result:
break
next_query = query.filter(Market.address!=None)
exclude |= set(r.id for r in next_query)
exclude |= set(r.address_id for r in also_exclude(exclude))
if not result and c['name'] and market.city:
query = get_query(exclude)
for name in c['name']:
full_text = Market.name.match(name)
next_query = query.filter(full_text > FT_NAME_THRESHOLD).order_by(full_text.desc())
result = next_query.first()
if result:
break
next_query = query.filter(Market.name!=None)
exclude |= set(r.id for r in next_query)
exclude |= set(r.address_id for r in also_exclude(exclude))
if not result and not any(c.values()) and market.city:
query = get_query(exclude)
result = query.first()
if not result and c['name']:
query = get_query(exclude, city=False)
next_query = None
if c['address'] and not c['po']:
next_query = query.filter(Market.address==None, Market.po!=None)
elif c['po'] and not c['address']:
next_query = query.filter(Market.address!=None, Market.po==None)
if next_query:
for name in c['name']:
full_text = Market.name.match(name)
next_query = next_query.filter(full_text > FT_NAME_ONLY_THRESHOLD).order_by(full_text.desc())
result = next_query.first()
if result:
break
if result:
query = session.query(Association
).join(Market, Market.id==Association.address_id
).filter_by(id=result.id
)
association = query.first()
if result and association:
premises = association.premises
else:
premises = None
return result, premises
Session = create_session(check=False, **session_args)
session = Session()
query = session.query(Market
).outerjoin(Association, Market.id==Association.address_id
).filter_by(premises_id=None
)
market = query.first()
while market:
match_chain = []
repeat = True
while repeat:
match_chain.append(market)
match, premises = next_match(session, market, match_chain)
if match and premises:
repeat = False
elif not match:
repeat = False
else:
market = match
if not premises:
premises = Premises()
for this_match in match_chain:
if premises.geoname and not premises.geoname.geonameId:
premises.geoname = None
session.add(Association(premises=premises, address=this_match))
session.commit()
market = query.first()
session.close()
def geoname_roundup_market():
def get_geoname(session, market):
def location_search(location, most_fuzzy=0):
def query_open_mapquest(location, geocodeQuality, geoname=[]):
api_key = 'DKSnfc0n2psp0v900BUPD1hNDGHJnhql' # developer.mapquest.com itc2@georgetown.edu
base_url = 'http://open.mapquestapi.com/geocoding/v1/address?key=' + api_key + '&'
param = {
'street': location.get('address'),
'city': location.get('city'),
'state': location.get('state'),
'postalCode': location.get('zip'),
'country': 'US',
}
query = {k: v for k, v in param.items() if v}
url = base_url + urlencode(query)
request = Request(url)
with urlopen(request) as io:
response = json.loads(io.read().decode())
mapquest = [
res for res in response['results'][0]['locations']
if res['geocodeQuality'] in geocodeQuality
]
match = None
for this_mapquest in mapquest:
match = next((
this_geoname for this_geoname in geoname
if this_mapquest['adminArea4'] in this_geoname['adminName2']
), None)
if match:
break
return match, mapquest
def query_geoname_reverse(lat_lng):
base_url = 'http://api.geonames.org/findNearbyPlaceNameJSON?'
param = {
'lat': lat_lng['lat'],
'lng': lat_lng['lng'],
'style': 'full',
'username': 'roundup',
}
query = {k: v for k, v in param.items() if v}
geoname_url = base_url + urlencode(query)
request = Request(geoname_url)
sleep(0.5)
with urlopen(request) as io:
response = json.loads(io.read().decode())
if 'geonames' in response:
geoname = response['geonames']
else:
print("No geonames in response from {}.".format(geoname_url))
geoname = []
geoname = [
this_geoname for this_geoname in geoname
if this_geoname.get('adminCode2') and (this_geoname['adminCode1'] in state_abbr.keys())
]
return geoname
def debbreviate(location):
search = [
{'pattern': r'(^|\b)St\.? ', 'sub':'Saint '},
{'pattern': r'(^|\b)Mt\.? ', 'sub':'Mount '},
{'pattern': r'(^|\b)Ft\.? ', 'sub':'Fort '},
{'pattern': r'(^|\b)N\.? ', 'sub':'North '},
{'pattern': r'(^|\b)S\.? ', 'sub':'South '},
{'pattern': r'(^|\b)Mc ', 'sub':'Mc'},
{'pattern': r'(^|\b)Sprgs', 'sub':'Springs'},
]
city = location['city']
for this_search in search:
city = re.sub(this_search['pattern'], this_search['sub'], city, flags=re.IGNORECASE)
if city==location['city']:
change = False
else:
location['city'] = city
change = True
return change
def query_geoname(location, fuzzy):
base_url = 'http://api.geonames.org/searchJSON?'
param = {
'name_equals': location['city'],
'adminCode1': location['state'],
'fuzzy': "{:.1f}".format(0.1*(10 - fuzzy)),
'style': 'full',
'username': 'roundup',
'featureClass': 'P',
'continentCode': 'NA',
}
query = {k: v for k, v in param.items() if v}
geoname_url = base_url + urlencode(query)
request = Request(geoname_url)
sleep(0.5)
with urlopen(request) as io:
response = json.loads(io.read().decode())
if 'geonames' in response:
geoname = response['geonames']
else:
geoname = []
geoname = [
this_geoname for this_geoname in geoname
if this_geoname.get('adminCode2') and (this_geoname['adminCode1'] in state_abbr.keys())
]
return geoname
geoname = []
if not geoname and location.get('address'):
street = {k: v for k, v in location.items() if k in ['address', 'city', 'state']}
match, mapquest = query_open_mapquest(street, ['ADDRESS', 'STREET'])
if len(mapquest)==1:
fuzzy = None
lat_lng = mapquest[0]['latLng']
geoname = query_geoname_reverse(lat_lng)
if not geoname and not location.get('city'):
if location.get('zip'):
zip = {k: v for k, v in location.items() if k in ['zip', 'state']}
match, mapquest = query_open_mapquest(zip, ['ZIP'])
elif location.get('address'):
street = {k: v for k, v in location.items() if k in ['address', 'city', 'state']}
match, mapquest = query_open_mapquest(street, ['ADDRESS', 'STREET'])
if len(mapquest)==1:
location['city'] = mapquest[0]['adminArea5']
else:
print('Now what ...') #FIXME
if not geoname:
fuzzy = -1
while fuzzy <= most_fuzzy:
fuzzy += 1 # C'mon, it's just Mollins -> Mullins!
geoname = query_geoname(location, fuzzy)
if geoname:
break
if not geoname:
success = debbreviate(location)
if success:
fuzzy = 0
geoname = query_geoname(location, fuzzy)
if not geoname and location.get('zip'):
fuzzy = 6
geoname = query_geoname(location, fuzzy)
zip = {k: v for k, v in location.items() if k in ['zip']}
match, mapquest = query_open_mapquest(zip, ['ZIP'], geoname)
if match:
geoname = [match]
elif len(mapquest)==1:
fuzzy = None
lat_lng = mapquest[0]['latLng']
geoname = query_geoname_reverse(lat_lng)
if len(geoname) > 1:
match = False
if not match and location.get('zip'):
zip = {k: v for k, v in location.items() if k in ['zip']}
match, mapquest = query_open_mapquest(zip, ['ZIP'], geoname)
if not match and location.get('address'):
street = {k: v for k, v in location.items() if k in ['address', 'city', 'state']}
match, mapquest = query_open_mapquest(street, ['ADDRESS', 'STREET'], geoname)
if not match and location.get('address'):
street = {k: v for k, v in location.items() if k in ['address', 'state']}
match, mapquest = query_open_mapquest(street, ['ADDRESS', 'STREET'], geoname)
if match:
geoname = [match]
return geoname, fuzzy
location = {k: getattr(market, k) for k in ['address', 'city', 'state', 'zip']}
geoname, fuzzy = location_search(location, most_fuzzy=2)
for idx, this_geoname in enumerate(geoname):
this_geoname = {k: v for k, v in this_geoname.items() if k in geoname_query_field and v}
if fuzzy:
this_geoname['fuzzy'] = "{:.1f}".format(0.1*(10 - fuzzy))
geoname[idx] = this_geoname
return geoname
Session = create_session(check=False, **session_args)
session = Session()
outer_query = (
session.query(Premises)
.filter_by(geoname=None)
.join(Association)
.filter_by(to_address=None, from_address=None)
.group_by(Premises)
)
for premises in outer_query:
query = (
session.query(Market)
.join(Association, Market.id == Association.address_id)
.filter(Association.premises_id == premises.id)
.filter(or_(
Market.address != null(),
Market.city != null(),
Market.zip != null(),
))
)
# Prefer city, state associated with non-null street address
inner_query = query.order_by(Market.po, Market.address.desc())
# Geolocate market location
geoname = []
for market in inner_query:
geoname = get_geoname(session, market)
if len(geoname) == 1:
break
if len(geoname) == 0:
print("No geoname for premises {}.".format(premises.id))
geoname = Geoname()
elif len(geoname) > 1:
print("Multiple geonames for premises {}.".format(premises.id))
geoname = Geoname()
else:
geoname = Geoname(**geoname[0])
geoname.address = market
premises.geoname = geoname
session.commit()
session.close()
def geoname_roundup():
Session = create_session(check=True, **session_args)
session = Session()
insert = Insert(session)
Session = create_session(check=False, database='CtyOD')
insert.ctyod = Session()
# Roundup addresses in the from_address position, to intersect with next query
subquery = (
session.query(Movement.id)
.outerjoin(
Association,
and_(
Movement.from_address_id == Association.address_id,
Movement.to_address_id == Association.to_address_id,
)
)
.filter(Association.premises_id == null())
.subquery()
)
# Roundup addresses in the to_address position
query = (
session.query(Movement.from_address_id, Movement.to_address_id)
.outerjoin(
Association,
and_(
Movement.to_address_id == Association.address_id,
Movement.from_address_id == Association.from_address_id,
)
)
.filter(Association.premises_id == null())
.join(subquery, Movement.id == subquery.c.movement_id)
.group_by(Movement.from_address_id, Movement.to_address_id)
)
for movement in query:
from_address = (
session.query(Address)
.filter_by(id=movement.from_address_id)
.one()
)
to_address = (
session.query(Address)
.filter_by(id=movement.to_address_id)
.one()
)
try:
insert.premises(from_address, to_address)
insert.session.commit()
except:
insert.session.rollback()
insert.session.close()
insert.ctyod.close()
def add_market():
Session = create_session(check=False, **session_args)
session = Session()
insert_stmt = text(
"INSERT INTO roundup_market SET"
" address_id=:address_id, roundup_website_id=:foreign_id;"
)
update_stmt = text(
"UPDATE address SET source='roundup_market' "
" WHERE address_id=:address_id;"
)
add_another = True
while add_another:
address_id = input('Enter address_id: ')
session.execute(update_stmt.params(address_id=address_id))
foreign_id = input('Enter roundup_website_id: ')
session.execute(insert_stmt.params(address_id=address_id, foreign_id=foreign_id))
add_another = bool(
input('Add another market? yes/(no) ')=='yes'
)
session.commit()
session.close()
if input('Continue execution? yes/(no) ')!='yes':
sys.exit()
def get_mns_receipts():
def get_report_date(report_date, line):
match = None
for this_line in line:
match = re.search(r'report for (.+)$', this_line, re.IGNORECASE)
if match:
break
if not match:
match = re.search(r'\s{2,}(.*?)\s{2,}', line[1])
if match:
date_string = match.group(1)
try:
new_date = dateutil.parser.parse(date_string, fuzzy=True).date()
except ValueError:
new_date = None
if new_date!=date.today():
report_date = new_date
return report_date
def get_report_head(line):
head = None
for this_line in line:
match = re.search(r'receipts: *([0-9,]+)', this_line, re.IGNORECASE)
if match:
head = match.group(1).replace(',', '')
break
return head
file_name_pattern = "{mns}{y:d}{m:02d}{d:02d}"
url_pattern = "http://search.ams.usda.gov/mndms/{y:d}/{m:02d}/{name}.TXT"
Session = create_session(check=False, **session_args)
session = Session()
query = (
session.query(RoundupReport, RoundupWebsite)
.join(RoundupWebsite, RoundupReport.reference.startswith(RoundupWebsite.script))
.filter(RoundupReport.mns_id==None)
.filter(RoundupWebsite.mns_id!=None)
)
for report, website in query: