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

The shapely.ops.substring() function fails if not all vertices of the LineString have a Z coordinate #2049

Open
vadimcn opened this issue May 11, 2024 · 0 comments

Comments

@vadimcn
Copy link

vadimcn commented May 11, 2024

The shapely.ops.substring() function fails if not all vertices of the LineString have a Z coordinate (which stems from some operations like segmentize() losing Z, but I guess that's a GEOS issue).

Steps to reproduce the problem.

import math
from shapely import LineString
from shapely.ops import substring
l = LineString([[0, 0, 0], [1, 2, math.nan], [10, 10, 0]])
substring(l, 0.1, 0.9, normalized=True)

Error

Traceback (most recent call last)
File repro.py:6
      [4](repro.py:4) from shapely.ops import substring
      [5](repro.py:5) l = LineString([[0, 0, 0], [1, 2, math.nan], [10, 10, 0]])
----> [6](repro.py:6) substring(l, 0.1, 0.9, normalized=True)

File ~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/ops.py:674, in substring(geom, start_dist, end_dist, normalized)
    [671](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/ops.py:671) else:
    [672](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/ops.py:672)     vertex_list.append(tuple(*end_point.coords))
--> [674](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/ops.py:674) return LineString(vertex_list)

File ~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:73, in LineString.__new__(self, coordinates)
     [68](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:68) if len(coordinates) == 0:
     [69](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:69)     # empty geometry
     [70](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:70)     # TODO better constructor + should shapely.linestrings handle this?
     [71](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:71)     return shapely.from_wkt("LINESTRING EMPTY")
---> [73](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:73) geom = shapely.linestrings(coordinates)
     [74](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:74) if not isinstance(geom, LineString):
     [75](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/geometry/linestring.py:75)     raise ValueError("Invalid values passed to LineString constructor")

File ~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/decorators.py:77, in multithreading_enabled.<locals>.wrapped(*args, **kwargs)
     [75](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/decorators.py:75)     for arr in array_args:
     [76](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/decorators.py:76)         arr.flags.writeable = False
---> [77](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/decorators.py:77)     return func(*args, **kwargs)
     [78](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/decorators.py:78) finally:
     [79](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/decorators.py:79)     for arr, old_flag in zip(array_args, old_flags):

File ~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/creation.py:119, in linestrings(coords, y, z, indices, out, **kwargs)
    [117](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/creation.py:117) coords = _xyz_to_coords(coords, y, z)
    [118](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/creation.py:118) if indices is None:
--> [119](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/creation.py:119)     return lib.linestrings(coords, out=out, **kwargs)
    [120](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/creation.py:120) else:
    [121](~/bin/miniconda/envs/mapgpt/lib/python3.11/site-packages/shapely/creation.py:121)     return simple_geometries_1d(coords, indices, GeometryType.LINESTRING, out=out)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

Operating system

Ubuntu 22.04

Shapely version and provenance

shapely 2.0.3 installed from PyPI using pip

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

No branches or pull requests

1 participant