Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ def create_database(path, enable_wal, init_spatialite, load_extension):

# load spatialite from expected locations and initialize metadata
if init_spatialite:
db.init_spatialite()
db.init_spatialite(find_spatialite() or load_extension)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this pattern.

The init_spatialite() function already calls find_spatialite() if necessary:

if path is None:
path = find_spatialite()
self.conn.enable_load_extension(True)
self.conn.load_extension(path)

The load_extension option is a bit different: It's actually a list of extensions passed using --load-extension path on the command-line, which is used by this code:

def _load_extensions(db, load_extension):
if load_extension:
db.conn.enable_load_extension(True)
for ext in load_extension:
if ext == "spatialite" and not os.path.exists(ext):
ext = find_spatialite()
if ":" in ext:
path, _, entrypoint = ext.partition(":")
db.conn.execute("SELECT load_extension(?, ?)", [path, entrypoint])
else:
db.conn.load_extension(ext)


db.vacuum()

Expand Down Expand Up @@ -2902,7 +2902,7 @@ def add_geometry_column(
# load spatialite, one way or another
if load_extension:
_load_extensions(db, load_extension)
db.init_spatialite()
db.init_spatialite(find_spatialite() or load_extension)

if db[table].add_geometry_column(
column_name, geometry_type, srid, coord_dimension, not_null
Expand Down Expand Up @@ -2934,7 +2934,7 @@ def create_spatial_index(db_path, table, column_name, load_extension):
# load spatialite
if load_extension:
_load_extensions(db, load_extension)
db.init_spatialite()
db.init_spatialite(find_spatialite() or load_extension)

if column_name not in db[table].columns_dict:
raise click.ClickException(
Expand Down
2 changes: 2 additions & 0 deletions sqlite_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
SPATIALITE_PATHS = (
"/usr/lib/x86_64-linux-gnu/mod_spatialite.so",
"/usr/local/lib/mod_spatialite.dylib",
"/usr/local/lib/mod_spatialite.so",
"/opt/homebrew/lib/mod_spatialite.dylib",
)

# Mainly so we can restore it if needed in the tests:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,30 @@ def test_cli_create_spatial_index(tmpdir):
assert 0 == result.exit_code

assert "idx_locations_geometry" in db.table_names()


def test_cli_create_spatial_index_with_path(tmpdir):
# create a rowid table with one column
db_path = tmpdir / "spatial.db"
db = Database(str(db_path))
db.init_spatialite()

table = db["locations"].create({"name": str})
table.add_geometry_column("geometry", "POINT")

path = find_spatialite()
result = CliRunner().invoke(
cli,
[
"create-spatial-index",
str(db_path),
table.name,
"geometry",
"--load-extension",
path,
],
)

assert 0 == result.exit_code

assert "idx_locations_geometry" in db.table_names()