def test_schwartzberg_comparison_esri_shapefile(self): shape_file = r"tests/tl_2014_55_sldl/tl_2014_55_sldl.shp" driver = ogr.GetDriverByName('ESRI Shapefile') data_source = driver.Open(shape_file, 0) if data_source is None: print ("Open failed.\n") sys.exit(1) layer = data_source.GetLayer() layer.ResetReading() compact = None dispersed = None for feature in layer: # feature field one is the district ID id = feature.GetField(1) # compact if id == "027": # the feature's geometry geom = feature.GetGeometryRef() geometry = geom.Clone() compact = District(id=27, geometry=geometry) # dispersed if id == "077": # the feature's geometry geom = feature.GetGeometryRef() geometry = geom.Clone() dispersed = District(id=77, geometry=geometry) compact_score = schwartzberg(compact) dispersed_score = schwartzberg(dispersed) self.assertTrue(compact_score > dispersed_score)
def test_schwartzberg_square_3857(self): # A square around Lake Winnebago geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9839815.088179024,5505529.83629639],[-9881396.831566159,5468840.062719505],[-9844707.057989275,5427258.31933237],[-9803125.31460214,5463948.092909254],[-9839815.088179024,5505529.83629639]]]}') district = District(geometry=geom) radius = math.sqrt(1/math.pi) circumference = 2*math.pi*radius schwartzberg_score = 1/(4/circumference) self.assertAlmostEqual(schwartzberg_score, schwartzberg(district), places=5)
def test_schwartzberg_square_4326(self): # A square around Lake Merritt: From PlanScore geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-122.2631266, 37.7987797], [-122.2631266, 37.8103489], [-122.2484841, 37.8103489], [-122.2484841, 37.7987797], [-122.2631266, 37.7987797]]]}') district = District(geometry=geom) radius = math.sqrt(1/math.pi) circumference = 2*math.pi*radius schwartzberg_score = 1/(4/circumference) self.assertAlmostEqual(schwartzberg_score, schwartzberg(district), places=5)
def test_schwartzberg_triangle_3857(self): # An equilateral triangle around Lake Mendota geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9942183.378309947,5335705.868703798],[-9966678.038775941,5335248.39511508],[-9954034.524793552,5314264.133688814],[-9942183.378309947,5335705.868703798]]]}') district = District(geometry=geom) area_of_triangle = math.sqrt(3)/4 radius = math.sqrt(area_of_triangle/math.pi) circumference = 2*math.pi*radius schwartzberg_score = 1/(3/circumference) self.assertAlmostEqual(schwartzberg_score, schwartzberg(district), places=5)
""" convex_hull_scores = [] for district in district_plan: score = convex_hull_ratio(district) convex_hull_scores.append(score) print("District: {}\tConvex Hull Ratio: {}".format(district.id, score)) print("Average Convex Hull Ratio: {}".format( statistics.mean(convex_hull_scores))) """ Schwartzberg for each district and the district plan average """ print("\n\n") schwartzberg_scores = [] for district in district_plan: score = schwartzberg(district) schwartzberg_scores.append(score) print("District: {}\tSchwartzberg: {}".format(district.id, score)) print("Average Schwartzberg: {}".format(statistics.mean(schwartzberg_scores))) """ Polsby_Popper for each district and the district plan average """ print("\n\n") polsby_popper_scores = [] for district in district_plan: score = polsby_popper(district) polsby_popper_scores.append(score) print("District: {}\tPolsby-Popper: {}".format(district.id, score)) print("Average Polsby-Popper: {}".format(
def state_assembly(filename): shape_file = filename driver = ogr.GetDriverByName('ESRI Shapefile') data_source = driver.Open(shape_file, 0) if data_source is None: print("Open failed.\n") sys.exit(1) layer = data_source.GetLayer() layer.ResetReading() district_plan = [] office = 'State Assembly' for feature in layer: # feature field one is the district ID ida = feature['District_N'] # the feature's geometry geom = feature.GetGeometryRef() geometry = geom.Clone() pre_districts = Pre_District.objects.filter( district_no=int(ida), office__contains='Assembly').values('population') red_votes = Pre_District.objects.filter( district_no=int(ida), office__contains='Assembly').values('red_votes') blue_votes = Pre_District.objects.filter( district_no=int(ida), office__contains='Assembly').values('blue_votes') total_votes = Pre_District.objects.filter( district_no=int(ida), office__contains='Assembly').values('total_votes') total_votes = total_votes[0]['total_votes'] party_votes = { 'rep': int(red_votes[0]['red_votes']), 'dem': int(blue_votes[0]['blue_votes']) } district_plan.append( D(id=int(ida), geometry=geometry, population=pre_districts[0]['population'], party_votes=party_votes, votes=int(total_votes))) total_polsby_popper = 0 total_sch = 0 total_hull = 0 ## Django Stuff for district in district_plan: polsby_popper_score = polsby_popper(district) sch_score = schwartzberg(district) hull_score = convex_hull_ratio(district) total_polsby_popper = total_polsby_popper + polsby_popper_score total_sch = total_sch + sch_score total_hull = total_hull + hull_score district_model = District(district_no=district.id, office=office, polsby_popper=polsby_popper_score, shwartzberg=sch_score, convex_hull_ratio=hull_score) district_model.save() #district plan avg_polsby = total_polsby_popper / 99.0 avg_sch = total_sch / 99.0 avg_hull = total_hull / 99.0 efficiency = efficiency_gap(district_plan, 'dem', 'rep') mean_median = mean_median_diff(district_plan, 'dem', 'rep') equi_pop = districts_in_percent_deviation(district_plan, 10.0) district_plan_model = District_Plan(name=office, year=2016, avg_polsby_popper=avg_polsby, avg_schwartzberg=avg_sch, avg_convex_hull=avg_hull, equal_population=equi_pop, efficiency_gap=efficiency, mean_median_diff=mean_median) district_plan_model.save()