to_wkb(geometry, hex=False, output_dimension=None, byte_order=-1, include_srid=False, flavor='extended', **kwargs)#

Converts to the Well-Known Binary (WKB) representation of a Geometry.

The Well-Known Binary format is defined in the OGC Simple Features Specification for SQL.

The following limitations apply to WKB serialization:

  • linearrings will be converted to linestrings

  • a point with only NaN coordinates is converted to an empty point

  • for GEOS <= 3.7, empty points are always serialized to 3D if output_dimension=3, and to 2D if output_dimension=2

  • for GEOS == 3.8, empty points are always serialized to 2D

geometryGeometry or array_like
hexbool, default False

If true, export the WKB as a hexadecimal string. The default is to return a binary bytes object.

output_dimensionint, 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 representation.

byte_orderint, default -1

Defaults to native machine byte order (-1). Use 0 to force big endian and 1 for little endian.

include_sridbool, default False

If True, the SRID is be included in WKB (this is an extension to the OGC WKB specification). Not allowed when flavor is “iso”.

flavor{“iso”, “extended”}, default “extended”

Which flavor of WKB will be returned. The flavor determines how extra dimensionality is encoded with the type number, and whether SRID can be included in the WKB. ISO flavor is “more standard” for 3D output, and does not support SRID embedding. Both flavors are equivalent when output_dimension=2 (or with 2D geometries) and include_srid=False. The from_wkb function can read both flavors.


See NumPy ufunc docs for other keyword arguments.


>>> from shapely import Point
>>> point = Point(1, 1)
>>> to_wkb(point, byte_order=1)
>>> to_wkb(point, hex=True, byte_order=1)