shapely / shapely/shapely

set_precision() can cause negative buffer on polygon to fail

Open
#1,960 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

geos upstream bug
Dominant language
Python
Stars
4.5k
Forks
631
Avg merge
1d 11h
Merged PRs (30d)
10

Description

Edited to include OS, shapely versions, etc.

Me again, with another unexpected set_precisionside-effect (I'm working on some low-level geometry stuff and have run into a bunch of these as reported in issues #1947, #1950, and #1952.)

Expected behavior and actual behavior

Setting precision on a polygon should not affect the correct working of a negative buffer, but in some instances (example below) it does.

This one is particularly strange and unpredictable. I tried to make an MRE with a nice simple square (0, 0) to (100, 100), but that didn't work, so I am presenting it here with a polygon that fails on my system. It's a cairo polygon for anyone interested.

Steps to reproduce the problem

import shapely.ops
import shapely.geometry

p = shapely.geometry.Polygon(
  [(-250.0, 0.0), (-197.168784, 197.168784),
   (0.0, 250.0), (52.831216, 52.831216),
   (-52.831216, -52.831216), (-250.0, 0.0)])
p = shapely.set_precision(p, grid_size = 1e-6)
p = p.buffer(-1e-5)
print(p)

which yields an empty polygon:

POLYGON EMPTY

The buffer does not fail at all sizes, and it's not predictable which sizes it will fail at: 1e-6 works fine, 1e-7 fails) For the most part very small buffers seem to fail, while larger ones succeed. I assume this is some strange floating point effect, but am unsure. I found it quite by chance and as I have noted, it doesn't fail on a simple square polygon.

Importantly the buffer doesn't fail at any size I have tested if I leave out the set_precision step.

I encountered this problem when using a negative buffer in data cleaning (where I had the buffer style set to minimise the buffer segments, and thinking it might relate to quantisation issues with a coarse buffer style and small numbers changed to 'default' buffering by not setting any of quad_segs, or join_style, etc., but as above the issue persists.

Operating system

Mac Sonoma 14.2.1

Shapely version and provenance

pip installed 2.0.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided set_precision() and negative buffer reproduction with the reported Shapely 2.0.2 setup, then compare buffer sizes with and without precision setting. Trace the set_precision() and buffer entry points to identify the interaction, and add a regression test showing that the negative buffer does not unexpectedly become empty.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.