Exemple #1
0
def test_set_precision_collapse(geometry, mode, expected):
    """Lines and polygons collapse to empty geometries if vertices are too close"""
    actual = shapely.set_precision(geometry, 1, mode=mode)
    if shapely.geos_version < (3, 9, 0):
        # pre GEOS 3.9 has difficulty comparing empty geometries exactly
        # normalize and compare by WKT instead
        assert shapely.to_wkt(shapely.normalize(actual)) == shapely.to_wkt(
            shapely.normalize(expected))
    else:
        # force to 2D because GEOS 3.10 yields 3D geometries when they are empty.
        assert_geometries_equal(shapely.force_2d(actual), expected)
def test_set_operation_prec_array(a, func, grid_size):
    actual = func([a, a], point, grid_size=grid_size)
    assert actual.shape == (2, )
    assert isinstance(actual[0], Geometry)

    # results should match the operation when the precision is previously set
    # to same grid_size
    b = shapely.set_precision(a, grid_size=grid_size)
    point2 = shapely.set_precision(point, grid_size=grid_size)
    expected = func([b, b], point2)

    assert shapely.equals(shapely.normalize(actual),
                          shapely.normalize(expected)).all()
Exemple #3
0
def test_set_precision_intersection():
    """Operations should use the most precise presision grid size of the inputs"""

    box1 = shapely.normalize(shapely.box(0, 0, 0.9, 0.9))
    box2 = shapely.normalize(shapely.box(0.75, 0, 1.75, 0.75))

    assert shapely.get_precision(shapely.intersection(box1, box2)) == 0

    # GEOS will use and keep the most precise precision grid size
    box1 = shapely.set_precision(box1, 0.5)
    box2 = shapely.set_precision(box2, 1)
    out = shapely.intersection(box1, box2)
    assert shapely.get_precision(out) == 0.5
    assert_geometries_equal(out, shapely.Geometry("LINESTRING (1 1, 1 0)"))
Exemple #4
0
    def normalize(self):
        """Converts geometry to normal form (or canonical form).

        This method orders the coordinates, rings of a polygon and parts of
        multi geometries consistently. Typically useful for testing purposes
        (for example in combination with `equals_exact`).

        Examples
        --------
        >>> from shapely.wkt import loads
        >>> p = loads("MULTILINESTRING((0 0, 1 1), (3 3, 2 2))")
        >>> p.normalize().wkt
        'MULTILINESTRING ((2 2, 3 3), (0 0, 1 1))'
        """
        return shapely.normalize(self)
Exemple #5
0
def assert_geometries_equal(
    x,
    y,
    tolerance=1e-7,
    equal_none=True,
    equal_nan=True,
    normalize=False,
    err_msg="",
    verbose=True,
):
    """Raises an AssertionError if two geometry array_like objects are not equal.

    Given two array_like objects, check that the shape is equal and all elements of
    these objects are equal. An exception is raised at shape mismatch or conflicting
    values. In contrast to the standard usage in shapely, no assertion is raised if
    both objects have NaNs/Nones in the same positions.

    Parameters
    ----------
    x : Geometry or array_like
    y : Geometry or array_like
    equal_none : bool, default True
        Whether to consider None elements equal to other None elements.
    equal_nan : bool, default True
        Whether to consider nan coordinates as equal to other nan coordinates.
    normalize : bool, default False
        Whether to normalize geometries prior to comparison.
    err_msg : str, optional
        The error message to be printed in case of failure.
    verbose : bool, optional
        If True, the conflicting values are appended to the error message.
    """
    __tracebackhide__ = True  # Hide traceback for py.test
    if normalize:
        x = shapely.normalize(x)
        y = shapely.normalize(y)
    x = np.array(x, copy=False)
    y = np.array(y, copy=False)

    is_scalar = x.ndim == 0 or y.ndim == 0

    # Check the shapes (condition is copied from numpy test_array_equal)
    if not (is_scalar or x.shape == y.shape):
        msg = build_err_msg(
            [x, y],
            err_msg + f"\n(shapes {x.shape}, {y.shape} mismatch)",
            verbose=verbose,
        )
        raise AssertionError(msg)

    flagged = False
    if equal_none:
        flagged = _assert_none_same(x, y, err_msg, verbose)

    if not np.isscalar(flagged):
        x, y = x[~flagged], y[~flagged]
        # Only do the comparison if actual values are left
        if x.size == 0:
            return
    elif flagged:
        # no sense doing comparison if everything is flagged.
        return

    is_equal = _equals_exact_with_ndim(x, y, tolerance=tolerance)
    if is_scalar and not np.isscalar(is_equal):
        is_equal = bool(is_equal[0])

    if np.all(is_equal):
        return
    elif not equal_nan:
        msg = build_err_msg(
            [x, y],
            err_msg + f"\nNot equal to tolerance {tolerance:g}",
            verbose=verbose,
        )
        raise AssertionError(msg)

    # Optionally refine failing elements if NaN should be considered equal
    if not np.isscalar(is_equal):
        x, y = x[~is_equal], y[~is_equal]
        # Only do the NaN check if actual values are left
        if x.size == 0:
            return
    elif is_equal:
        # no sense in checking for NaN if everything is equal.
        return

    is_equal = _assert_nan_coords_same(x, y, tolerance, err_msg, verbose)
    if not np.all(is_equal):
        msg = build_err_msg(
            [x, y],
            err_msg + f"\nNot equal to tolerance {tolerance:g}",
            verbose=verbose,
        )
        raise AssertionError(msg)
def test_normalize(geom, expected):
    actual = shapely.normalize(geom)
    assert actual == expected
def test_make_valid_1d(geom, expected):
    actual = shapely.make_valid(geom)
    # normalize needed to handle variation in output across GEOS versions
    assert np.all(shapely.normalize(actual) == shapely.normalize(expected))
def test_make_valid(geom, expected):
    actual = shapely.make_valid(geom)
    assert actual is not expected
    # normalize needed to handle variation in output across GEOS versions
    assert shapely.normalize(actual) == expected