Skip to content

Commit

Permalink
Fix TypeError when nothing is assigned by covering (#48)
Browse files Browse the repository at this point in the history
* Fix TypeError when unassigned is empty

* Coerce type to float if possible

* Add handling if nothing is assigned
  • Loading branch information
InnovativeInventor authored May 2, 2022
1 parent 6049d0b commit b138f7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 15 additions & 4 deletions maup/assign.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pandas

from .indexed_geometries import IndexedGeometries
from .intersections import intersections
from .crs import require_same_crs
Expand All @@ -9,11 +11,20 @@ def assign(sources, targets):
target that covers it, or, if no target covers the entire source, the
target that covers the most of its area.
"""
assignment = assign_by_covering(sources, targets)
unassigned = sources[assignment.isna()]
assignments_by_area = assign_by_area(unassigned, targets)
assignment.update(assignments_by_area)
assignment = pandas.Series(
assign_by_covering(sources, targets),
dtype="float"
)
assignment.name = None
unassigned = sources[assignment.isna()]

if len(unassigned): # skip if done
assignments_by_area = pandas.Series(
assign_by_area(unassigned, targets),
dtype="float"
)
assignment.update(assignments_by_area)

return assignment.astype(targets.index.dtype, errors="ignore")


Expand Down
7 changes: 5 additions & 2 deletions maup/indexed_geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ def assign(self, targets):
target_geometries.items(), len(target_geometries)
)
]
assignment = pandas.concat(groups).reindex(self.index)
return assignment
if groups:
return pandas.concat(groups).reindex(self.index)
else:
return geopandas.GeoSeries()


def enumerate_intersections(self, targets):
target_geometries = get_geometries(targets)
Expand Down

0 comments on commit b138f7f

Please sign in to comment.