Skip to content

Export Utilities

Exports the structured dataframe to a standard CSV file.

Parameters:

Name Type Description Default
data_package CrimeDataPackage

A CrimeDataPackage wrapper containing the target DataFrame and metadata.

required
output_dir str

The destination directory for the exported file. Defaults to current directory.

'.'

Returns:

Name Type Description
bool bool

True if export succeeds, False otherwise.

Source code in src/geocrime_stl/utils/export.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def export_to_csv(data_package: CrimeDataPackage, output_dir: str = ".") -> bool:
    """Exports the structured dataframe to a standard CSV file.

    Args:
        data_package: A CrimeDataPackage wrapper containing the target DataFrame and metadata.
        output_dir: The destination directory for the exported file. Defaults to current directory.

    Returns:
        bool: True if export succeeds, False otherwise.
    """
    df = data_package.df
    month = data_package.month
    year = data_package.year

    filename = f"stl_crime_data_{month:02d}_{year}.csv"
    full_path = os.path.join(output_dir, filename)

    try:
        # If geometry somehow sneaked back into the dataframe, drop it safely
        df_to_export = df.drop(columns=["geometry"], errors="ignore")
        df_to_export.to_csv(full_path, index=False)
        logger.info("CSV successfully written to %s", full_path)
        return True
    except PermissionError:
        logger.error("Cannot write CSV. Is %s open in Excel?", full_path)
        return False
    except Exception as e:
        logger.error("Error exporting CSV to %s: %e", full_path, e)
        return False

Reconstructs spatial points and exports to a standardized GeoJSON file.

Parameters:

Name Type Description Default
data_package CrimeDataPackage

A CrimeDataPackage wrapper containing the target DataFrame and metadata.

required
output_dir str

The destination directory for the exported file. Defaults to current directory.

'.'

Returns:

Name Type Description
bool bool

True if export succeeds, False otherwise.

Source code in src/geocrime_stl/utils/export.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def export_to_geojson(data_package: CrimeDataPackage, output_dir: str = ".") -> bool:
    """Reconstructs spatial points and exports to a standardized GeoJSON file.

    Args:
        data_package: A CrimeDataPackage wrapper containing the target DataFrame and metadata.
        output_dir: The destination directory for the exported file. Defaults to current directory.

    Returns:
        bool: True if export succeeds, False otherwise.
    """
    df = data_package.df
    month = data_package.month
    year = data_package.year

    filename = f"stl_crime_data_{month:02d}_{year}.geojson"
    full_path = os.path.join(output_dir, filename)

    try:
        gdf = _build_spatial_gdf(df)
        gdf.to_file(full_path, driver="GeoJSON")
        logger.info("GeoJSON successfully written to %s", full_path)
        return True
    except Exception as e:
        logger.error("Error exporting GeoJSON to %s: %e", full_path, e)
        return False

Reconstructs spatial points and exports to a standardized GeoPackage layer.

Parameters:

Name Type Description Default
data_package CrimeDataPackage

A CrimeDataPackage wrapper containing the target DataFrame and metadata.

required
output_dir str

The destination directory for the exported file. Defaults to current directory.

'.'

Returns:

Name Type Description
bool bool

True if export succeeds, False otherwise.

Source code in src/geocrime_stl/utils/export.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def export_to_gpkg(data_package: CrimeDataPackage, output_dir: str = ".") -> bool:
    """Reconstructs spatial points and exports to a standardized GeoPackage layer.

    Args:
        data_package: A CrimeDataPackage wrapper containing the target DataFrame and metadata.
        output_dir: The destination directory for the exported file. Defaults to current directory.

    Returns:
        bool: True if export succeeds, False otherwise.
    """
    df = data_package.df
    month = data_package.month
    year = data_package.year

    filename = f"stl_crime_data_{month:02d}_{year}.gpkg"
    full_path = os.path.join(output_dir, filename)
    layer_name = f"stl_crime_{month:02d}_{year}"

    try:
        gdf = _build_spatial_gdf(df)
        gdf.to_file(full_path, driver="GPKG", layer=layer_name)
        logger.info("GeoPackage successfully written to %s (Layer: %s)", full_path, layer_name)
        return True
    except PermissionError:
        logger.error("Cannot write GPKG. Is %s locked by GIS software?", full_path)
        return False
    except Exception as e:
        logger.error("Error exporting GeoPackage to %s: %e", full_path, e)
        return False