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

Expose makeValid #90

Open
tdunning opened this issue Sep 6, 2021 · 5 comments
Open

Expose makeValid #90

tdunning opened this issue Sep 6, 2021 · 5 comments
Labels

Comments

@tdunning
Copy link

tdunning commented Sep 6, 2021

I see that there is a function LibGEOS.GEOSMakeValid in the generated code.

Unfortunately, when I call it like LibGEOS.GEOSMakeValid(Ref(p)) where p is a self-intersecting polygon, I get back a null pointer.

This would be a very nice function to have working, but I don't understand how call it (else I would have brought a pull request).

Is there any way to get this to work?

@jaakkor2
Copy link
Contributor

jaakkor2 commented Sep 8, 2021

Yes, doable. LibGEOS.GEOSMakeValid_r returns a pointer, maybe one should convert to Julia type based on LibGEOS.geomTypeId as the type of the output depends on the input.

makeValid(g) = LibGEOS.GEOSMakeValid_r(LibGEOS._context.ptr, g.ptr)
a = LibGEOS.Polygon([[[0.,0],[2,0],[2,2],[0,-1],[0,0]]])
isValid(a) # false
b = makeValid(a)
isValid(b) # true
LibGEOS.geomTypeId(b) # 6
writegeom(MultiPolygon(b))

outputs a multi-polygon

"MULTIPOLYGON (((0 0, 0.6666666666666667 0, 0 -1, 0 0)), ((2 0, 0.6666666666666667 0, 2 2, 2 0)))"

whereas a valid polygon results in a polygon

a2 = LibGEOS.Polygon([[[0.,0],[2,0],[2,2],[0,+1],[0,0]]])
isValid(a2) # true
b2 = makeValid(a2)
isValid(b2)  # true
LibGEOS.geomTypeId(b2) # 3
writegeom(Polygon(b2))

outputs

"POLYGON ((0 0, 2 0, 2 2, 0 1, 0 0))"

Not only polygons, many other geometry types are supported https://github.com/libgeos/geos/blob/main/src/operation/valid/MakeValid.cpp

@tdunning
Copy link
Author

tdunning commented Sep 8, 2021

Brilliant.

Not sure where I went wrong when guessing how to call this, but this is very helpful. I will take a look at it and see if I can package your one liner as a pull request.

@jaakkor2
Copy link
Contributor

jaakkor2 commented Sep 8, 2021

Tests could come from https://github.com/libgeos/geos/blob/main/tests/unit/capi/GEOSMakeValidTest.cpp

There seems to be functions not yet wrapped GEOSMakeValidParams_create, GEOSMakeValidParams_setKeepCollapsed, GEOSMakeValidWithParams, ... . Maybe just one user-facing function makeValid and keyword arguments for parameters.

@jaakkor2
Copy link
Contributor

jaakkor2 commented Sep 8, 2021

LibGEOS.geomFromGEOS is the function to go from GEOS to geom.
Better one-liner

makeValid(g) = LibGEOS.geomFromGEOS(LibGEOS.GEOSMakeValid_r(LibGEOS._context.ptr, g.ptr))

@tdunning
Copy link
Author

tdunning commented Sep 8, 2021 via email

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

No branches or pull requests

3 participants