Skip to content

Commit

Permalink
Fix for table creation with spatialite, closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2020
1 parent f258ed1 commit aad086d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
32 changes: 21 additions & 11 deletions geojson_to_sqlite/utils.py
@@ -1,5 +1,6 @@
from shapely.geometry import shape
import sqlite_utils
import itertools
import os

SPATIALITE_PATHS = (
Expand All @@ -22,16 +23,6 @@ def import_features(
spatialite_mod=None,
):
db = sqlite_utils.Database(db_path)
conversions = {}
if spatialite_mod:
spatialite = True
if spatialite:
lib = spatialite_mod or find_spatialite()
if not lib:
raise SpatiaLiteError("Could not find SpatiaLite module")
init_spatialite(db, lib)
ensure_table_has_geometry(db, table)
conversions = {"geometry": "GeomFromText(?, 4326)"}

def yield_records():
for feature in features:
Expand All @@ -42,6 +33,25 @@ def yield_records():
record["geometry"] = feature["geometry"]
yield record

conversions = {}
if spatialite_mod:
spatialite = True
if spatialite:
lib = spatialite_mod or find_spatialite()
if not lib:
raise SpatiaLiteError("Could not find SpatiaLite module")
init_spatialite(db, lib)
if table not in db.table_names():
# Create the table, using detected column types
column_types = db[table].detect_column_types(
itertools.islice(yield_records(), 0, 100)
)
column_types.pop("geometry")
db[table].create(column_types, pk=pk)
ensure_table_has_geometry(db, table)
# conversions = {"geometry": "CastToMultiPolygon(GeomFromText(?, 4326))"}
conversions = {"geometry": "GeomFromText(?, 4326)"}

if pk:
db[table].upsert_all(
yield_records(), conversions=conversions, pk=pk, alter=alter
Expand Down Expand Up @@ -70,5 +80,5 @@ def init_spatialite(db, lib):
def ensure_table_has_geometry(db, table):
if "geometry" not in db[table].columns_dict:
db.conn.execute(
"SELECT AddGeometryColumn(?, 'geometry', 4326, 'MULTIPOLYGON', 2);", [table]
"SELECT AddGeometryColumn(?, 'geometry', 4326, 'GEOMETRY', 2);", [table]
)
28 changes: 25 additions & 3 deletions tests/test_geojson_to_sqlite.py
Expand Up @@ -39,7 +39,7 @@ def test_single_feature(tmpdir):


@pytest.mark.skipif(not utils.find_spatialite(), reason="Could not find SpatiaLite")
def test_feature_collection_sqlite(tmpdir):
def test_feature_collection_spatialite(tmpdir):
db_path = str(tmpdir / "output.db")
result = CliRunner().invoke(
cli.cli,
Expand All @@ -48,7 +48,10 @@ def test_feature_collection_sqlite(tmpdir):
"features",
str(testdir / "feature-collection.geojson"),
"--spatialite",
"--alter",
"--pk=slug",
],
catch_exceptions=False,
)
assert 0 == result.exit_code, result.stdout
db = sqlite_utils.Database(db_path)
Expand All @@ -57,7 +60,7 @@ def test_feature_collection_sqlite(tmpdir):
rows = db.execute_returning_dicts(
"select slug, AsGeoJSON(geometry) as geometry from features"
)
assert [
expected_rows = [
{
"slug": "uk",
"geometry": '{"type":"Polygon","coordinates":[[[-8.0859375,60.93043220292332],[-16.875,50.28933925329177],[-5.9765625,48.92249926375824],[4.21875,52.26815737376816],[1.0546875,60.06484046010452],[-8.0859375,60.93043220292332]]]}',
Expand All @@ -66,7 +69,26 @@ def test_feature_collection_sqlite(tmpdir):
"slug": "usa",
"geometry": '{"type":"Polygon","coordinates":[[[-129.375,47.75409797968003],[-119.53125,33.43144133557529],[-96.6796875,25.48295117535531],[-85.4296875,24.20688962239802],[-77.34374999999998,25.48295117535531],[-61.52343749999999,44.33956524809713],[-84.375,51.39920565355377],[-100.8984375,50.06419173665909],[-115.3125,50.73645513701067],[-129.375,47.75409797968003]]]}',
},
] == rows
]
assert ["slug"] == db["features"].pks
assert expected_rows == rows
# Run it once more to check that upserting should work
result = CliRunner().invoke(
cli.cli,
[
db_path,
"features",
str(testdir / "feature-collection.geojson"),
"--spatialite",
"--alter",
"--pk=slug",
],
catch_exceptions=False,
)
assert 0 == result.exit_code
assert expected_rows == db.execute_returning_dicts(
"select slug, AsGeoJSON(geometry) as geometry from features"
)


def test_feature_collection(tmpdir):
Expand Down

0 comments on commit aad086d

Please sign in to comment.