def test_to_wkt(): point = shapely.points(1, 1) actual = shapely.to_wkt(point) assert actual == "POINT (1 1)" actual = shapely.to_wkt(point, trim=False) assert actual == "POINT (1.000000 1.000000)" actual = shapely.to_wkt(point, rounding_precision=3, trim=False) assert actual == "POINT (1.000 1.000)"
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_to_wkt_3D(): # 3D points point_z = shapely.points(1, 1, 1) actual = shapely.to_wkt(point_z) assert actual == "POINT Z (1 1 1)" actual = shapely.to_wkt(point_z, output_dimension=3) assert actual == "POINT Z (1 1 1)" actual = shapely.to_wkt(point_z, output_dimension=2) assert actual == "POINT (1 1)" actual = shapely.to_wkt(point_z, old_3d=True) assert actual == "POINT (1 1 1)"
def test_to_wkb_srid(): # hex representation of POINT (0 0) with SRID=4 ewkb = "01010000200400000000000000000000000000000000000000" wkb = "010100000000000000000000000000000000000000" actual = shapely.from_wkb(ewkb) assert shapely.to_wkt(actual, trim=True) == "POINT (0 0)" assert shapely.to_wkb(actual, hex=True, byte_order=1) == wkb assert shapely.to_wkb(actual, hex=True, include_srid=True, byte_order=1) == ewkb point = shapely.points(1, 1) point_with_srid = shapely.set_srid(point, np.int32(4326)) result = shapely.to_wkb(point_with_srid, include_srid=True, byte_order=1) assert np.frombuffer(result[5:9], "<u4").item() == 4326
def dumps(ob, trim=False, rounding_precision=-1, **kw): """ Dump a WKT representation of a geometry to a string. Parameters ---------- ob : A geometry object of any type to be dumped to WKT. trim : bool, default False Remove excess decimals from the WKT. rounding_precision : int Round output to the specified number of digits. Default behavior returns full precision. output_dimension : int, default 3 Force removal of dimensions above the one specified. Returns ------- input geometry as WKT string """ return shapely.to_wkt(ob, trim=trim, rounding_precision=rounding_precision, **kw)
def test_to_wkt_multipoint_with_point_empty_errors(): # test if segfault is prevented geom = shapely.multipoints([empty_point, point]) with pytest.raises(ValueError): shapely.to_wkt(geom)
def test_to_wkt_multipoint_with_point_empty(): geom = shapely.multipoints([empty_point, point]) assert shapely.to_wkt(geom) == "MULTIPOINT (EMPTY, 2 3)"
def test_to_wkt_geometrycollection_with_point_empty(): collection = shapely.geometrycollections([empty_point, point]) # do not check the full value as some GEOS versions give # GEOMETRYCOLLECTION Z (...) and others give GEOMETRYCOLLECTION (...) assert shapely.to_wkt(collection).endswith("(POINT EMPTY, POINT (2 3))")
def test_to_wkt_empty_z(wkt): assert shapely.to_wkt(shapely.Geometry(wkt)) == wkt
def test_to_wkt_point_empty(): assert shapely.to_wkt(empty_point) == "POINT EMPTY"
def test_to_wkt_exceptions(): with pytest.raises(TypeError): shapely.to_wkt(1) with pytest.raises(shapely.GEOSException): shapely.to_wkt(point, output_dimension=4)
def test_to_wkt_none(): # None propagates assert shapely.to_wkt(None) is None
def setup(self): self.to_write = shapely.polygons(np.random.random((10000, 100, 2))) self.to_read_wkt = shapely.to_wkt(self.to_write) self.to_read_wkb = shapely.to_wkb(self.to_write)
def wkt(self): """WKT representation of the geometry""" # TODO(shapely-2.0) keep default of not trimming? return shapely.to_wkt(self, rounding_precision=-1)
def test_from_wkt_empty(wkt): geom = shapely.from_wkt(wkt) assert shapely.is_geometry(geom).all() assert shapely.is_empty(geom).all() assert shapely.to_wkt(geom) == wkt
def test_from_wkt_all_types(geom): wkt = shapely.to_wkt(geom) actual = shapely.from_wkt(wkt) assert_geometries_equal(actual, geom)
def time_write_to_wkt(self): shapely.to_wkt(self.to_write)