utils
This module contains general utility functions.
create_logger #
create_logger(logger_name: str) -> Logger
Creates a logger object using input name parameter that outputs to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger_name
|
str
|
Name of logger |
required |
Returns:
Source code in src/geospatial_tools/utils.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
get_yaml_config #
get_yaml_config(yaml_config_file: str, logger: Logger = LOGGER) -> dict
This function takes in the path, or name of the file if it can be found in the config/ folder, with of without the extension, and returns the values of the file in a dictionary format.
Ex. For a file named app_config.yml (or app_config.yaml), directly in the config/ folder,
the function could be called like so : params = get_yaml_config('app_config')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_config_file
|
str
|
Path to yaml config file. If config file is in the config folder, you can use the file's name without the extension. |
required |
logger
|
Logger
|
Logger to handle messaging, by default LOGGER |
LOGGER
|
Returns:
Source code in src/geospatial_tools/utils.py
64 65 66 67 68 69 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 95 96 97 98 99 100 101 102 103 104 105 | |
get_json_config #
get_json_config(json_config_file: str, logger=LOGGER) -> dict
This function takes in the path, or name of the file if it can be found in the config/ folder, with of without the extension, and returns the values of the file in a dictionary format.
Ex. For a file named app_config.json, directly in the config/ folder,
the function could be called like so : params = get_json_config('app_config')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_config_file
|
str
|
Path to JSON config file. If config file is in the config folder, |
required |
logger
|
Logger to handle messaging |
LOGGER
|
Returns:
Source code in src/geospatial_tools/utils.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
create_crs #
create_crs(dataset_crs: str | int, logger=LOGGER)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_crs
|
str | int
|
EPSG code in string or int format. Can be given in the following ways: 5070 | "5070" | "EPSG:5070" |
required |
logger
|
Logger instance (Default value = LOGGER) |
LOGGER
|
Returns:
Source code in src/geospatial_tools/utils.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
download_url #
download_url(
url: str,
filename: str | Path,
overwrite: bool = False,
headers: dict | None = None,
logger=LOGGER,
) -> Path | None
This function downloads a file from a given URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Url to download |
required |
filename
|
str | Path
|
Filename (or full path) to save the downloaded file |
required |
overwrite
|
bool
|
If True, overwrite existing file |
False
|
headers
|
dict | None
|
Optional headers to include in the request (e.g., Authorization) |
None
|
logger
|
Logger instance |
LOGGER
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to downloaded file |
Source code in src/geospatial_tools/utils.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
unzip_file #
unzip_file(
zip_path: str | Path, extract_to: str | Path, logger: Logger = LOGGER
) -> list[str | Path]
This function unzips an archive to a specific directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zip_path
|
str | Path
|
Path to zip file |
required |
extract_to
|
str | Path
|
Path of directory to extract the zip file |
required |
logger
|
Logger
|
Logger instance |
LOGGER
|
Returns:
Source code in src/geospatial_tools/utils.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
parse_gzip_header #
parse_gzip_header(path: str | Path) -> dict[str, Any]
Parse the gzip header at the beginning of path (first member only).
Raises ValueError if file doesn't look like gzip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to gzip file |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Returns a dict with keys - compression_method (int) - flags (int) - mtime (int, Unix epoch or 0) - xflags (int) - os (int) - original_name (Optional[str]) # FNAME - comment (Optional[str]) # FCOMMENT - header_end_offset (int) # file offset where compressed data starts |
Source code in src/geospatial_tools/utils.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |