def clip_by_rect(geom, xmin, ymin, xmax, ymax): """Returns the portion of a geometry within a rectangle The geometry is clipped in a fast but possibly dirty way. The output is not guaranteed to be valid. No exceptions will be raised for topological errors. Parameters ---------- geom : geometry The geometry to be clipped xmin : float Minimum x value of the rectangle ymin : float Minimum y value of the rectangle xmax : float Maximum x value of the rectangle ymax : float Maximum y value of the rectangle Notes ----- Requires GEOS >= 3.5.0 New in 1.7. """ if geom.is_empty: return geom return shapely.clip_by_rect(geom, xmin, ymin, xmax, ymax)
def test_clip_by_rect_non_scalar_kwargs(): msg = "only accepts scalar values" with pytest.raises(TypeError, match=msg): shapely.clip_by_rect([line_string, line_string], 0, 0, 1, np.array([0, 1]))
def test_clip_by_rect_empty(geom): # TODO empty point actual = shapely.clip_by_rect(geom, 0, 0, 1, 1) assert actual == Geometry("GEOMETRYCOLLECTION EMPTY")
def test_clip_by_rect_missing(): actual = shapely.clip_by_rect(None, 0, 0, 1, 1) assert actual is None
def test_clip_by_rect_array(geometry): actual = shapely.clip_by_rect([geometry, geometry], 0.0, 0.0, 1.0, 1.0) assert actual.shape == (2,) assert actual[0] is None or isinstance(actual[0], Geometry)
def test_clip_by_rect_polygon(geom, rect, expected): geom, expected = shapely.Geometry(geom), shapely.Geometry(expected) actual = shapely.clip_by_rect(geom, *rect) assert_geometries_equal(actual, expected)
def test_clip_by_rect(geom, expected): geom, expected = shapely.Geometry(geom), shapely.Geometry(expected) actual = shapely.clip_by_rect(geom, 10, 10, 20, 20) assert_geometries_equal(actual, expected)
def time_clip_by_rect(self): for bounds in self.bounds: shapely.clip_by_rect(self.polygon, *bounds)