Conversion

coordinate_array(gdf, strict=False, include_z=False)[source]

Creates a 2d ndarray of coordinates from point geometries.

Parameters:
  • gdf (GeoDataFrame | GeoSeries) – GeoDataFrame or GeoSeries of point geometries.

  • strict (bool) – If False (default), geometries without coordinates are given the value None.

  • include_z (bool) – Whether to include z-coordinates. Defaults to False.

Return type:

NDArray[float64]

Returns:

np.ndarray of np.ndarrays of coordinates.

Examples:

>>> import sgis as sg
>>> points = sg.to_gdf(
...     [
...         (0, 1),
...         (1, 0),
...         (1, 1),
...         (0, 0),
...         (0.5, 0.5),
...         (0.5, 0.25),
...         (0.25, 0.25),
...         (0.75, 0.75),
...         (0.25, 0.75),
...         (0.75, 0.25),
...     ]
... )
>>> points
            geometry
0        POINT (0 1)
1        POINT (1 0)
2        POINT (1 1)
3        POINT (0 0)
4    POINT (0.5 0.5)
5   POINT (0.5 0.25)
6  POINT (0.25 0.25)
7  POINT (0.75 0.75)
8  POINT (0.25 0.75)
9  POINT (0.75 0.25)
>>> sg.coordinate_array(points)
array([[0.  , 1.  ],
       [1.  , 0.  ],
       [1.  , 1.  ],
       [0.  , 0.  ],
       [0.5 , 0.5 ],
       [0.5 , 0.25],
       [0.25, 0.25],
       [0.75, 0.75],
       [0.25, 0.75],
       [0.75, 0.25]])
>>> sg.coordinate_array(points.geometry)
array([[0.  , 1.  ],
       [1.  , 0.  ],
       [1.  , 1.  ],
       [0.  , 0.  ],
       [0.5 , 0.5 ],
       [0.5 , 0.25],
       [0.25, 0.25],
       [0.75, 0.75],
       [0.25, 0.75],
       [0.75, 0.25]])
crs_to_string(crs)[source]

Extract the string of a CRS-like object.

Return type:

str

Parameters:

crs (Any)

from_4326(lon, lat, crs=25833)[source]

Get utm 33 N coordinates from lonlat (4326).

Return type:

tuple[float, float]

Parameters:
  • lon (float)

  • lat (float)

get_crs_from_dict(obj)[source]

Try to extract the ‘crs’ attribute of the object or an object in the object.

Return type:

CRS | Any | None

Parameters:

obj (Any)

is_bbox_like(obj)[source]

Returns True if the object is an iterable of 4 numbers.

Parameters:

obj (Any) – Any object.

Return type:

bool

is_nested_geojson(obj)[source]

Returns True if the object is an iterable of all dicts.

Parameters:

obj (Any) – Any object.

Return type:

bool

to_4326(lon, lat, crs=25833)[source]

Get degree coordinates 33 N coordinates from lonlat (4326).

Return type:

tuple[float, float]

Parameters:
  • lon (float)

  • lat (float)

to_bbox(obj)[source]

Returns 4-length tuple of bounds if possible, else raises ValueError.

Parameters:

obj (GeoDataFrame | GeoSeries | Geometry | Collection | Mapping) – Object to be converted to bounding box. Can be geopandas or shapely objects, iterables of exactly four numbers or dictionary like/class with a the keys/attributes “minx”, “miny”, “maxx”, “maxy” or “xmin”, “ymin”, “xmax”, “ymax”.

Return type:

tuple[float, float, float, float]

to_gdf(obj, crs=None, geometry=None, **kwargs)[source]

Converts geometry-like objects to a GeoDataFrame.

Constructs a GeoDataFrame from any geometry-like object (coordinates, wkt, wkb, dict, string) or any interable of such objects.

NOTE: The function is meant for convenience in testing and exploring, not for production code since it introduces unnecessary overhead and potential errors.

If obj is a DataFrame or dictionary, geometries can be in one column/key or 2-3 if coordiantes are in x and x (and z) columns. The column/key “geometry” is used by default if it exists. The index and other columns/keys are preserved.

Parameters:
  • obj (Geometry | str | bytes | list | tuple | dict | GeoSeries | Series | DataFrame | Iterator) – the object to be converted to a GeoDataFrame.

  • crs (str | Any | None) – if None (default), it uses the crs of the GeoSeries if GeoSeries is the input type. Otherwise, no crs is used.

  • geometry (str | tuple[str] | int | None) – Name of column(s) or key(s) in obj with geometry-like values. If not specified, the key/column ‘geometry’ will be used if it exists. If multiple columns, can be given as e.g. “xyz” or [“x”, “y”].

  • **kwargs – additional keyword arguments taken by the GeoDataFrame constructor.

Return type:

GeoDataFrame

Returns:

A GeoDataFrame with one column, the geometry column.

Examples:

>>> import sgis as sg
>>> import numpy as np
>>> np.random.seed(0)
>>> import pandas as pd
>>> coords = (10, 60)
>>> sg.to_gdf(coords, crs=4326)
        geometry
0  POINT (10 60)

From wkt.

>>> wkt = "POINT (10 60)"
>>> sg.to_gdf(wkt, crs=4326)
        geometry
0  POINT (10 60)

From DataFrame with x, y (optionally z) coordinate columns. Index and columns are preserved.

>>> df = pd.DataFrame({"x": [10, 11], "y": [60, 59]}, index=[1,3])
>>> df
    x   y
1  10  60
3  11  59
>>> gdf = sg.to_gdf(df, geometry=["x", "y"], crs=4326)
>>> print(gdf.to_string(max_rows=10, max_colwidth=50, show_dimensions=True))
    x   y       geometry
1  10  60  POINT (10 60)
3  11  59  POINT (11 59)

[2 rows x 3 columns]

For DataFrame/dict with a geometry-like column named “geometry”. If the column has another name, it must be set with the geometry parameter.

>>> df = pd.DataFrame({"col": [1, 2], "geometry": ["point (10 60)", (11, 59)]})
>>> df
   col       geometry
0    1  point (10 60)
1    2       (11, 59)
>>> gdf = sg.to_gdf(df, crs=4326)
>>> gdf
   col       geometry
0    1  POINT (10 60)
1    2  POINT (11 59)

From Series.

>>> series = pd.Series({1: (10, 60), 3: (11, 59)})
>>> sg.to_gdf(series)
        geometry
1  POINT (10 60)
3  POINT (11 59)

Multiple coordinates will be converted to points, unless a line or polygon geometry is constructed beforehand.

>>> coordslist = [(10, 60), (11, 59)]
>>> sg.to_gdf(coordslist, crs=4326)
        geometry
0  POINT (10 60)
1  POINT (11 59)
>>> from shapely.geometry import LineString
>>> sg.to_gdf(LineString(coordslist), crs=4326)
                    geometry
0  LINESTRING (10 60, 11 59)

From 2 or 3 dimensional array.

>>> arr = np.random.randint(100, size=(5, 3))
>>> sg.to_gdf(arr)
             geometry
0  POINT Z (44 47 64)
1   POINT Z (67 67 9)
2  POINT Z (83 21 36)
3  POINT Z (87 70 88)
4  POINT Z (88 12 58)
to_geoseries(obj, crs=None)[source]

Convert an object to GeoSeries.

Return type:

GeoSeries

Parameters:
  • obj (Any)

  • crs (Any | None)

to_shapely(obj)[source]

Convert a geometry object or bounding box to a shapely Geometry.

Return type:

Geometry

Parameters:

obj (Any)