Input/Output

pygeos.io.from_shapely(geometry, **kwargs)

Creates geometries from shapely Geometry objects.

This function requires the GEOS version of PyGEOS and shapely to be equal.

Parameters
geometryshapely Geometry object or array_like

Examples

>>> from shapely.geometry import Point   
>>> from_shapely(Point(1, 2))   
<pygeos.Geometry POINT (1 2)>
pygeos.io.from_wkb(geometry, **kwargs)

Creates geometries from the Well-Known Binary (WKB) representation.

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

Parameters
geometrystr or array_like

The WKB byte object(s) to convert.

Examples

>>> from_wkb(b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?')
<pygeos.Geometry POINT (1 1)>
pygeos.io.from_wkt(geometry, **kwargs)

Creates geometries from the Well-Known Text (WKT) representation.

The Well-known Text format is defined in the OGC Simple Features Specification for SQL.

Parameters
geometrystr or array_like

The WKT string(s) to convert.

Examples

>>> from_wkt('POINT (0 0)')
<pygeos.Geometry POINT (0 0)>
pygeos.io.to_wkb(geometry, hex=False, output_dimension=3, byte_order=- 1, include_srid=False, **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

  • empty points are transformed to 3D in GEOS < 3.8

  • empty points are transformed to 2D in GEOS 3.8

Parameters
geometryGeometry or array_like
hexbool, default False

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

output_dimensionint, default 3

The output dimension for the WKB. Supported values are 2 and 3. Specifying 3 means that up to 3 dimensions will be written but 2D geometries will still be represented as 2D in the WKB represenation.

byte_orderint

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

include_sridbool, default False

Whether the SRID should be included in WKB (this is an extension to the OGC WKB specification).

Examples

>>> to_wkb(Geometry("POINT (1 1)"), byte_order=1)
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?'
>>> to_wkb(Geometry("POINT (1 1)"), hex=True, byte_order=1)
'0101000000000000000000F03F000000000000F03F'
pygeos.io.to_wkt(geometry, rounding_precision=6, trim=True, output_dimension=3, old_3d=False, **kwargs)

Converts to the Well-Known Text (WKT) representation of a Geometry.

The Well-known Text format is defined in the OGC Simple Features Specification for SQL.

Parameters
geometryGeometry or array_like
rounding_precisionint, default 6

The rounding precision when writing the WKT string. Set to a value of -1 to indicate the full precision.

trimbool, default True

Whether to trim unnecessary decimals (trailing zeros).

output_dimensionint, default 3

The output dimension for the WKT string. Supported values are 2 and 3. Specifying 3 means that up to 3 dimensions will be written but 2D geometries will still be represented as 2D in the WKT string.

old_3dbool, default False

Enable old style 3D/4D WKT generation. By default, new style 3D/4D WKT (ie. “POINT Z (10 20 30)”) is returned, but with old_3d=True the WKT will be formatted in the style “POINT (10 20 30)”.

Notes

The defaults differ from the default of the GEOS library. To mimic this, use:

to_wkt(geometry, rounding_precision=-1, trim=False, output_dimension=2)

Examples

>>> to_wkt(Geometry("POINT (0 0)"))
'POINT (0 0)'
>>> to_wkt(Geometry("POINT (0 0)"), rounding_precision=3, trim=False)
'POINT (0.000 0.000)'
>>> to_wkt(Geometry("POINT (0 0)"), rounding_precision=-1, trim=False)
'POINT (0.0000000000000000 0.0000000000000000)'
>>> to_wkt(Geometry("POINT (1 2 3)"), trim=True)
'POINT Z (1 2 3)'
>>> to_wkt(Geometry("POINT (1 2 3)"), trim=True, output_dimension=2)
'POINT (1 2)'
>>> to_wkt(Geometry("POINT (1 2 3)"), trim=True, old_3d=True)
'POINT (1 2 3)'