Skip to content

Commit

Permalink
Merge d7ac3dd into 9540b77
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Nov 5, 2023
2 parents 9540b77 + d7ac3dd commit bc9c1ca
Show file tree
Hide file tree
Showing 5 changed files with 503 additions and 22 deletions.
26 changes: 17 additions & 9 deletions shapely/io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from shapely import lib
from shapely import geos_version, lib
from shapely._enum import ParamEnum

# include ragged array functions here for reference documentation purpose
Expand Down Expand Up @@ -33,7 +33,7 @@ def to_wkt(
geometry,
rounding_precision=6,
trim=True,
output_dimension=3,
output_dimension=None,
old_3d=False,
**kwargs,
):
Expand All @@ -58,8 +58,10 @@ def to_wkt(
-1 to indicate the full precision.
trim : bool, default True
If True, trim unnecessary decimals (trailing zeros).
output_dimension : int, default 3
The output dimension for the WKT string. Supported values are 2 and 3.
output_dimension : int, default None
The output dimension for the WKT string. Supported values are 2, 3 and
4 for GEOS 3.12+. Default None will automatically choose 3 or 4,
depending on the version of GEOS.
Specifying 3 means that up to 3 dimensions will be written but 2D
geometries will still be represented as 2D in the WKT string.
old_3d : bool, default False
Expand Down Expand Up @@ -97,7 +99,9 @@ def to_wkt(
raise TypeError("rounding_precision only accepts scalar values")
if not np.isscalar(trim):
raise TypeError("trim only accepts scalar values")
if not np.isscalar(output_dimension):
if output_dimension is None:
output_dimension = 3 if geos_version < (3, 12, 0) else 4
elif not np.isscalar(output_dimension):
raise TypeError("output_dimension only accepts scalar values")
if not np.isscalar(old_3d):
raise TypeError("old_3d only accepts scalar values")
Expand All @@ -115,7 +119,7 @@ def to_wkt(
def to_wkb(
geometry,
hex=False,
output_dimension=3,
output_dimension=None,
byte_order=-1,
include_srid=False,
flavor="extended",
Expand All @@ -141,8 +145,10 @@ def to_wkb(
hex : bool, default False
If true, export the WKB as a hexidecimal string. The default is to
return a binary bytes object.
output_dimension : int, default 3
The output dimension for the WKB. Supported values are 2 and 3.
output_dimension : int, default None
The output dimension for the WKB. Supported values are 2, 3 and 4 for
GEOS 3.12+. Default None will automatically choose 3 or 4, depending on
the version of GEOS.
Specifying 3 means that up to 3 dimensions will be written but 2D
geometries will still be represented as 2D in the WKB represenation.
byte_order : int, default -1
Expand Down Expand Up @@ -173,7 +179,9 @@ def to_wkb(
"""
if not np.isscalar(hex):
raise TypeError("hex only accepts scalar values")
if not np.isscalar(output_dimension):
if output_dimension is None:
output_dimension = 3 if geos_version < (3, 12, 0) else 4
elif not np.isscalar(output_dimension):
raise TypeError("output_dimension only accepts scalar values")
if not np.isscalar(byte_order):
raise TypeError("byte_order only accepts scalar values")
Expand Down
102 changes: 102 additions & 0 deletions shapely/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,64 @@
multi_line_stringt_empty_z = shapely.multilinestrings([empty_line_string_z])
multi_polygon_empty_z = shapely.multipolygons([empty_polygon_z])
geometry_collection_empty_z = shapely.geometrycollections([empty_line_string_z])
# XYM
point_m = shapely.from_wkt("POINT M (2 3 5)")
line_string_m = shapely.from_wkt("LINESTRING M (0 0 1, 1 0 2, 1 1 3)")
linear_ring_m = shapely.from_wkt("LINEARRING M (0 0 1, 1 0 2, 1 1 3, 0 1 2, 0 0 1)")
polygon_m = shapely.from_wkt("POLYGON M ((0 0 1, 2 0 2, 2 2 3, 0 2 2, 0 0 1))")
polygon_with_hole_m = shapely.from_wkt(
"""POLYGON M ((0 0 1, 0 10 2, 10 10 3, 10 0 2, 0 0 1),
(2 2 6, 2 4 5, 4 4 4, 4 2 5, 2 2 6))"""
)
multi_point_m = shapely.from_wkt("MULTIPOINT M ((0 0 3), (1 2 5))")
multi_line_string_m = shapely.from_wkt("MULTILINESTRING M ((0 0 3, 1 2 5))")
multi_polygon_m = shapely.from_wkt(
"""MULTIPOLYGON M (((0 0 1, 2 0 2, 2 2 3, 0 2 2, 0 0 1)),
((2.1 2.1 1.1, 2.2 2.1 1.2, 2.2 2.2 1.3, 2.1 2.2 1.4, 2.1 2.1 1.1)))"""
)
geometry_collection_m = shapely.GeometryCollection([point_m, line_string_m])
empty_geometry_collection_m = shapely.from_wkt("GEOMETRYCOLLECTION M EMPTY")
empty_point_m = shapely.from_wkt("POINT M EMPTY")
empty_line_string_m = shapely.from_wkt("LINESTRING M EMPTY")
empty_polygon_m = shapely.from_wkt("POLYGON M EMPTY")
empty_multi_point_m = shapely.from_wkt("MULTIPOINT M EMPTY")
empty_multi_line_string_m = shapely.from_wkt("MULTILINESTRING M EMPTY")
empty_multi_polygon_m = shapely.from_wkt("MULTIPOLYGON M EMPTY")
multi_point_empty_m = shapely.multipoints([empty_point_m])
multi_line_stringt_empty_m = shapely.multilinestrings([empty_line_string_m])
multi_polygon_empty_m = shapely.multipolygons([empty_polygon_m])
geometry_collection_empty_m = shapely.geometrycollections([empty_line_string_m])
# XYZM
point_zm = shapely.from_wkt("POINT ZM (2 3 4 5)")
line_string_zm = shapely.from_wkt("LINESTRING ZM (0 0 4 1, 1 0 4 2, 1 1 4 3)")
linear_ring_zm = shapely.from_wkt(
"LINEARRING ZM (0 0 1 8, 1 0 2 7, 1 1 3 6, 0 1 2 9, 0 0 1 8)"
)
polygon_zm = shapely.from_wkt(
"POLYGON ZM ((0 0 4 1, 2 0 4 2, 2 2 4 3, 0 2 4 2, 0 0 4 1))"
)
polygon_with_hole_zm = shapely.from_wkt(
"""POLYGON ZM ((0 0 4 1, 0 10 4 2, 10 10 4 3, 10 0 4 2, 0 0 4 1),
(2 2 4 6, 2 4 4 5, 4 4 4 4, 4 2 4 5, 2 2 4 6))"""
)
multi_point_zm = shapely.from_wkt("MULTIPOINT ZM ((0 0 4 3), (1 2 4 5))")
multi_line_string_zm = shapely.from_wkt("MULTILINESTRING ZM ((0 0 4 3, 1 2 4 5))")
multi_polygon_zm = shapely.from_wkt(
"""MULTIPOLYGON ZM (((0 0 4 1, 2 0 4 2, 2 2 4 3, 0 2 4 2, 0 0 4 1)),
((2.1 2.1 4 1.1, 2.2 2.1 4 1.2, 2.2 2.2 4 1.3, 2.1 2.2 4 1.4, 2.1 2.1 4 1.1)))"""
)
geometry_collection_zm = shapely.GeometryCollection([point_zm, line_string_zm])
empty_geometry_collection_zm = shapely.from_wkt("GEOMETRYCOLLECTION ZM EMPTY")
empty_point_zm = shapely.from_wkt("POINT ZM EMPTY")
empty_line_string_zm = shapely.from_wkt("LINESTRING ZM EMPTY")
empty_polygon_zm = shapely.from_wkt("POLYGON ZM EMPTY")
empty_multi_point_zm = shapely.from_wkt("MULTIPOINT ZM EMPTY")
empty_multi_line_string_zm = shapely.from_wkt("MULTILINESTRING ZM EMPTY")
empty_multi_polygon_zm = shapely.from_wkt("MULTIPOLYGON ZM EMPTY")
multi_point_empty_zm = shapely.multipoints([empty_point_zm])
multi_line_stringt_empty_zm = shapely.multilinestrings([empty_line_string_zm])
multi_polygon_empty_zm = shapely.multipolygons([empty_polygon_zm])
geometry_collection_empty_zm = shapely.geometrycollections([empty_line_string_zm])

all_types = (
point,
Expand Down Expand Up @@ -122,6 +180,50 @@
multi_polygon_empty_z,
geometry_collection_empty_z,
)
all_types_m = (
point_m,
line_string_m,
linear_ring_m,
polygon_m,
polygon_with_hole_m,
multi_point_m,
multi_line_string_m,
multi_polygon_m,
geometry_collection_m,
empty_geometry_collection_m,
empty_point_m,
empty_line_string_m,
empty_polygon_m,
empty_multi_point_m,
empty_multi_line_string_m,
empty_multi_polygon_m,
multi_point_empty_m,
multi_line_stringt_empty_m,
multi_polygon_empty_m,
geometry_collection_empty_m,
)
all_types_zm = (
point_zm,
line_string_zm,
linear_ring_zm,
polygon_zm,
polygon_with_hole_zm,
multi_point_zm,
multi_line_string_zm,
multi_polygon_zm,
geometry_collection_zm,
empty_geometry_collection_zm,
empty_point_zm,
empty_line_string_zm,
empty_polygon_zm,
empty_multi_point_zm,
empty_multi_line_string_zm,
empty_multi_polygon_zm,
multi_point_empty_zm,
multi_line_stringt_empty_zm,
multi_polygon_empty_zm,
geometry_collection_empty_zm,
)


@contextmanager
Expand Down

0 comments on commit bc9c1ca

Please sign in to comment.