forked from sc3/cook-convictions-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
882 lines (715 loc) · 29 KB
/
models.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
from datetime import datetime
import logging
import math
import re
import geopy.geocoders
from django.db import models
from django.db.models import Q
from django.contrib.gis.db import models as geo_models
from django.conf import settings
from django.contrib.gis.geos import Point
from model_utils.managers import PassThroughManager
from convictions_data.cleaner import CityStateCleaner, CityStateSplitter
from convictions_data.manager import (CensusPlaceManager,
CensusTractManager, CommunityAreaManager, DispositionManager)
from convictions_data.query import ConvictionQuerySet
from convictions_data.statute import (get_iucr, parse_statute, format_statute,
MultipleMatchingILCSError, ILCSLookupError, IUCRLookupError,
StatuteFormatError)
from convictions_data.signals import post_load_spatial_data
logger = logging.getLogger(__name__)
MAX_LENGTH=200
ZIPCODE_RE = re.compile(r'^\d{5}$')
class RawDisposition(models.Model):
"""Disposition record loaded verbatim from the raw CSV"""
# case_number is not unique
case_number = models.CharField(max_length=MAX_LENGTH)
sequence_number = models.CharField(max_length=MAX_LENGTH)
st_address = models.CharField(max_length=MAX_LENGTH)
city_state = models.CharField(max_length=MAX_LENGTH)
zipcode = models.CharField(max_length=MAX_LENGTH)
ctlbkngno = models.CharField(max_length=MAX_LENGTH)
fgrprntno = models.CharField(max_length=MAX_LENGTH)
statepoliceid = models.CharField(max_length=MAX_LENGTH)
fbiidno = models.CharField(max_length=MAX_LENGTH)
# Field in CSV is "DOB", not the lowercase "dob"
dob = models.CharField(max_length=MAX_LENGTH)
arrest_date = models.CharField(max_length=MAX_LENGTH)
initial_date = models.CharField(max_length=MAX_LENGTH)
sex = models.CharField(max_length=MAX_LENGTH)
statute = models.CharField(max_length=MAX_LENGTH)
chrgdesc = models.CharField(max_length=MAX_LENGTH)
chrgtype = models.CharField(max_length=MAX_LENGTH)
chrgtype2 = models.CharField(max_length=MAX_LENGTH)
chrgclass = models.CharField(max_length=MAX_LENGTH)
chrgdisp = models.CharField(max_length=MAX_LENGTH)
chrgdispdate = models.CharField(max_length=MAX_LENGTH)
ammndchargstatute = models.CharField(max_length=MAX_LENGTH)
ammndchrgdescr = models.CharField(max_length=MAX_LENGTH)
ammndchrgtype = models.CharField(max_length=MAX_LENGTH)
ammndchrgclass = models.CharField(max_length=MAX_LENGTH)
minsent = models.CharField(max_length=MAX_LENGTH)
maxsent = models.CharField(max_length=MAX_LENGTH)
amtoffine = models.CharField(max_length=MAX_LENGTH)
# Choices for validation of various fields
SEX_CHOICES = (
('male', 'Male'),
('female', 'Female'),
)
CHRGTYPE_CHOICES = (
('A', 'A'),
('C', 'C'),
('F', 'F'),
('M', 'M'),
('R', 'R'),
('T', 'T'),
('V', 'V'),
('Y', 'Y'),
)
CHRGTYPE_VALUES = [v for v,c in CHRGTYPE_CHOICES]
CHRGTYPE2_CHOICES = (
('2', '2'),
('3', '3'),
('Felony', 'Felony'),
('Misdemeanor', 'Misdemeanor'),
('R', 'R'),
('Traffic', 'Traffic'),
('V', 'V'),
('Y', 'Y'),
)
CHRGCLASS_CHOICES = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('A', 'A'),
('B', 'B'),
('C', 'C'),
# 'D' is only seen in ammndchrgclass
('D', 'D'),
# 'F' is only seen in ammndchrgclass
('F', 'F'),
('G', 'G'),
('M', 'M'),
('N', 'N'),
# 'O' is only seen in ammndchrgclass
('O', 'O'),
('P', 'P'),
('T', 'T'),
('U', 'U'),
('X', 'X'),
('Z', 'Z'),
)
CHRGCLASS_VALUES = [v for v,c in CHRGCLASS_CHOICES]
class Disposition(models.Model):
"""Disposition record with cleaned/transformed data"""
raw_disposition = models.ForeignKey(RawDisposition)
# ID Fields
case_number = models.CharField(max_length=MAX_LENGTH, db_index=True)
sequence_number = models.CharField(max_length=MAX_LENGTH)
ctlbkngno = models.CharField(max_length=MAX_LENGTH)
fgrprntno = models.CharField(max_length=MAX_LENGTH)
statepoliceid = models.CharField(max_length=MAX_LENGTH)
fbiidno = models.CharField(max_length=MAX_LENGTH)
dob = models.DateField(null=True)
st_address = models.CharField(max_length=MAX_LENGTH)
city = models.CharField(max_length=MAX_LENGTH)
state = models.CharField(max_length=2)
zipcode = models.CharField(max_length=5)
county = models.CharField(max_length=80, default="")
arrest_date = models.DateField(null=True)
initial_date = models.DateField(null=True, db_index=True)
chrgdispdate = models.DateField(null=True, db_index=True)
sex = models.CharField(max_length=10, choices=SEX_CHOICES)
# Fields that describe the charges
statute = models.CharField(max_length=50, help_text=("The statutory or local "
"ordinance citation for the offense which the defendant was "
"convicted."))
chrgdesc = models.CharField(max_length=50, help_text="Initial Charge Description")
chrgtype = models.CharField(max_length=1, choices=CHRGTYPE_CHOICES)
chrgtype2 = models.CharField(max_length=15, choices=CHRGTYPE2_CHOICES)
chrgclass = models.CharField(max_length=1, choices=CHRGCLASS_CHOICES)
chrgdisp = models.CharField(max_length=30)
ammndchargstatute = models.CharField(max_length=50)
ammndchrgdescr = models.CharField(max_length=50)
ammndchrgtype = models.CharField(max_length=1, choices=CHRGTYPE_CHOICES)
ammndchrgclass = models.CharField(max_length=1, choices=CHRGCLASS_CHOICES)
minsent_years = models.IntegerField(null=True)
minsent_months = models.IntegerField(null=True)
minsent_days = models.IntegerField(null=True)
minsent_life = models.BooleanField(default=False)
minsent_death = models.BooleanField(default=False)
maxsent_years = models.IntegerField(null=True)
maxsent_months = models.IntegerField(null=True)
maxsent_days = models.IntegerField(null=True)
maxsent_life = models.BooleanField(default=False)
maxsent_death = models.BooleanField(default=False)
amtoffine = models.IntegerField(null=True)
final_statute = models.CharField(max_length=50, default="",
help_text=("Field to make querying easier. Set to the value of "
"ammndchargstatute if present, otherwise set to the value "
"of statute"),
db_index=True)
final_statute_formatted = models.CharField(max_length=50, default="",
db_index=True,
help_text=("Value from final_statute but parsed and reformatted "
"to try to normalize the formats and make grouping "
"queries easier"))
final_chrgdesc = models.CharField(max_length=50, default="", db_index=True)
final_chrgtype = models.CharField(max_length=1, choices=CHRGTYPE_CHOICES,
default="", db_index=True)
final_chrgclass = models.CharField(max_length=1, choices=CHRGCLASS_CHOICES,
default="", db_index=True)
iucr_code = models.CharField(max_length=4, default="", db_index=True)
iucr_category = models.CharField(max_length=50, default="", db_index=True)
# Spatial fields
lat = models.FloatField(null=True)
lon = models.FloatField(null=True)
community_area = models.ForeignKey('CommunityArea', null=True,
on_delete=models.SET_NULL)
place = models.ForeignKey('CensusPlace', null=True, on_delete=models.SET_NULL)
conviction = models.ForeignKey('Conviction', null=True,
on_delete=models.SET_NULL)
# Use a custom manager to add geocoding methods
objects = DispositionManager()
def __init__(self, *args, **kwargs):
super(Disposition, self).__init__(*args, **kwargs)
if self.pk is None:
# New model, populate it's fields by parsing the values from
self.load_from_raw()
def geocode(self, geocoder_cls=geopy.geocoders.OpenMapQuest):
geocoder = geocoder_cls(
api_key=settings.CONVICTIONS_GEOCODER_API_KEY,
)
address, (self.lat, self.lon) = geocoder.geocode(self.geocoder_address)
return self
@property
def geocoder_address(self):
if not self.st_address:
raise ValueError("Need an address to geocode")
bits = [self.st_address]
if self.zipcode:
bits.append(self.zipcode)
elif self.city and self.state:
bits.append(self.city)
bits.append(self.state)
else:
raise ValueError("Need a zipcode or city and state to geocode")
return ",".join(bits)
def load_from_raw(self):
"""Load fields from related RawDisposition model"""
for field_name in RawDisposition._meta.get_all_field_names():
if field_name == "disposition":
# Skip reverse name on related field
continue
self.load_field_from_raw(field_name)
self.load_final_fields()
return self
def load_field_from_raw(self, field_name):
val = getattr(self.raw_disposition, field_name)
try:
loader = getattr(self, "_load_field_{}".format(field_name))
loader(val)
except AttributeError:
try:
parser = getattr(self, "_parse_{}".format(field_name))
val = parser(val)
except AttributeError:
pass
except ValueError as e:
msg = ("Error when parsing '{}' from RawDisposition with case "
"number '{}': {}")
msg = msg.format(field_name, self.raw_disposition.case_number, e)
logger.warning(msg)
if 'date' in field_name or field_name == 'dob':
val = None
setattr(self, field_name, val)
return self
def load_final_fields(self):
self.load_final_statute_and_iucr(self.statute, self.ammndchargstatute)
self.load_final_field('final_chrgdesc', self.chrgdesc,
self.ammndchrgdescr)
self.load_final_field('final_chrgtype', self.chrgtype,
self.ammndchrgtype)
self.load_final_field('final_chrgclass', self.chrgclass,
self.ammndchrgclass)
return self
def load_final_field(self, fieldname, val1, val2):
val = val2 if val2 else val1
setattr(self, fieldname, val)
return self
def _load_field_city_state(self, val):
self.city, self.state = self._parse_city_state(val)
if not self.state:
self.state = self._detect_state(self.city)
assert len(self.state) <= 2, self._invalid_len_msg('state')
return self
def _load_field_minsent(self, val):
self.minsent_years, self.minsent_months, self.minsent_days, self.minsent_life, self.minsent_death = self._parse_sentence(val)
return self
def _load_field_maxsent(self, val):
self.maxsent_years, self.maxsent_months, self.maxsent_days, self.maxsent_life, self.maxsent_death = self._parse_sentence(val)
return self
def _load_field_statute(self, val):
self.statute = val
return self
def _load_field_chrgdesc(self, val):
self.chrgdesc = val
return self
def _load_field_chrgtype(self, val):
self.chrgtype = val
assert len(self.chrgtype) <= 1, self._invalid_len_msg('chrgtype')
return self
def _invalid_len_msg(self, attr):
val = getattr(self, attr)
return ("Invalid length for {} '{}' when loading from RawResult with "
"pk {}".format(attr, val, self.raw_disposition.pk))
def _load_field_chrgclass(self, val):
self.chrgclass = val
assert len(self.chrgclass) <= 1, self._invalid_len_msg('chrgclass')
return self
def _load_field_ammndchargstatute(self, val):
self.ammndchargstatute = val
return self
def load_final_statute_and_iucr(self, val1, val2):
"""Populate the final_statute, final_statute_formatted, iucr_code and
iucr_category fields from the value."""
self.load_final_field('final_statute', val1, val2)
try:
parsed_statute = parse_statute(self.final_statute)
except (StatuteFormatError, ILCSLookupError, MultipleMatchingILCSError) as e:
logger.warn(e)
# If we weren't able to parse the statute, return early
return self
else:
# We've parsed the statute. Format it, and save this value.
self.final_statute_formatted = format_statute(parsed_statute)
try:
offenses = get_iucr(parsed_statute)
if len(offenses) == 1:
self.iucr_code = offenses[0].code
self.iucr_category = offenses[0].offense_category
else:
logger.warn("Multiple matching IUCR offenses found for statute '{}'".format(self.final_statute))
except IUCRLookupError as e:
# HACK: The original error will have a nicely-formatted statute.
# Replace it with the raw statute value
logger.warn(IUCRLookupError(self.final_statute))
return self
def _load_field_ammndchrgdescr(self, val):
self.ammndchrgdescr = val
return self
def _load_field_ammndchrgtype(self, val):
self.ammndchrgtype = val
assert len(self.ammndchrgtype) <= 1, self._invalid_len_msg('ammndchrgtype')
return self
def _load_field_ammndchrgclass(self, val):
self.ammndchrgclass = val
assert len(self.ammndchrgclass) <= 1, self._invalid_len_msg('ammndchrgclass')
return self
def boundarize(self):
pnt = Point(self.lon, self.lat)
try:
self.community_area = CommunityArea.objects.get(boundary__contains=pnt)
self.save()
return self.community_area
except CommunityArea.DoesNotExist:
try:
self.place = CensusPlace.objects.get(boundary__contains=pnt)
self.save()
return self.place
except CensusPlace.DoesNotExist:
return False
@classmethod
def _parse_city_state(cls, city_state):
city, state = CityStateSplitter.split_city_state(city_state)
return CityStateCleaner.clean_city_state(city, state)
@classmethod
def _parse_zipcode(cls, zipcode):
zipcode = zipcode.strip()
if not ZIPCODE_RE.match(zipcode):
return ""
return zipcode
@classmethod
def _detect_state(cls, city):
# Check and see if the city name
# matches the name of a municipality in Cook County. If it does,
# set the state to IL.
q = Q(municipality_name__iexact=city) | Q(agency_name__iexact=city)
if Municipality.objects.filter(q).count():
return "IL"
return ""
@classmethod
def _parse_dob(cls, dob):
return cls._parse_date(dob)
@classmethod
def _parse_arrest_date(cls, arrest_date):
return cls._parse_date(arrest_date)
@classmethod
def _parse_initial_date(cls, date_string):
return cls._parse_date(date_string)
@classmethod
def _parse_chrgdispdate(cls, date_string):
return cls._parse_date(date_string)
@classmethod
def _parse_date(cls, date_string, date_format="%d-%b-%y"):
if not date_string:
return None
dt = datetime.strptime(date_string, date_format)
# Try to correctly interpret the two digit year.
if dt.year > datetime.now().year:
dt = datetime(dt.year - 100, dt.month, dt.day)
return dt.date()
@classmethod
def _parse_sex(cls, sex_string):
cln_sex_string = sex_string.strip()
if cln_sex_string and cln_sex_string.lower() not in ("male", "female"):
raise ValueError("Unexpected value '{}' for 'sex' field".format(
cln_sex_string))
return cln_sex_string.lower()
@classmethod
def _parse_chrgtype(cls, s):
cln_s = s.strip()
if cln_s == "Felony":
chrgtype = "F"
else:
chrgtype = cln_s
if chrgtype and chrgtype not in cls.CHRGTYPE_VALUES:
msg = "Unexpected value '{}'".format(chrgtype)
raise ValueError(msg)
return chrgtype
@classmethod
def _parse_chrgclass(cls, s):
if s and s not in cls.CHRGCLASS_VALUES:
msg = "Unexpected value '{}'".format(s)
raise ValueError(msg)
return s
@classmethod
def _parse_ammndchrgtype(cls, s):
return cls._parse_chrgtype(s)
@classmethod
def _parse_ammndchrgclass(cls, s):
return cls._parse_chrgclass(s)
@classmethod
def _parse_sentence(cls, s):
if s == "88888888":
return None, None, None, True, False
if s == "99999999":
return None, None, None, False, True
val = s.zfill(8)
year = int(val[0:3])
mon = int(val[3:5])
day = int(val[5:8])
return year, mon, day, False, False
@classmethod
def _parse_amtoffine(cls, s):
return cls._parse_int(s)
@classmethod
def _parse_int(cls, s):
if not s:
return None
return int(s)
@classmethod
def get_community_area_cache(cls):
try:
return cls._community_area_cache
except AttributeError:
cls._community_area_cache = {ca.id: ca for ca in CommunityArea.objects.all()}
return cls._community_area_cache
@classmethod
def get_place_cache(cls):
try:
return cls._place_cache
except AttributeError:
cls._place_cache = {ca.id: ca for ca in CensusPlace.objects.all()}
return cls._place_cache
@classmethod
def create_conviction(cls, **kwargs):
"""
Convenience method for creating a conviction from a disposition
This exists to avoid circular imports in DispositionQuerySet.
"""
return Conviction.objects.create(**kwargs)
class Conviction(models.Model):
case_number = models.CharField(max_length=MAX_LENGTH, db_index=True)
ctlbkngno = models.CharField(max_length=MAX_LENGTH)
fgrprntno = models.CharField(max_length=MAX_LENGTH)
statepoliceid = models.CharField(max_length=MAX_LENGTH)
fbiidno = models.CharField(max_length=MAX_LENGTH)
dob = models.DateField(null=True)
st_address = models.CharField(max_length=MAX_LENGTH)
city = models.CharField(max_length=MAX_LENGTH)
state = models.CharField(max_length=2)
zipcode = models.CharField(max_length=5)
county = models.CharField(max_length=80, default="")
sex = models.CharField(max_length=10, choices=SEX_CHOICES, db_index=True)
chrgdispdate = models.DateField(null=True)
final_statute = models.CharField(max_length=50, default="",
help_text=("Field to make querying easier. Set to the value of "
"ammndchargstatute if present, otherwise set to the value "
"of statute"),
db_index=True)
final_statute_formatted = models.CharField(max_length=50, default="",
db_index=True,
help_text=("Value from final_statute but parsed and reformatted "
"to try to normalize the formats and make grouping "
"queries easier"))
final_chrgdesc = models.CharField(max_length=50, default="", db_index=True)
final_chrgtype = models.CharField(max_length=1, choices=CHRGTYPE_CHOICES,
default="", db_index=True)
final_chrgclass = models.CharField(max_length=1, choices=CHRGCLASS_CHOICES,
default="", db_index=True)
iucr_code = models.CharField(max_length=4, default="", db_index=True)
iucr_category = models.CharField(max_length=50, default="", db_index=True)
community_area = models.ForeignKey('CommunityArea', null=True,
on_delete=models.SET_NULL)
place = models.ForeignKey('CensusPlace', null=True,
on_delete=models.SET_NULL)
objects = PassThroughManager.for_queryset_class(ConvictionQuerySet)()
def __str__(self):
return "{} {} {}".format(self.case_number, self.chrgdispdate, self.final_statute)
class Municipality(geo_models.Model):
"""
Cook County, Illinois municipality
Wraps spatial data set found at
https://datacatalog.cookcountyil.gov/GIS-Maps/ccgisdata-Municipality/ta8t-zebk
"""
agency_id = geo_models.IntegerField()
agency_name = geo_models.CharField(max_length=60)
municipality_name = geo_models.CharField(max_length=25)
st_area = geo_models.FloatField()
sde_length = geo_models.FloatField()
shape_area = geo_models.FloatField()
shape_length = geo_models.FloatField()
boundary = geo_models.MultiPolygonField()
objects = geo_models.GeoManager()
FIELD_MAPPING = {
'agency_id': 'AGENCY',
'agency_name': 'AGENCY_DES',
'municipality_name': 'MUNICIPALI',
'st_area': 'ST_AREA_SH',
'sde_length': 'SDELENGTH_',
'shape_area': 'SHAPE_area',
'shape_length': 'SHAPE_len',
'boundary': 'MULTIPOLYGON',
}
@property
def name(self):
if self.municipality_name:
return self.municipality_name
else:
return self.agency_name
def __str__(self):
return self.name
class CensusFieldsMixin(geo_models.Model):
# Census fields
total_population = geo_models.IntegerField(null=True)
total_population_moe = geo_models.IntegerField(null=True)
per_capita_income = geo_models.IntegerField(null=True,
help_text=("PER CAPITA INCOME IN THE PAST 12 MONTHS (IN 2010 "
"INFLATION-ADJUSTED DOLLARS)"))
per_capita_income_moe = geo_models.IntegerField(null=True)
class Meta:
abstract = True
class ConvictionsAggregateMixin(object):
@classmethod
def get_conviction_model(cls):
return Conviction
def most_common_statutes(self, count=10):
filter_kwargs = {}
filter_kwargs[self.get_conviction_related_field_name()] = self
return self.get_conviction_model().objects.filter(**filter_kwargs).most_common_statutes(count)
GEOJSON_FIELDS_BASE = [
'name',
'total_population',
'num_convictions',
'convictions_per_capita',
'num_homicides',
'num_affecting_women',
'affecting_women_per_capita',
'boundary',
]
class CommunityArea(ConvictionsAggregateMixin, CensusFieldsMixin, geo_models.Model):
"""
Chicago Community Area
Wraps
https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas-current-/cauq-8yn6
"""
number = geo_models.IntegerField()
name = geo_models.CharField(max_length=80)
shape_area = geo_models.FloatField()
shape_len = geo_models.FloatField()
boundary = geo_models.MultiPolygonField()
objects = CommunityAreaManager()
FIELD_MAPPING = {
'number': 'AREA_NUMBE',
'name': 'COMMUNITY',
'shape_area': 'SHAPE_AREA',
'shape_len': 'SHAPE_LEN',
'boundary': 'MULTIPOLYGON',
}
GEOJSON_FIELDS = GEOJSON_FIELDS_BASE + [
'number',
]
def __str__(self):
return self.name
def aggregate_census_fields(self):
fields = ('total_population', 'per_capita_income')
for field in fields:
self.aggregate_census_field(field)
def aggregate_census_field(self, field):
moe_field = field + "_moe"
aggregate = 0
aggregate_moe = 0
for tract in self.censustract_set.all():
val = getattr(tract, field)
if val is not None:
aggregate += val
moe = getattr(tract, moe_field)
aggregate_moe += moe**2
aggregate_moe = math.sqrt(aggregate_moe)
setattr(self, field, aggregate)
setattr(self, moe_field, aggregate_moe)
return aggregate, aggregate_moe
@classmethod
def get_conviction_related_column_name(cls):
return 'community_area_id'
@classmethod
def get_conviction_related_field_name(cls):
return 'community_area'
class CensusTract(CensusFieldsMixin, geo_models.Model):
"""
Census Tract
Wraps
https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Census-Tracts-2010/5jrd-6zik
"""
statefp10 = geo_models.CharField(max_length=2)
countyfp10 = geo_models.CharField(max_length=3)
tractce10 = geo_models.CharField(max_length=6)
geoid10 = geo_models.CharField(max_length=11, db_index=True)
name = geo_models.CharField(max_length=7, db_index=True)
community_area_number = geo_models.IntegerField()
notes = geo_models.CharField(max_length=80)
# Spatial fields
boundary = geo_models.MultiPolygonField()
community_area = geo_models.ForeignKey(CommunityArea, null=True,
on_delete=models.SET_NULL)
objects = CensusTractManager()
FIELD_MAPPING = {
'statefp10': 'STATEFP10',
'countyfp10': 'COUNTYFP10',
'tractce10': 'TRACTCE10',
'geoid10': 'GEOID10',
'name': 'NAME10',
'community_area_number': 'COMMAREA_N',
'notes': 'NOTES',
'boundary': 'MULTIPOLYGON',
}
def __str__(self):
return self.geoid10
@classmethod
def get_community_area_model(cls):
return CommunityArea
class CensusPlace(ConvictionsAggregateMixin, CensusFieldsMixin, geo_models.Model):
"""
Census Place
Wraps TIGER Shapefile http://www2.census.gov/geo/tiger/TIGER2010/PLACE/2010/tl_2010_17_place10.zip
"""
# From ShapeFile
statefp10 = geo_models.CharField(max_length=2)
placefp10 = geo_models.CharField(max_length=5)
placens10 = geo_models.CharField(max_length=8)
geoid10 = geo_models.CharField(max_length=11, db_index=True)
name = geo_models.CharField(max_length=100, db_index=True)
namelsad10 = geo_models.CharField(max_length=100)
lsad10 = geo_models.CharField(max_length=2)
classfp10 = geo_models.CharField(max_length=2)
pcicbsa10 = geo_models.CharField(max_length=1)
pcinecta10 = geo_models.CharField(max_length=1)
mtfcc10 = geo_models.CharField(max_length=5)
funcstat10 = geo_models.CharField(max_length=1)
aland10 = geo_models.FloatField()
awater10 = geo_models.FloatField()
intptlat10 = geo_models.CharField(max_length=11)
intptlon10 = geo_models.CharField(max_length=12)
# Custom Fields
in_chicago_msa = geo_models.BooleanField(default=False,
help_text=("Is this place within one of the counties that is part "
"of Chicago's Metropolitan Statistical Area: Cook, DeKalb, "
"DuPage, Grundy, Kane, Kendall, McHenry, Will, Lake"))
in_cook_county = geo_models.BooleanField(default=False,
help_text="Is this palce within Cook County?")
# Spatial fields
boundary = geo_models.MultiPolygonField()
objects = CensusPlaceManager()
FIELD_MAPPING = {
'placefp10': 'PLACEFP10',
'placens10': 'PLACENS10',
'geoid10': 'GEOID10',
'name': 'NAME10',
'namelsad10': 'NAMELSAD10',
'lsad10': 'LSAD10',
'classfp10': 'CLASSFP10',
'pcicbsa10': 'PCICBSA10',
'pcinecta10': 'PCINECTA10',
'mtfcc10': 'MTFCC10',
'funcstat10': 'FUNCSTAT10',
'aland10': 'ALAND10',
'awater10': 'AWATER10',
'intptlat10': 'INTPTLAT10',
'intptlon10': 'INTPTLON10',
'boundary': 'MULTIPOLYGON',
}
GEOJSON_FIELDS = GEOJSON_FIELDS_BASE
def __str__(self):
return self.name
@classmethod
def get_conviction_related_column_name(cls):
return 'place_id'
@classmethod
def get_conviction_related_field_name(cls):
return 'place'
class County(geo_models.Model):
statefp10 = geo_models.CharField(max_length=2)
countyfp10 = geo_models.CharField(max_length=3)
countyns10 = geo_models.CharField(max_length=8)
geoid10 = geo_models.CharField(max_length=5)
name = geo_models.CharField(max_length=100)
namelsad10 = geo_models.CharField(max_length=100)
lsad10 = geo_models.CharField(max_length=2)
classfp10 = geo_models.CharField(max_length=2)
mtfcc10 = geo_models.CharField(max_length=5)
csafp10 = geo_models.CharField(max_length=3)
cbsafp10 = geo_models.CharField(max_length=5)
metdivfp10 = geo_models.CharField(max_length=5)
funcstat10 = geo_models.CharField(max_length=1)
aland10 = geo_models.FloatField()
awater10 = geo_models.FloatField()
intptlat10 = geo_models.CharField(max_length=11)
intptlon10 = geo_models.CharField(max_length=12)
geom = geo_models.MultiPolygonField()
objects = geo_models.GeoManager()
FIELD_MAPPING = {
'statefp10' : 'STATEFP10',
'countyfp10' : 'COUNTYFP10',
'countyns10' : 'COUNTYNS10',
'geoid10' : 'GEOID10',
'name' : 'NAME10',
'namelsad10' : 'NAMELSAD10',
'lsad10' : 'LSAD10',
'classfp10' : 'CLASSFP10',
'mtfcc10' : 'MTFCC10',
'csafp10' : 'CSAFP10',
'cbsafp10' : 'CBSAFP10',
'metdivfp10' : 'METDIVFP10',
'funcstat10' : 'FUNCSTAT10',
'aland10' : 'ALAND10',
'awater10' : 'AWATER10',
'intptlat10': 'INTPTLAT10',
'intptlon10': 'INTPTLON10',
'geom': 'MULTIPOLYGON',
}
def handle_post_load_spatial_data(sender, **kwargs):
if kwargs['model'] == CensusTract:
CensusTract.objects.set_community_area_relations()
post_load_spatial_data.connect(handle_post_load_spatial_data)