def test_set_precision_drop_coords(): # setting precision of 0 will not drop duplicated points in original geometry = shapely.set_precision( shapely.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)"), 0) assert_geometries_equal( geometry, shapely.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)")) # setting precision will remove duplicated points geometry = shapely.set_precision(geometry, 1) assert_geometries_equal(geometry, shapely.Geometry("LINESTRING (0 0, 0 1, 1 1)"))
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()
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)"))
def test_set_precision(mode): initial_geometry = shapely.Geometry("POINT (0.9 0.9)") assert shapely.get_precision(initial_geometry) == 0 geometry = shapely.set_precision(initial_geometry, 0, mode=mode) assert shapely.get_precision(geometry) == 0 assert_geometries_equal(geometry, initial_geometry) geometry = shapely.set_precision(initial_geometry, 1, mode=mode) assert shapely.get_precision(geometry) == 1 assert_geometries_equal(geometry, shapely.Geometry("POINT (1 1)")) # original should remain unchanged assert_geometries_equal(initial_geometry, shapely.Geometry("POINT (0.9 0.9)"))
def test_set_precision_z(mode): with warnings.catch_warnings(): warnings.simplefilter( "ignore") # GEOS <= 3.9 emits warning for 'pointwise' geometry = shapely.set_precision( shapely.Geometry("POINT Z (0.9 0.9 0.9)"), 1, mode=mode) assert shapely.get_precision(geometry) == 1 assert_geometries_equal(geometry, shapely.Geometry("POINT Z (1 1 0.9)"))
def set_precision_pointwise_pre_310(): # using 'pointwise' emits a warning with pytest.warns(UserWarning): actual = shapely.set_precision( shapely.Geometry("LINESTRING (0 0, 0.1 0.1)"), 1.0, mode="pointwise", ) assert_geometries_equal(shapely.force_2d(actual), shapely.Geometry("LINESTRING EMPTY"))
def set_precision_preserve_topology(preserve_topology): # the preserve_topology kwarg is deprecated (ignored) with pytest.warns(UserWarning): actual = shapely.set_precision( shapely.Geometry("LINESTRING (0 0, 0.1 0.1)"), 1.0, preserve_topology=preserve_topology, ) assert_geometries_equal(shapely.force_2d(actual), shapely.Geometry("LINESTRING EMPTY"))
def test_get_precision(): geometries = all_types + (point_z, empty_point, empty_line_string, empty_polygon) # default is 0 actual = shapely.get_precision(geometries).tolist() assert actual == [0] * len(geometries) geometry = shapely.set_precision(geometries, 1) actual = shapely.get_precision(geometry).tolist() assert actual == [1] * len(geometries)
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_precision_grid_size_nan(): assert shapely.set_precision(shapely.Geometry("POINT (0.9 0.9)"), np.nan) is None
def test_set_precision_none(): assert shapely.set_precision(None, 0) is None
def test_set_precision_nan(mode): with warnings.catch_warnings(): warnings.simplefilter( "ignore") # GEOS <= 3.9 emits warning for 'pointwise' actual = shapely.set_precision(line_string_nan, 1, mode=mode) assert_geometries_equal(actual, line_string_nan)