Skip to content

Commit d09e76d

Browse files
giswqsCopilot
andauthored
Add close_db_connections method (#1261)
* Add close_db_connections method * Update leafmap/common.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update leafmap/common.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update leafmap/maplibregl.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2512340 commit d09e76d

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ docs/cache/
4343
docs/changelog_update.md
4444
**/*.las
4545
*.parquet
46+
*.zip
47+
*.db
4648
# docs/*.html
4749
cache/
4850
testing/

docs/maplibre/duckdb_layer.ipynb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@
9393
"m"
9494
]
9595
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"metadata": {},
100+
"outputs": [],
101+
"source": [
102+
"m.close_db_connections()"
103+
]
104+
},
96105
{
97106
"cell_type": "markdown",
98107
"metadata": {},
@@ -141,6 +150,15 @@
141150
")\n",
142151
"m"
143152
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": [
160+
"m.close_db_connections()"
161+
]
144162
}
145163
],
146164
"metadata": {
@@ -159,7 +177,7 @@
159177
"name": "python",
160178
"nbconvert_exporter": "python",
161179
"pygments_lexer": "ipython3",
162-
"version": "3.12.2"
180+
"version": "3.12.9"
163181
}
164182
},
165183
"nbformat": 4,

leafmap/maplibregl.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def __init__(
243243
self._deck_layers = []
244244
self._deck_layer_ids = []
245245
self._deck_layer_tooltips = {}
246+
self._duckdb_databases = [] # Track database paths for cleanup
246247

247248
if projection.lower() == "globe":
248249
self.add_globe_control()
@@ -3332,6 +3333,10 @@ def add_duckdb_layer(
33323333
src_crs=src_crs,
33333334
)
33343335

3336+
# Track the database path for cleanup
3337+
if db_path not in self._duckdb_databases:
3338+
self._duckdb_databases.append(db_path)
3339+
33353340
# Create tile URL
33363341
# Auto-configure for JupyterHub (like get_local_tile_url does)
33373342
import os as _os
@@ -3546,6 +3551,47 @@ def add_duckdb_layer(
35463551

35473552
traceback.print_exc()
35483553

3554+
def close_db_connections(self, database_path: str = None, quiet: bool = False):
3555+
"""
3556+
Close DuckDB connections for databases used by this map.
3557+
3558+
This method closes all connections in the connection pool for the specified
3559+
database or all databases used by this map instance, allowing other programs
3560+
to access the database files. This is useful when you're done using the
3561+
database and want to release the file lock.
3562+
3563+
Args:
3564+
database_path (str, optional): Path to the DuckDB database file.
3565+
If None, closes connections for all databases used by this map.
3566+
Defaults to None.
3567+
quiet (bool, optional): If True, suppress status messages. Defaults to False.
3568+
3569+
Returns:
3570+
None
3571+
3572+
Example:
3573+
>>> import leafmap.maplibregl as leafmap
3574+
>>> m = leafmap.Map()
3575+
>>> m.add_duckdb_layer("tiles.db")
3576+
>>> # Later, close the connections
3577+
>>> m.close_db_connections()
3578+
>>> # Or close connections for a specific database
3579+
>>> m.close_db_connections("tiles.db")
3580+
"""
3581+
from .common import close_duckdb_connections
3582+
3583+
if database_path is None:
3584+
# Close connections for all databases used by this map
3585+
for db_path in self._duckdb_databases:
3586+
close_duckdb_connections(db_path, quiet=quiet)
3587+
else:
3588+
# Close connections for specific database
3589+
if database_path in self._duckdb_databases:
3590+
close_duckdb_connections(database_path, quiet=quiet)
3591+
else:
3592+
if not quiet:
3593+
print(f"Database not tracked by this map: {database_path}")
3594+
35493595
def add_marker(
35503596
self,
35513597
marker: Marker = None,

0 commit comments

Comments
 (0)