mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-25 18:34:32 +02:00
New spatialite helper methods, closes #79
- db.init_spatialite() - table.add_geometry_column() - table.create_spatial_index() Co-authored-by: Simon Willison <swillison@gmail.com>
This commit is contained in:
parent
9dcb099905
commit
ee11274fcb
6 changed files with 267 additions and 33 deletions
83
tests/test_gis.py
Normal file
83
tests/test_gis.py
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import pytest
|
||||
from sqlite_utils.utils import find_spatialite
|
||||
from sqlite_utils.db import Database
|
||||
from sqlite_utils.utils import sqlite3
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.skipif(
|
||||
not find_spatialite(), reason="Could not find SpatiaLite extension"
|
||||
),
|
||||
pytest.mark.skipif(
|
||||
not hasattr(sqlite3.Connection, "enable_load_extension"),
|
||||
reason="sqlite3.Connection missing enable_load_extension",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def test_find_spatialite():
|
||||
spatialite = find_spatialite()
|
||||
assert spatialite is None or isinstance(spatialite, str)
|
||||
|
||||
|
||||
def test_init_spatialite():
|
||||
db = Database(memory=True)
|
||||
spatialite = find_spatialite()
|
||||
db.init_spatialite(spatialite)
|
||||
assert "spatial_ref_sys" in db.table_names()
|
||||
|
||||
|
||||
def test_add_geometry_column():
|
||||
db = Database(memory=True)
|
||||
spatialite = find_spatialite()
|
||||
db.init_spatialite(spatialite)
|
||||
|
||||
# create a table first
|
||||
table = db.create_table("locations", {"id": str, "properties": str})
|
||||
table.add_geometry_column(
|
||||
column_name="geometry",
|
||||
geometry_type="Point",
|
||||
srid=4326,
|
||||
coord_dimension=2,
|
||||
)
|
||||
|
||||
assert db["geometry_columns"].get(["locations", "geometry"]) == {
|
||||
"f_table_name": "locations",
|
||||
"f_geometry_column": "geometry",
|
||||
"geometry_type": 1, # point
|
||||
"coord_dimension": 2,
|
||||
"srid": 4326,
|
||||
"spatial_index_enabled": 0,
|
||||
}
|
||||
|
||||
|
||||
def test_create_spatial_index():
|
||||
db = Database(memory=True)
|
||||
spatialite = find_spatialite()
|
||||
assert db.init_spatialite(spatialite)
|
||||
|
||||
# create a table, add a geometry column with default values
|
||||
table = db.create_table("locations", {"id": str, "properties": str})
|
||||
assert table.add_geometry_column("geometry", "Point")
|
||||
|
||||
# index it
|
||||
assert table.create_spatial_index("geometry")
|
||||
|
||||
assert "idx_locations_geometry" in db.table_names()
|
||||
|
||||
|
||||
def test_double_create_spatial_index():
|
||||
db = Database(memory=True)
|
||||
spatialite = find_spatialite()
|
||||
db.init_spatialite(spatialite)
|
||||
|
||||
# create a table, add a geometry column with default values
|
||||
table = db.create_table("locations", {"id": str, "properties": str})
|
||||
table.add_geometry_column("geometry", "Point")
|
||||
|
||||
# index it, return True
|
||||
assert table.create_spatial_index("geometry")
|
||||
|
||||
assert "idx_locations_geometry" in db.table_names()
|
||||
|
||||
# call it again, return False
|
||||
assert not table.create_spatial_index("geometry")
|
||||
|
|
@ -22,11 +22,6 @@ def test_decode_base64_values(input, expected, should_be_is):
|
|||
assert actual == expected
|
||||
|
||||
|
||||
def test_find_spatialite():
|
||||
spatialite = utils.find_spatialite()
|
||||
assert spatialite is None or isinstance(spatialite, str)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"size,expected",
|
||||
(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue