Skip to content

GeoJSON Format (.geojson)

GeoJSON (RFC 7946) is a JSON-based format for encoding geographic data structures. sleap-io uses it to store ROIs (regions of interest) as a human-readable, standalone format. The output is compatible with the movement library (v0.15.0+) and the broader geospatial Python ecosystem (Shapely, GeoPandas, QGIS, QuPath).

Each ROI is serialized as a GeoJSON Feature with geometry and metadata properties. The ROI class also implements the Python __geo_interface__ protocol for direct interoperability with Shapely and other geo-aware libraries.

Examples

import sleap_io as sio
from sleap_io.model.roi import UserROI
from shapely.geometry import box

# Create some ROIs
rois = [
    UserROI(geometry=box(100, 200, 150, 280), name="box1", category="animal"),
    UserROI.from_polygon([(0, 0), (50, 0), (50, 50)], name="region"),
]

# Save to GeoJSON
sio.save_geojson(rois, "rois.geojson")

# Load back
loaded_rois = sio.load_geojson("rois.geojson")

# Also works with load_file/save_file (wraps in Labels)
labels = sio.load_file("rois.geojson")  # Returns Labels(rois=...)
sio.save_file(labels, "rois.geojson")

sleap_io.io.main.load_geojson(filename)

Load ROIs from a GeoJSON file.

Parameters:

Name Type Description Default
filename str

Path to a .geojson file containing ROI features.

required

Returns:

Type Description
list

A list of ROI objects.

See Also

ROI: Region of interest data structure. save_geojson: Write ROIs to GeoJSON.

Source code in sleap_io/io/main.py
def load_geojson(filename: str) -> list:
    """Load ROIs from a GeoJSON file.

    Args:
        filename: Path to a ``.geojson`` file containing ROI features.

    Returns:
        A list of `ROI` objects.

    See Also:
        `ROI`: Region of interest data structure.
        `save_geojson`: Write ROIs to GeoJSON.
    """
    from sleap_io.io import geojson

    return geojson.read_rois(filename)

sleap_io.io.main.save_geojson(rois, filename)

Save ROIs to a GeoJSON file.

Parameters:

Name Type Description Default
rois list

A list of ROI objects to save.

required
filename str

Path to the output .geojson file.

required
See Also

ROI: Region of interest data structure. load_geojson: Read ROIs from GeoJSON.

Source code in sleap_io/io/main.py
def save_geojson(rois: list, filename: str) -> None:
    """Save ROIs to a GeoJSON file.

    Args:
        rois: A list of `ROI` objects to save.
        filename: Path to the output ``.geojson`` file.

    See Also:
        `ROI`: Region of interest data structure.
        `load_geojson`: Read ROIs from GeoJSON.
    """
    from sleap_io.io import geojson

    geojson.write_rois(rois, filename)