mggg / maup

The geospatial toolkit for redistricting data.
https://maup.readthedocs.io/en/latest/
MIT License
67 stars 23 forks source link

Fix IndexedGeometries for Shapely==1.7.1, see #29 #30

Closed frnsys closed 3 years ago

frnsys commented 3 years ago

When using Shapely 1.7.1 IndexedGeometries.query fails because the assigned geom.index values don't persist.

Shapely documentation suggests building your own index:

    To get the original indices of the returned objects, create an
    auxiliary dictionary. But use the geometry *ids* as keys since
    the shapely geometry objects themselves are not hashable.

    >>> index_by_id = dict((id(pt), i) for i, pt in enumerate(points))
    >>> [(index_by_id[id(pt)], pt.wkt) for pt in tree.query(Point(2,2).buffer(1.0))]
    [(1, 'POINT (1 1)'), (2, 'POINT (2 2)'), (3, 'POINT (3 3)')]

The problem with this particular approach is that using id is unreliable--multiple objects may have the same id throughout the lifecycle of a program.

Instead I'm using a kind of ugly way to generate a hash for a given geometry.

frnsys commented 3 years ago

It looks like the issue is actually because of geopandas--adding the index attribute to geopandas geometries didn't persist. Creating a separate geometry list out of the geopandas geometries seems to work ok.

InnovativeInventor commented 3 years ago

I'm closing this PR due to https://github.com/mggg/maup/issues/29. Let me know if you want me to re-open this (or if you have another PR that you want me to look at)!