geopandas.GeoSeries.set_precision#

GeoSeries.set_precision(grid_size, mode='valid_output')[source]#

Returns a GeoSeries with the precision set to a precision grid size.

By default, geometries use double precision coordinates (grid_size=0).

Coordinates will be rounded if a precision grid is less precise than the input geometry. Duplicated vertices will be dropped from lines and polygons for grid sizes greater than 0. Line and polygon geometries may collapse to empty geometries if all vertices are closer together than grid_size. Spikes or sections in Polygons narrower than grid_size after rounding the vertices will be removed, which can lead to MultiPolygons or empty geometries. Z values, if present, will not be modified.

Parameters:
grid_sizefloat

Precision grid size. If 0, will use double precision (will not modify geometry if precision grid size was not previously set). If this value is more precise than input geometry, the input geometry will not be modified.

mode{‘valid_output’, ‘pointwise’, ‘keep_collapsed’}, default ‘valid_output’

This parameter determines the way a precision reduction is applied on the geometry. There are three modes:

  • 'valid_output' (default): The output is always valid. Collapsed geometry elements (including both polygons and lines) are removed. Duplicate vertices are removed.

  • 'pointwise': Precision reduction is performed pointwise. Output geometry may be invalid due to collapse or self-intersection. Duplicate vertices are not removed.

  • 'keep_collapsed': Like the default mode, except that collapsed linear geometry elements are preserved. Collapsed polygonal input elements are removed. Duplicate vertices are removed.

Notes

Subsequent operations will always be performed in the precision of the geometry with higher precision (smaller grid_size). That same precision will be attached to the operation outputs.

Input geometries should be geometrically valid; unexpected results may occur if input geometries are not. You can check the validity with is_valid() and fix invalid geometries with make_valid() methods.

Examples

>>> from shapely import LineString, Point
>>> s = geopandas.GeoSeries(
...     [
...        Point(0.9, 0.9),
...        Point(0.9, 0.9, 0.9),
...        LineString([(0, 0), (0, 0.1), (0, 1), (1, 1)]),
...        LineString([(0, 0), (0, 0.1), (0.1, 0.1)])
...     ],
... )
>>> s
0                      POINT (0.9 0.9)
1                POINT Z (0.9 0.9 0.9)
2    LINESTRING (0 0, 0 0.1, 0 1, 1 1)
3     LINESTRING (0 0, 0 0.1, 0.1 0.1)
dtype: geometry
>>> s.set_precision(1)
0                   POINT (1 1)
1             POINT Z (1 1 0.9)
2    LINESTRING (0 0, 0 1, 1 1)
3            LINESTRING Z EMPTY
dtype: geometry
>>> s.set_precision(1, mode="pointwise")
0                        POINT (1 1)
1                  POINT Z (1 1 0.9)
2    LINESTRING (0 0, 0 0, 0 1, 1 1)
3         LINESTRING (0 0, 0 0, 0 0)
dtype: geometry
>>> s.set_precision(1, mode="keep_collapsed")
0                   POINT (1 1)
1             POINT Z (1 1 0.9)
2    LINESTRING (0 0, 0 1, 1 1)
3         LINESTRING (0 0, 0 0)
dtype: geometry