def test_convex_hull_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) self.assertAlmostEqual(1.0, convex_hull_ratio(district), places=5)
def test_convex_hull_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) self.assertAlmostEqual(1.0, convex_hull_ratio(district), places=5)
def test_convex_hull_line_4326(self): # A thin line through Lake Merritt: From PlanScore geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-122.2631266, 37.804111], [-122.2631266, 37.804112], [-122.2484841, 37.804112], [-122.2484841, 37.804111], [-122.2631266, 37.804111]]]}') district = District(geometry = geom) self.assertAlmostEqual(1.0, convex_hull_ratio(district), places=3)
def test_convex_hull_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) self.assertAlmostEqual(1.0, convex_hull_ratio(district), places=5)
def test_convex_hull_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 == "019": # the feature's geometry geom = feature.GetGeometryRef() geometry = geom.Clone() dispersed = District(id=19, geometry=geometry) compact_score = convex_hull_ratio(compact) dispersed_score = convex_hull_ratio(dispersed) self.assertTrue(compact_score > dispersed_score)
def test_convex_hull_star_3857(self): # A star in the continental 48 star = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-12146761.038853927,5875255.742111786],[-11809365.518752303,5017520.836898427],[-12525054.307214756,4436715.824286485],[-11613536.329536539,4300041.463210875],[-11468388.828200482,3389834.284892711],[-10894266.370623887,4110894.8290304607],[-10033430.080825375,3781492.6633242397],[-10370825.600927,4639227.568537598],[-9655136.812464546,5220032.581149541],[-10566654.790142763,5356706.942225151],[-10711802.291478822,6266914.120543315],[-11285924.749055414,5545853.576405566],[-12146761.038853927,5875255.742111786]]]}') area = star.GetArea() hexagon = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-12146761.038853927,5875255.742111786],[-12525054.307214756,4436715.824286485],[-11468388.828200482,3389834.284892711],[-10033430.080825375,3781492.6633242397],[-9655136.812464546,5220032.581149541],[-10711802.291478822,6266914.120543315],[-12146761.038853927,5875255.742111786]]]}') convex_hull = hexagon.GetArea() district = District(geometry=star) self.assertAlmostEqual((area/convex_hull), convex_hull_ratio(district), places=5)
district.party_votes = {'dem': 137870, 'rep': 213459} elif district.id == "8": district.population = 706840 district.party_votes = {'dem': 142678, 'rep': 207621} """ Demonstrating libdistrict functions starts here """ print("\nCalculating Scores for 2011 Congressional District Plan") print("\n\t----COMPACTNESS----\n") """ Convex Hull Ratio for each district and the district plan average """ 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))
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()