Closed InnovativeInventor closed 3 years ago
Note: forcing the source and target geometries to be cropped to each other like so does not resolve the issue.
precincts["geometry"] = maup.crop_to(precincts, blocks)
blocks["geometry"] = maup.crop_to(blocks, precincts)
Additionally, cropping then dropping the cropped out blocks also does not resolve the issue:
blocks = blocks.drop(blocks[blocks.area==0].index)
This works. You need to BOTH drop the empty geometries and weight with respect to assignment.
import geopandas as gpd
import geopandas
import maup
import math
blocks = geopandas.read_file("zip://./examples/blocks.zip")
precincts = geopandas.read_file("zip://./examples/precincts.zip")
districts = geopandas.read_file("zip://./examples/districts.zip")
precincts["geometry"] = maup.crop_to(precincts, blocks)
blocks["geometry"] = maup.crop_to(blocks, precincts)
blocks = blocks[~blocks["geometry"].is_empty]
election_columns = ["PRES16D", "PRES16R"]
assignment = maup.assign(blocks, precincts)
blocks['assignment'] = assignment
weights = blocks.TOTPOP / assignment.map(blocks.TOTPOP.groupby(assignment).sum())
assert math.isclose(sum(weights),len(precincts))
prorated = maup.prorate(assignment, precincts[election_columns], weights)
blocks[election_columns] = prorated
print(blocks[election_columns].sum())
print(precincts[election_columns].sum())
forcing precinct TOTPOP to equal block TOTPOP doesn't resovle the issue: