shapely / shapely/shapely

`IndexError` in `shapely.geometry.shape` when parsing a MultiPolygon with some empty members

Open
#1,407 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Expected behavior and actual behavior.

Create a MultiPolygon.

Steps to reproduce the problem.

I think this is valid GeoJSON, but shapely errors when trying to convert it.

import shapely.geometry
data = {
    "type": "MultiPolygon",
    "coordinates": [
        [],
        [[[0, 0],
          [1, 0],
          [1, 1],
          [0, 1],
          [0, 0]]]
    ]
}

shapely.geometry.shape(data)

That raises with

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [3], in <cell line: 13>()
      1 data = {
      2     "type": "MultiPolygon",
      3     "coordinates": [
   (...)
     10     ]
     11 }
---> 13 shapely.geometry.shape(data)

File ~/src/stac-utils/stac-geoparquet/.venv/lib/python3.8/site-packages/shapely/geometry/geo.py:118, in shape(context)
    116     return MultiLineString(ob["coordinates"])
    117 elif geom_type == "multipolygon":
--> 118     return MultiPolygon(ob["coordinates"], context_type='geojson')
    119 elif geom_type == "geometrycollection":
    120     geoms = [shape(g) for g in ob.get("geometries", [])]

File ~/src/stac-utils/stac-geoparquet/.venv/lib/python3.8/site-packages/shapely/geometry/multipolygon.py:62, in MultiPolygon.__init__(self, polygons, context_type)
     60     geom, n = geos_multipolygon_from_polygons(polygons)
     61 elif context_type == 'geojson':
---> 62     geom, n = geos_multipolygon_from_py(polygons)
     63 self._set_geom(geom)
     64 self._ndim = n

File ~/src/stac-utils/stac-geoparquet/.venv/lib/python3.8/site-packages/shapely/geometry/multipolygon.py:146, in geos_multipolygon_from_py(ob)
    143 L = len(ob)
    144 assert L >= 1
--> 146 N = len(ob[0][0][0])
    147 assert N == 2 or N == 3
    149 subs = (c_void_p * L)()

IndexError: list index out of range

Operating system

Linux

Shapely version and provenance

1.8.2 from PyPI

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 GeoJSON reproducer through shapely.geometry.shape and trace the MultiPolygon path shown in geometry/geo.py and geometry/multipolygon.py, especially geos_multipolygon_from_py. Check how empty members are handled and add regression coverage for this input. Done means the example no longer raises IndexError when parsed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.