Skip to content

Commit 38613aa

Browse files
authored
refactor: move osm index cache location from local to global (#174)
1 parent 9d41249 commit 38613aa

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Moved location of the OSM extracts providers to the global cache [#173](https://github.com/kraina-ai/quackosm/issues/173)
13+
1014
## [0.11.2] - 2024-10-14
1115

1216
### Added

quackosm/osm_extracts/extract.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from pathlib import Path
77
from typing import TYPE_CHECKING, Callable, cast
88

9+
import platformdirs
10+
911
from quackosm._constants import WGS84_CRS
1012

1113
if TYPE_CHECKING: # pragma: no cover
@@ -56,14 +58,21 @@ def load_index_decorator(
5658

5759
def inner(function: Callable[[], "GeoDataFrame"]) -> Callable[[], "GeoDataFrame"]:
5860
def wrapper() -> "GeoDataFrame":
59-
cache_file_path = _get_cache_file_path(extract_source)
61+
global_cache_file_path = _get_global_cache_file_path(extract_source)
6062
expected_columns = ["id", "name", "file_name", "parent", "geometry", "area", "url"]
6163

6264
# Check if index exists in cache
63-
if cache_file_path.exists():
65+
if global_cache_file_path.exists():
66+
import geopandas as gpd
67+
68+
index_gdf = gpd.read_file(global_cache_file_path)
69+
elif (local_cache_file_path := _get_local_cache_file_path(extract_source)).exists():
70+
import shutil
71+
6472
import geopandas as gpd
6573

66-
index_gdf = gpd.read_file(cache_file_path)
74+
shutil.copy(local_cache_file_path, global_cache_file_path)
75+
index_gdf = gpd.read_file(global_cache_file_path)
6776
# Download index
6877
else: # pragma: no cover
6978
index_gdf = function()
@@ -87,14 +96,14 @@ def wrapper() -> "GeoDataFrame":
8796
stacklevel=0,
8897
)
8998
# Invalidate previous cached index
90-
cache_file_path.replace(cache_file_path.with_suffix(".geojson.old"))
99+
global_cache_file_path.replace(global_cache_file_path.with_suffix(".geojson.old"))
91100
# Download index again
92101
index_gdf = wrapper()
93102

94103
# Save index to cache
95-
if not cache_file_path.exists():
96-
cache_file_path.parent.mkdir(parents=True, exist_ok=True)
97-
index_gdf[expected_columns].to_file(cache_file_path, driver="GeoJSON")
104+
if not global_cache_file_path.exists():
105+
global_cache_file_path.parent.mkdir(parents=True, exist_ok=True)
106+
index_gdf[expected_columns].to_file(global_cache_file_path, driver="GeoJSON")
98107

99108
return index_gdf
100109

@@ -112,7 +121,14 @@ def extracts_to_geodataframe(extracts: list[OpenStreetMapExtract]) -> "GeoDataFr
112121
).set_crs(WGS84_CRS)
113122

114123

115-
def _get_cache_file_path(extract_source: OsmExtractSource) -> Path:
124+
def _get_global_cache_file_path(extract_source: OsmExtractSource) -> Path:
125+
return (
126+
Path(platformdirs.user_cache_dir("QuackOSM"))
127+
/ f"{extract_source.value.lower()}_index.geojson"
128+
)
129+
130+
131+
def _get_local_cache_file_path(extract_source: OsmExtractSource) -> Path:
116132
return Path(f"cache/{extract_source.value.lower()}_index.geojson")
117133

118134

tests/base/test_osm_extracts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
find_smallest_containing_extracts_total,
2828
get_extract_by_query,
2929
)
30-
from quackosm.osm_extracts.extract import _get_cache_file_path, _get_full_file_name_function
30+
from quackosm.osm_extracts.extract import _get_full_file_name_function, _get_global_cache_file_path
3131
from quackosm.osm_extracts.geofabrik import _load_geofabrik_index
3232

3333
ut = TestCase()
@@ -224,15 +224,15 @@ def test_uncovered_geometry_extract(
224224

225225
def test_proper_cache_saving() -> None:
226226
"""Test if file is saved in cache properly."""
227-
save_path = _get_cache_file_path(OsmExtractSource.geofabrik)
227+
save_path = _get_global_cache_file_path(OsmExtractSource.geofabrik)
228228
loaded_index = _load_geofabrik_index()
229229
assert save_path.exists()
230230
assert len(loaded_index.columns) == 7
231231

232232

233233
def test_wrong_cached_index() -> None:
234234
"""Test if cached file with missing columns is redownloaded again."""
235-
save_path = _get_cache_file_path(OsmExtractSource.geofabrik)
235+
save_path = _get_global_cache_file_path(OsmExtractSource.geofabrik)
236236
column_to_remove = "id"
237237

238238
# load index first time

0 commit comments

Comments
 (0)