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

Splitting multipolygons by 180th meridian #90

Merged
merged 3 commits into from
Oct 11, 2022

Conversation

mngr777
Copy link
Contributor

@mngr777 mngr777 commented Oct 10, 2022

Adds h3_cells_to_multi_polygon_wkb function (and PostGIS wrappers) that allows to split multipoligons returned by cellsToLinkedMultiPolygon by 180th meridian.

Standalone tool using the same algorithm: https://github.com/konturio/h3-split

I noticed that a boolean flag has been removed for h3_cell_to_boundary_wkb, but WKB version of h3_cells_to_multi_polygon also greatly simplifies the interface, so maybe it's better to keep the flag in case the user doesn't need to split?

Non-convex polygon with holes:

\set polygon '\'POLYGON((-170 12.5, 170 12.5, 170 7.5, -175 7.5, -175 2.5, 170 2.5, 170 -12.5, -170 -12.5, -170 -7.5, 175 -7.5, 175 -2.5, -170 -2.5, -170 12.5), (-176 11.5, -179 11.5, -179 8.5, -176 8.5, -176 11.5), (174 11.5, 171 11.5, 171 8.5, 174 8.5, 174 11.5), (174 -3.5, 171 -3.5, 171 -6.5, 174 -6.5, 174 -3.5),(-176 -8.5, -179 -8.5, -179 -11.5, -176 -11.5, -176 -8.5))\''::geometry
SELECT ST_AsText(h3_cells_to_multi_polygon_wkb(array(SELECT h3_polygon_to_cells(:polygon, 4)), true)::geometry);

s

Polygon at North Pole:

\set north '\'point(0 90)\''::geometry
SELECT ST_AsText(h3_cells_to_multi_polygon_wkb(array(SELECT h3_cell_to_children(h3_lat_lng_to_cell(:north, 2), 4)), true)::geometry);

north

@zachasme
Copy link
Owner

This looks great! And thanks for adding _geometry and _geography variants for h3_cells_to_multi_polygon.

I ended up removing flags in favor of GUC variables. Furthermore, based on input from @Komzpa in #79 (review), I figured it was best to always split the PostGIS types (including the raw WKB).

Could you update the PR to not include the boolean flag, and always split?

@zachasme zachasme self-assigned this Oct 11, 2022
@zachasme zachasme added the enhancement 🚀 New feature or request label Oct 11, 2022
@mngr777
Copy link
Contributor Author

mngr777 commented Oct 11, 2022

Updated.
Thank you for the explanation, I didn't see that discussion.

@zachasme zachasme merged commit 88c8bea into zachasme:main Oct 11, 2022
@zachasme
Copy link
Owner

Thank you for the contribution!

zachasme added a commit that referenced this pull request Oct 11, 2022
@mngr777
Copy link
Contributor Author

mngr777 commented Oct 11, 2022

@zachasme I'm also going to add aggregate versions of geometry/geography functions. Now that they don't have a second argument no C code is required to pass it.

@zachasme
Copy link
Owner

Interesting, I have not yet worked with aggregate functions. Would that be similar to ST_Extent in PostGIS, for example? What kind of aggregate functions can be made for h3 specifically?

@mngr777
Copy link
Contributor Author

mngr777 commented Oct 11, 2022

Yes, similar to ST_Extent. This is basically all it takes:

CREATE AGGREGATE h3_cells_to_multi_polygon_geometry(h3index) (
    sfunc = array_append,
    stype = h3index[],
    finalfunc = h3_cells_to_multi_polygon_geometry,
    parallel = safe
);

This allows to apply h3_cells_to_multi_polygon_geometry without explicitly creating an array:

select h3_cells_to_multi_polygon_geometry(h3) from (select h3_polygon_to_cells(:polygon, 4) as h3) as t;

Aggregate functions could be added for h3_compact_cells/h3_uncompact_cells since they take an array, not sure how it would work with h3_cells_to_multi_polygon. I think that's all.

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

Successfully merging this pull request may close these issues.

2 participants