Skip to content

Raise an error on creation of MultiPoint or LineString with empty components#898

Merged
sgillies merged 5 commits intoshapely:maint-1.7from
tomplex:guard-empty-parts-of-multi
May 7, 2020
Merged

Raise an error on creation of MultiPoint or LineString with empty components#898
sgillies merged 5 commits intoshapely:maint-1.7from
tomplex:guard-empty-parts-of-multi

Conversation

@tomplex
Copy link
Copy Markdown
Contributor

@tomplex tomplex commented Apr 16, 2020

Closes #836.

As of Shapely 1.7, asking for the WKT of a MultiPoint or -LineString with an empty component results in a segfault. I figured it made more sense to error in that case and let the user take care of the issue - realistically I can't see much utility in having a MultiPoint or -LineString with an empty component anyway, and I don't think you can create such a geometry through WKT, WKB, or GeoJSON, so it doesn't make sense to allow it through the geometry object constructor, either.

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 16, 2020

Coverage Status

Coverage increased (+0.05%) to 81.77% when pulling ba295be on tomplex:guard-empty-parts-of-multi into a34ec59 on Toblerity:maint-1.7.

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Apr 16, 2020

I can't for the life of me figure out why adding 4 (tested) lines of code reduced test coverage by half a percent.

geom, ndims = linestring.geos_linestring_from_py(obs[l])

if lgeos.GEOSisEmpty(geom):
raise ValueError("Can't create MultiLineString with empty component")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

@tomplex tomplex Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea - thanks for sharing that book! It definitely makes sense from a library point of view. You mentioned in #836 you'd rather this be a warning and skip the empty geometry instead of raise-ing. Would you prefer if I re-worked this to do that instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After more thought, I think an exception is the right approach. GEOS is going to crash otherwise, so an exception only helps.

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Apr 28, 2020

While I'm in here, in multilinestring.py and multipoint.py there are ValueErrors which are raising for invalid coordinate dimensionality, when we have DimensionError in shapely.errors. Should those be changed (here, or in another PR)? Or would an error change need a warning of some sort because it could be breaking?

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Apr 28, 2020

Also, I'd be happy to move the relevant tests to pytest if that's desirable, but I don't want to include excess or unnecessary change.

@sgillies
Copy link
Copy Markdown
Contributor

@tomplex let's deal with the existing exceptions in a different PR. Would be a breaking change, a small one.

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented May 5, 2020

I think this is functionally "done" but I'm still not sure why coverage was reduced. I'll try and take a closer look this evening.

@sgillies
Copy link
Copy Markdown
Contributor

sgillies commented May 6, 2020

Don't sweat the little coverage changes, it's not entirely reliable. There's an issue with using assertRegex with python 2.7 though.

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented May 6, 2020

Can't believe I missed that...I'll fix that this evening.

@sgillies
Copy link
Copy Markdown
Contributor

sgillies commented May 7, 2020

🎉

@sgillies sgillies added this to the 1.7.1 milestone May 7, 2020
@sgillies sgillies merged commit b2f6d8c into shapely:maint-1.7 May 7, 2020
@tomplex tomplex deleted the guard-empty-parts-of-multi branch May 7, 2020 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants