Skip to content

Renderers

to_geojson(df, x_key='longitude', y_key='latitude')

Converts a DataFrame to GeoJSON.

Parameters:

Name Type Description Default
df DataFrame

The DataFrame to convert.

required
x_key str

The key in the DataFrame that contains the longitude. Defaults to "longitude".

'longitude'
y_key str

The key in the DataFrame that contains the latitude. Defaults to "latitude".

'latitude'

Returns:

Type Description
Optional[str]

Optional[str]: The GeoJSON representation of the DataFrame.

Examples:

from seasnake import MermaidAuth, FishBeltTransect, to_geojson

auth = MermaidAuth()
fish_belt = FishBeltTransect(token=auth.get_token())
project_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"
geojson = to_geojson(fish_belt.sample_events(project_id))
print(geojson)
Source code in seasnake/io.py
def to_geojson(
    df: DataFrame, x_key: str = "longitude", y_key: str = "latitude"
) -> Optional[str]:
    """Converts a DataFrame to GeoJSON.

    Args:
        df (DataFrame): The DataFrame to convert.
        x_key (str, optional): The key in the DataFrame that contains the longitude.
            Defaults to "longitude".
        y_key (str, optional): The key in the DataFrame that contains the
            latitude. Defaults to "latitude".

    Returns:
        Optional[str]: The GeoJSON representation of the DataFrame.

    Examples:
    ```
    from seasnake import MermaidAuth, FishBeltTransect, to_geojson

    auth = MermaidAuth()
    fish_belt = FishBeltTransect(token=auth.get_token())
    project_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"
    geojson = to_geojson(fish_belt.sample_events(project_id))
    print(geojson)
    ```

    """

    gdf = _to_geodataframe(df, x_key, y_key)
    return None if gdf is None else gdf.to_json()