Make GIS helpers into methods on Database and Table. Remove gis.py.

This commit is contained in:
Chris Amico 2022-02-03 15:23:21 -05:00
commit 481eb60a1b
7 changed files with 179 additions and 186 deletions

View file

@ -7,8 +7,7 @@ from unittest import mock
import json
import os
import pytest
from sqlite_utils.utils import sqlite3
from sqlite_utils.gis import find_spatialite
from sqlite_utils.utils import sqlite3, find_spatialite
import textwrap
from .utils import collapse_whitespace

View file

@ -1,53 +1,46 @@
import pytest
from sqlite_utils import gis
from sqlite_utils.utils import find_spatialite
from sqlite_utils.db import Database
from sqlite_utils.utils import sqlite3
@pytest.mark.skipif(
not gis.find_spatialite(), reason="Could not find SpatiaLite extension"
)
@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 = gis.find_spatialite()
spatialite = find_spatialite()
assert spatialite is None or isinstance(spatialite, str)
@pytest.mark.skipif(
not gis.find_spatialite(), reason="Could not find SpatiaLite extension"
)
@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_init_spatialite():
db = Database(memory=True)
spatialite = gis.find_spatialite()
gis.init_spatialite(db, spatialite)
spatialite = find_spatialite()
db.init_spatialite(spatialite)
assert "spatial_ref_sys" in db.table_names()
@pytest.mark.skipif(
not gis.find_spatialite(), reason="Could not find SpatiaLite extension"
)
@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_add_geometry_column():
db = Database(memory=True)
spatialite = gis.find_spatialite()
gis.init_spatialite(db, spatialite)
spatialite = find_spatialite()
db.init_spatialite(spatialite)
# create a table first
db.create_table("locations", {"id": str, "properties": str})
gis.add_geometry_column(
db["locations"],
geometry_type="Point",
table = db.create_table("locations", {"id": str, "properties": str})
table.add_geometry_column(
column_name="geometry",
geometry_type="Point",
srid=4326,
coord_dimension=2,
)
@ -62,48 +55,44 @@ def test_add_geometry_column():
}
@pytest.mark.skipif(
not gis.find_spatialite(), reason="Could not find SpatiaLite extension"
)
@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_create_spatial_index():
db = Database(memory=True)
spatialite = gis.find_spatialite()
assert gis.init_spatialite(db, spatialite)
spatialite = find_spatialite()
assert db.init_spatialite(spatialite)
# create a table, add a geometry column with default values
db.create_table("locations", {"id": str, "properties": str})
assert gis.add_geometry_column(db["locations"], "Point", "geometry")
table = db.create_table("locations", {"id": str, "properties": str})
assert table.add_geometry_column("geometry", "Point")
# index it
assert gis.create_spatial_index(db["locations"], "geometry")
assert table.create_spatial_index("geometry")
assert "idx_locations_geometry" in db.table_names()
@pytest.mark.skipif(
not gis.find_spatialite(), reason="Could not find SpatiaLite extension"
)
@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_double_create_spatial_index():
db = Database(memory=True)
spatialite = gis.find_spatialite()
gis.init_spatialite(db, spatialite)
spatialite = find_spatialite()
db.init_spatialite(spatialite)
# create a table, add a geometry column with default values
db.create_table("locations", {"id": str, "properties": str})
gis.add_geometry_column(db["locations"], "Point", "geometry")
table = db.create_table("locations", {"id": str, "properties": str})
table.add_geometry_column("geometry", "Point")
# index it, return True
assert gis.create_spatial_index(db["locations"], "geometry")
assert table.create_spatial_index("geometry")
assert "idx_locations_geometry" in db.table_names()
# call it again, return False
assert not gis.create_spatial_index(db["locations"], "geometry")
assert not table.create_spatial_index("geometry")