Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Implement a "finer polyfill" #6

Open
DahnJ opened this issue Jun 29, 2021 · 3 comments
Open

ENH: Implement a "finer polyfill" #6

DahnJ opened this issue Jun 29, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@DahnJ
Copy link
Owner

DahnJ commented Jun 29, 2021

A common use-case is to generate all hexagons that intersect with a given polygon. That's not straightforward to do using H3.

One approximate solution is to first generate a finer resolution grid using polyfill and then return to the desired resolution using h3_to_parent.

The H3-Pandas API could thus contain a convenience function that performs both of these operations in a single pass.

A quick demonstration of the idea:

import geopandas as gpd
import h3pandas

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

# Resample to H3 cells
gdf = gdf.h3.polyfill_resample(4)
gdf = gdf.h3.h3_to_parent_aggregate(2)

Sidenote

The above code yields a "strange" result:

image

This is because a number of the hexagons cross the anti-meridian, and get rendered wrongly. Since this case might be reasonably common, H3-Pandas could perhaps provide a fix, something like

import numpy as np
from shapely.geometry import Polygon

def fix(cell):
    cell_coords = np.array(cell.boundary.coords)
    lngs = cell_coords[:, 0]
    if not (np.any(lngs < 0) and np.any(lngs > 0)) or not (np.any(np.abs(lngs) > 170)):
        return cell
    
    negative = lngs < 0
    lngs[negative] = lngs[negative] + 360
    return Polygon(cell_coords)

Applying the fix to the dataframe's geometries

gdf['geometry'] = gdf['geometry'].apply(fix)

fixes the visual output

image

@DahnJ DahnJ changed the title Implement a "finer polyfill" ENH: Implement a "finer polyfill" Jun 29, 2021
@DahnJ DahnJ added the enhancement New feature or request label Jun 29, 2021
@Rimasko
Copy link

Rimasko commented Apr 6, 2022

Please can you help. For which object to use the fix function?
when i use it for gdf, i get an error
AttributeError: 'GeoSeries' object has no attribute 'coords'

@DahnJ
Copy link
Owner Author

DahnJ commented Apr 7, 2022

Please can you help. For which object to use the fix function? when i use it for gdf, i get an error AttributeError: 'GeoSeries' object has no attribute 'coords'

The fix must be applied to the geometries themselves. I have added the relevant line to my post above.

@andresfchamorro
Copy link

Hi @DahnJ

Long time on this issue but..

I've modified polyfill with the idea suggested above (not sure if you were planning on making changes).

This solution below does two things:

  1. If a given polygon is too small (returns no h3 cells), check iteratively with higher resolutions and return the parent cells at the res requested.
  2. Add option for overfill returning also the outer edge cases using h3.k_ring.

I can submit a PR if you think this could be a useful change!

andresfchamorro@c4a7852

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants