示例#1
0
    def test_polsby_popper_not_geometry(self):

        not_geometry = "Not a geometry"

        district = District(id=id, geometry=not_geometry)

        with self.assertRaises(AttributeError):
            polsby_popper(district)
示例#2
0
    def test_polsby_popper_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(math.pi/4, polsby_popper(district), places=5)
示例#3
0
    def test_polsby_popper_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(0., polsby_popper(district), places=3)
示例#4
0
    def test_polsby_popper_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(math.pi/4, polsby_popper(district), places=5)
示例#5
0
    def test_polsby_popper_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)
        triangle_score = (4.0*math.pi) * ((math.sqrt(3.0)/4.0)/9.0)

        self.assertAlmostEqual(triangle_score, polsby_popper(district), places=5)
示例#6
0
    def test_polsby_popper_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 = polsby_popper(compact)
        dispersed_score = polsby_popper(dispersed)

        self.assertTrue(compact_score > dispersed_score)
示例#7
0
"""
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(
    statistics.mean(polsby_popper_scores)))
"""
Equal population for the district plan
"""
print("\n\n\t----EQUAL POPULATION 2010 CENSUS DATA----\n")
print("Districts in 10% deviation: {}".format(
    districts_in_percent_deviation(district_plan, 10)))
print("Districts in 5% deviation: {}".format(
    districts_in_percent_deviation(district_plan, 5)))
print("Districts in 1% deviation: {}".format(
    districts_in_percent_deviation(district_plan, 1)))
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()
示例#9
0
    def test_polsby_popper_no_geometry(self):

        district = District()

        with self.assertRaises(TypeError):
            polsby_popper(district)
示例#10
0
    def test_polsby_popper_not_district(self):

        district = None

        with self.assertRaises(TypeError):
            polsby_popper(district)