Conversion

class RasterDataset[source]

Bases: object

Placeholder.

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[Any, dtype[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.59376 0.92577)
1  POINT (0.34075 0.91650)
2  POINT (0.74841 0.10627)
3  POINT (0.00966 0.87868)
4  POINT (0.38046 0.87879)
>>> sg.coordinate_array(points)
array([[0.59376221, 0.92577159],
    [0.34074678, 0.91650446],
    [0.74840912, 0.10626954],
    [0.00965935, 0.87867915],
    [0.38045827, 0.87878816]])
>>> sg.coordinate_array(points.geometry)
array([[0.59376221, 0.92577159],
    [0.34074678, 0.91650446],
    [0.74840912, 0.10626954],
    [0.00965935, 0.87867915],
    [0.38045827, 0.87878816]])
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 | None | Any

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), or any interable of such objects.

Meant for convenience in testing and exploring, not for production code since it introduces unnecessary overhead.

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 | tuple[str] | 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
>>> coords = (10, 60)
>>> sg.to_gdf(coords, crs=4326)
                    geometry
0  POINT (10.00000 60.00000)

From wkt.

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

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])
    x   y
1  10  60
3  11  59
>>> gdf = sg.to_gdf(df, geometry=["x", "y"], crs=4326)
>>> gdf
    x   y                   geometry
1  10  60  POINT (10.00000 60.00000)
3  11  59  POINT (11.00000 59.00000)

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.00000 60.00000)
1    2  POINT (11.00000 59.00000)

From Series.

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

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.00000 60.00000)
1  POINT (11.00000 59.00000)
>>> from shapely.geometry import LineString
>>> sg.to_gdf(LineString(coordslist), crs=4326)
                                            geometry
0  LINESTRING (10.00000 60.00000, 11.00000 59.00000)

From 2 or 3 dimensional array.

>>> arr = np.random.randint(100, size=(5, 3))
>>> sg.to_gdf(arr)
                         geometry
0  POINT Z (82.000 88.000 82.000)
1  POINT Z (70.000 92.000 20.000)
2   POINT Z (91.000 34.000 3.000)
3   POINT Z (1.000 50.000 77.000)
4  POINT Z (58.000 49.000 46.000)
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)