-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbl_query.py
235 lines (195 loc) · 9.19 KB
/
bbl_query.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
from pymongo import MongoClient
from datetime import datetime
from owner import Owner
from bbl import BBL
from building import Building
class BBLQuery:
def __init__(self, host, port):
client = MongoClient(host, port)
self.db = client.furman
def normalize_str(self, string):
return str(string).replace('|', ',')
def query_lots(self, bbl):
'''
This part is used to get all lots associated with a borough and block
'''
borough = bbl.borough
block = bbl.block
bbl_records = list(self.db.lot_records.find({'Borough':borough,'Block':block}, {'Lot':1}))
print "Found", len(bbl_records), "BBL in this area..."
lots = set()
for bbl_record in bbl_records:
lots.add(bbl_record['Lot'])
return lots
def get_bbls(self, bbl):
'''
This part will construct ALL bbls for a given borough and block
'''
lots = self.query_lots(bbl)
bbls = set()
for lot in lots:
bbls.add(BBL(bbl.borough, bbl.block, lot))
return bbls
def get_blocks(self, borough):
block_records = list(self.db.lot_records.find({'Borough':borough}, {'Block':1}))
blocks = set()
for block_record in block_records:
blocks.add(block_record['Block'])
return blocks
def match_owner(self, owner1, owner2):
'''
Each owner is a record of name and address
'''
def query_bbl(self, bbl):
borough = bbl.borough
block = bbl.block
lot = bbl.lot
bbl_repr = bbl.bbl_repr
print "BBL:", bbl
#This is the output
building = Building(bbl)
print "=== STEP 1: ==="
lot_records = list(self.db.lot_records.find({'Borough':borough,'Block':block,'Lot':lot}))
print "Found", len(lot_records), "records in lot_records..."
unique_keys = list()
for lot_record in lot_records:
unique_keys.append(lot_record['Unique_Key'])
master_records = list(self.db.master_records.find({'Unique_Key':{'$in':unique_keys}}))
print "Found", len(master_records), "records in master_records..."
latest_unique_key = None
latest_doc_date = datetime.strptime('1970-01-01', '%Y-%m-%d')
for master_record in master_records:
if master_record['Document_Date'] == "":
continue
if master_record['Doc_type'][0:4] == 'DEED':
if datetime.strptime(master_record['Document_Date'], '%Y-%m-%d') > latest_doc_date:
latest_unique_key = master_record['Unique_Key']
latest_doc_date = datetime.strptime(master_record['Document_Date'], '%Y-%m-%d')
if latest_unique_key is None:
print "Nothing found!"
return None
elif str(latest_unique_key)[0:3] == "BK_" or str(latest_unique_key)[0:3] == "FT_":
print "Latest UK is", latest_unique_key
print "We're not interested in this type of property!"
return None
else:
print "Latest UK is", latest_unique_key
#FINISH STEP 1
print "=== STEP 2: ==="
party_records = list(self.db.party_records.find({'Unique_Key':latest_unique_key}))
print "Found", len(party_records), "records in party_records..."
primary_party = None
for party_record in party_records:
if party_record['Party_type'] == 2:
primary_party = Owner({
'name': self.normalize_str(party_record['Name']),
'addr1': self.normalize_str(party_record['Addr1']),
'addr2': self.normalize_str(party_record['Addr2']),
'city': party_record['City'],
'state': party_record['State'],
'zip': party_record['Zip']
})
if primary_party is None:
print "No name found!"
return None
else:
print "Primary party is ", primary_party['name']
print primary_party
building.primary = primary_party
#FINISH STEP 2
print "=== STEP 3: ==="
secondary_unique_keys = list()
for master_record in master_records:
if master_record['Document_Date'] == "":
continue
if datetime.strptime(master_record['Document_Date'], '%Y-%m-%d') < latest_doc_date:
continue
#if master_record['doc_type'][0:4] == 'MTGE' or master_record['doc_type'][0:4] == 'AGMT':
if master_record['Doc_type'][0:4] == 'MTGE': #Not consider AGMT right now
secondary_unique_keys.append(master_record['Unique_Key'])
print "Found", len(secondary_unique_keys), "secondary UKs: ", secondary_unique_keys
#FINISH STEP 3
print "=== STEP 4: ==="
party_records = list(self.db.party_records.find({'Unique_Key':{'$in': secondary_unique_keys}}))
print "Found", len(party_records), "records in party_records..."
secondary_parties = set()
for party_record in party_records:
if party_record['Party_type'] == 1:
secondary_party = Owner({
'name': self.normalize_str(party_record['Name']),
'addr1': self.normalize_str(party_record['Addr1']),
'addr2': self.normalize_str(party_record['Addr2']),
'city': party_record['City'],
'state': party_record['State'],
'zip': party_record['Zip']
})
secondary_parties.add(secondary_party)
print "Found", len(secondary_parties), "secondary parties..."
for secondary_party in secondary_parties:
print "Name:", secondary_party['name']
print secondary_parties
building.secondary = secondary_parties
#FINISH STEP 4
print "=== STEP 5: ==="
lot_records = list(self.db.lot_records.find({'Unique_key':{'$in': secondary_unique_keys}}))
print "Found", len(lot_records), "records in lot_records..."
cache_bbls = set([bbl])
for lot_record in lot_records:
secondary_bbl = BBL(lot_record['Borough'], lot_record['Block'], lot_record['Lot'])
cache_bbls.add(secondary_bbl)
secondary_bbls = cache_bbls.difference(set([bbl]))
print "These are secondary BBLs:"
for secondary_bbl in secondary_bbls:
print secondary_bbl
building.linkedbbls = secondary_bbls
#FINISH STEP 5
print "=== STEP 6: HPD ==="
hpd_records = list(self.db.hpd.find({'bbl':bbl_repr}))
print "Found", len(hpd_records), "records in hpd..."
#Get the first one
if len(hpd_records) == 0:
print "No HPD records!"
else:
hpd_record = hpd_records[0]
hpd_reg_id = hpd_record['RegistrationID']
#FINISH STEP 6
print "=== STEP 7: HPD Contacts ==="
hpd_contact_records = list(self.db.hpd_contact.find({'RegistrationID': hpd_reg_id}))
print "Found", len(hpd_contact_records), "records in hpd_contacts..."
hpd_parties = set()
for hpd_contact_record in hpd_contact_records:
hpd_party = Owner({
'name': str(hpd_contact_record['LastName']) + ', ' + str(hpd_contact_record['FirstName']),
'addr1': str(hpd_contact_record['BusinessHouseNumber']) + ' ' + str(hpd_contact_record['BusinessStreetName']),
'addr2': hpd_contact_record['BusinessApartment'],
'city': hpd_contact_record['BusinessCity'],
'state': hpd_contact_record['BusinessState'],
'zip': hpd_contact_record['BusinessZip'],
'description': hpd_contact_record['ContactDescription'],
'corporation_name': hpd_contact_record['CorporationName']
})
hpd_parties.add(hpd_party)
print hpd_parties
building.hpd = hpd_parties
#FINISH STEP 7
print "=== STEP 8: DOF Tax Bills ==="
tax_records = list(self.db.dof_taxes.find({'id-boro':borough, 'id-block':block, 'id-lot':lot}))
print "Found", len(tax_records), "records in DOF tax bills..."
#Extract information
tax_parties = set()
for tax_record in tax_records:
tax_party = Owner({
'name': str(tax_record['nm-recipient-1']) + ' ' + str(tax_record['nm-recipient-2']) + ' ' + str(tax_record['ad-name-attention']),
'addr1': str(tax_record['ad-street-no']) + ' ' + str(tax_record['ad-street-1']),
'addr2': tax_record['ad-street-2'],
'city': tax_record['ad-city'],
'state': tax_record['cd-addr-state'],
'zip': tax_record['cd-addr-zip']
})
tax_parties.add(tax_party)
print tax_parties
building.tax = tax_parties
#FINISH STEP 8
print "STEP 9: DOS"
#Need to do it fuzzily by matching name?
return building