forked from johnjreiser/NJParcelTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaxListParser.py
executable file
·378 lines (363 loc) · 16.3 KB
/
TaxListParser.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
#!/usr/bin/env python
# TaxListParser.py - State of New Jersey certified tax list parser
#
# last modified: 2014-05-09
# author: John Reiser <jreiser@njgeo.org>
# purpose: parses NJ MOD IV certified task lists from:
# http://www.state.nj.us/treasury/taxation/lpt/TaxListSearchPublicWebpage.shtml
# license: Copyright (C) 2014, John Reiser
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------
# 2014-05-09 revisions:
# - fixed source field, so it can be used in partitioned tables
# - fixed getField handling of dirty data so erroneous data will not cause an exception
import re, datetime
class TaxListParser:
"""convert a fixed-width string from MOD-IV to a dict"""
def __init__(self,record,source=None):
self.record = record
if not source == None:
self.source = source
self.numCheck = re.compile(r'[^\d.]+')
"""tags is a dict of tuples, with [0] as start, [1] as length, [2] as type ["string","int","date","float"], [3] as float format"""
self.tags = {"muncode": (0,4,0),
"block": (4,9,0),
"lot": (13,9,0),
"qual": (22,11,0),
"transaction_date": (37,6,2),
"transaction_update_number": (43,4,1),
"tax_account_number": (47,8,0),
"property_class": (55,3,0),
"property_location": (58,25,0),
"building_description": (83,15,0),
"land_description": (98,20,0),
"calc_acreage": (118,9,3,"5.4"), # formatted as 12345(.)6789
"additional_lots": (127,20,0),
"additional_lots2":(147,20,0),
"zoning": (167,4,0),
"tax_map_number": (171,4,0),
"owner_name": (175,35,0),
"owner_address": (210,25,0),
"owner_city": (235,25,0),
"owner_zip": (260,9,0),
"number_of_owners": (269,4,1),
"deduction": (273,6,1),
"bank_code": (280,5,0),
"mortgage_account_number": (285,10,0),
"deed_book": (295,5,0),
"deed_page": (300,5,0),
"sales_price_code": (305,1,0),
"sale_date": (306,6,2),
"sale_price":(312,9,1),
"sale_assessment":(321,9,1),
"sale_sr1a":(330,2,0),
"rebate_ssn":(332,9,0),
"rebate_spouse":(341,9,0),
"rebate_number_dwellings":(350,2,0),
"rebate_number_commercial":(352,2,0),
"rebate_multiple_occupancy":(354,1,0),
"rebate_percent_owned": (355,1,0),
"rebate_code": (356,2,0),
"rebate_delinquent": (358,1,0),
"exempt_own": (359,2,0),
"exempt_use": (361,2,0),
"exempt_desc":(363,3,0),
"exempt_initial_date":(366,6,2),
"exempt_further_date":(372,6,2),
"exempt_statute": (378,12,0),
"exempt_facility":(390,20,0),
"building_class":(410,5,0),
"year_constructed":(415,4,0),
"assessment_code":(419,1,0,1),
"land_value":(420,9,1),
"improvement_value": (429,9,1),
"net_value":(438,9,1),
"tax_code_1": (447,3,0),
"tax_code_2": (450,3,0),
"tax_code_3": (453,3,0),
"tax_code_4": (456,3,0),
"exemption_1_code":(459,1,0),
"exemption_1_amt": (460,8,1),
"exemption_2_code":(468,1,0),
"exemption_2_amt": (469,8,1),
"exemption_3_code":(477,1,0),
"exemption_3_amt": (478,8,1),
"exemption_4_code":(486,1,0),
"exemption_4_amt": (487,8,1),
"deduction_senior":(495,4,1),
"deduction_veteran":(499,4,1),
"deduction_widow":(503,4,1),
"deduction_surv_spouse":(507,3,1),
"deduction_disabled":(510,3,1),
"user_field_1":(513,4,0),
"user_field_2":(517,4,0),
"old_property_id":(521,29,0),
"census_tract":(550,5,0),
"census_block":(555,4,0),
"property_use_code":(559,3,0),
"property_flags":(562,10,0),
"tenant_response":(572,1,0),
"tenant_base_year":(573,4,0),
"tenant_base_tax":(577,9,3,"7.2"),
"tenant_base_net_val":(586,9,1),
"taxes_last_year":(600,9,3,"7.2"),
"taxes_current_year":(609,9,3,"7.2"),
"non_municipal_half1":(618,9,3,"7.2"),
"non_municipal_half2":(627,9,3,"7.2"),
"municipal_half1":(636,9,3,"7.2"),
"municipal_half2":(645,9,3,"7.2"),
"non_municipal_half3":(654,9,3,"7.2"),
"municipal_half3":(663,9,3,"7.2"),
"bill_status_flag":(672,1,0),
"tax_estimated_qtr3":(673,9,3,"7.2"),
"net_value_prior_year":(682,9,1),
"statement_aid_amt":(691,9,3,"7.2")
}
self.fields = ['pams_pin', 'source']
self.fields.extend(sorted(self.tags.keys()))
if(len(self.record) != 701):
self.record = self.record.rstrip("\r\n")
self.record += ("0"*(701-len(self.record)))
def getField(self,field):
if field in self.tags:
stri = self.tags[field][0]
endi = self.tags[field][1]+self.tags[field][0]
value = self.record[stri:endi].strip()
if(field == "block" or field == "lot"):
zeroStrip = re.compile(r'^[0]*(\w+$)') # this is a kludge and should probably be rewritten to match two groups of out the "00000SS00" format for decimalized blocks and lots
if(len(value) == 5):
return zeroStrip.sub(r'\1', value)
# return str(int(value)) # drop padded zeroes
else:
part = []
part.append(zeroStrip.sub(r"\1",value[:5]))
zeroStrip = re.compile(r'^[\s]*(\w+$)') # change the regex for the lot to only look for spaces.
part.append(zeroStrip.sub(r"\1",value[-4:]))
return ".".join(part)
# return str(int(value[:5]))+"."+value[-4:].strip() # return formatted decimal block/lot
elif(self.tags[field][2] == 1):
if(value != ''):
return int(value)
else:
return ''
elif(self.tags[field][2] == 2):
value = self.numCheck.sub('', value)
ds = '0000-00-00'
if(len(value) == 6):
if(value == '000000' or value == ' '):
return '0000-00-00'
else:
if(int(value[-2:]) > 13):
ds = "19%s-%s-%s" % (value[-2:],value[:2],value[2:4])
else:
ds = "20%s-%s-%s" % (value[-2:],value[:2],value[2:4])
else:
return "0000-00-00"
# return ds
return self.dateTest(ds)
elif(self.tags[field][2] == 3):
if(value == ''):
return 0
else:
try:
brk = self.tags[field][3].split(".")
if("." in value):
return float(value)
value = value[:int(brk[0])] + "." + value[-1*int(brk[1]):]
value = self.numCheck.sub("",value)
if(len(value) == 1):
return 0
else:
return float(value)
except:
print "record length:",len(self.record)
print "value:",value
print "break:",brk
raise
else:
return value.replace("\\", r"\\").replace("'","\\'")
elif(field == 'pams_pin'):
return self.getPAMSpin()
elif(field == 'source'):
return self.source
else:
return "invalid record"
raise
def getPAMSpin(self):
try:
mun = self.getField("muncode")
block = self.getField("block")
lot = self.getField("lot")
qual = self.getField("qual")
if(qual == ''):
return "_".join([mun, block, lot])
else:
return "_".join([mun, block, lot, qual])
except:
print self.record
raise
def genCreateTableMySQL(self, tablename):
text = "CREATE TABLE `" + tablename + "` ( `recordid` INT NOT NULL UNSIGNED AUTO_INCREMENT, \n" + \
"`pams_pin` VARCHAR(30),\n" + \
"`source` VARCHAR(20),\n" + \
"`timeinserted` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, \n"
for key in sorted(self.tags.iteritems()):
if(key[1][2] == 0):
text += "`%s` VARCHAR(%s),\n" % (key[0],str(key[1][1]))
elif(key[1][2] == 1):
text += "`%s` INT,\n" % (key[0])
elif(key[1][2] == 2):
text += "`%s` DATE,\n" % (key[0])
elif(key[1][2] == 3):
precision = key[1][3].split(".")
precision[0] = str(int(precision[0]) + int(precision[1]))
text += "`%s` DECIMAL(%s),\n" % (key[0], ",".join(precision))
text = text + "PRIMARY KEY (`recordid`));"
return text
def genInsertMySQL(self, table, source):
self.source = source
pams = self.getPAMSpin()
text = "INSERT INTO %s SET `pams_pin` = '%s', `source` = '%s', " % (table, pams, source)
for key in sorted(self.tags.iteritems()):
if(key[1][2] == 1 or key[1][2] == 2):
text += "`%s` = %s, " % (key[0], self.getField(key[0]))
else:
text += """`%s` = '%s', """ % (key[0], self.getField(key[0]))
text = text[:-2]
text += ";\n"
return text
def genCSVheader(self, df=None): # df: desired fields
if(self.source == None):
fields = ["pams_pin"]
else:
fields = ["pams_pin", "source"]
if df == None:
for key in sorted(self.tags.iteritems()):
if(key[0] in df) and (not key[0] in ("pams_pin", "source")):
fields.append( key[0] )
else:
for key in df:
if not key in ("pams_pin", "source"):
fields.append(key)
return ",".join( fields ) + "\n"
def genCSVrecord(self, fields):
if(self.source == "None"):
values = ['"{0}"'.format( self.getPAMSpin() )]
else:
values = ['"{0}"'.format( self.getPAMSpin() ), '"{0}"'.format( self.source )]
for f in fields:
if f in self.fields: ##self.tags.keys():
if f in ('pams_pin', 'source'):
pass ## ignore these two special fields, they're handled above
# if f == 'pams_pin':
# values.append( self.getPAMSpin() )
# if f == 'source':
# if(self.source.isdigit()):
# values.append( self.source )
# else:
# values.append( '"{0}"'.format( self.source ) )
else:
if(self.tags[f][2] == 0):
sv = str(self.getField(f))
sv = sv.replace('"', "'")
sv = sv.replace("\\'", "'")
sv = sv.replace("\\", "/")
values.append( '"{0}"'.format( sv ) )
elif(self.tags[f][2] == 2):
if(self.getField(f) == "0000-00-00"):
values.append( '' )
else:
values.append( '"{0}"'.format( str(self.getField(f))) )
else:
if self.getField(f) == None:
values.append( '' )
else:
values.append( str( self.getField(f) ).replace('"', "'") )
return ",".join(values) + "\n"
#### old alphanumerically sorted output below
def genCSVrecordAlpha(self, fields):
pams = self.getPAMSpin()
values = ['"{0}"'.format(pams)]
for key in sorted(self.tags.iteritems()):
if(key[0] in fields):
if(key[1][2] == 0):
values.append( '"{0}"'.format( str(self.getField(key[0])) ) )
elif(key[1][2] == 2):
if(self.getField(key[0]) == "0000-00-00"):
values.append( '' )
else:
values.append( '"{0}"'.format( str(self.getField(key[0])) ) )
else:
# print key[0]+":"+str(self.getField(key[0]))+" - "+str(type(self.getField(key[0])))
if self.getField(key[0]) == None:
values.append( '' )
else:
values.append( str( self.getField(key[0]) ) )
return ",".join(values) + "\n"
def genCreateTablePG(self, tablename, fields=None):
text = "CREATE TABLE " + tablename + " (recordid serial primary key, \n" + \
"pams_pin varchar(30), \n" + \
"source varchar(20), \n"
#### if fields parameter is not passed, all possible fields will be returned in alphabetical order
if(fields != None):
keysource = []
for f in fields:
if f in self.tags.keys():
keysource.append( [f, self.tags[f]] )
else:
keysource = sorted(self.tags.iteritems())
for key in keysource:
if(key[1][2] == 0):
text += "%s VARCHAR(%s),\n" % (key[0].replace("-","_"),str(key[1][1]))
elif(key[1][2] == 1):
text += "%s INT,\n" % (key[0].replace("-","_"))
elif(key[1][2] == 2):
text += "%s DATE,\n" % (key[0].replace("-","_"))
elif(key[1][2] == 3):
precision = key[1][3].split(".")
precision[0] = str(int(precision[0]) + int(precision[1]))
text += "%s DECIMAL(%s),\n" % (key[0].replace("-","_"), ",".join(precision))
text = text + "timeinserted timestamp default 'now' );"
return text
def getAllFields(self):
fs = {}
for key in self.fields:
fs[key] = self.getField(key)
return fs
def getAllFieldsTuple(self):
fs = []
for key in self.fields:
fs.append(self.getField(key))
return fs
def genExecuteManyInsert(self, tablename):
sql = r"INSERT INTO " + tablename + " ("
sql += ", ".join(map(lambda x: "`%s`"%x,self.fields)) # map/lambda to wrap keys in MySQL field delimiters
sql += ") VALUES (" + (r" %s,"*len(self.fields))[:-1]
sql += ")"
return sql
def dateTest(self, datestr):
dateOk = False
format = '%Y-%m-%d'
try:
result = datetime.datetime.strptime(datestr, format)
dateOk = (datestr == result.strftime(format)) # this makes sure the parsed date matches the original string
except:
# print "Date did not match: {0}".format(datestr)
pass
if dateOk:
return datestr
else:
return '0000-00-00'