Skip to content

Commit

Permalink
Always use id as pk, refs #4
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 17, 2020
1 parent 8fa7277 commit 22a5611
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
4 changes: 1 addition & 3 deletions shapefile_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
nargs=-1,
)
@click.option("--table", help="Table to load data into")
@click.option("--pk", help="Column to use as a primary key")
@click.option("--alter", is_flag=True, help="Add any missing columns")
@click.option("--spatialite", is_flag=True, help="Use SpatiaLite")
@click.option(
"--spatialite_mod",
help="Path to SpatiaLite module, for if --spatialite cannot find it automatically",
)
def cli(db_path, table, shapefile, pk, alter, spatialite, spatialite_mod):
def cli(db_path, table, shapefile, alter, spatialite, spatialite_mod):
"Load shapefiles into a SQLite (optionally SpatiaLite) database"
for filepath in shapefile:
openpath = filepath
Expand All @@ -39,7 +38,6 @@ def cli(db_path, table, shapefile, pk, alter, spatialite, spatialite_mod):
db_path,
table=table or Path(filepath).stem,
features=bar,
pk=pk,
alter=alter,
spatialite=spatialite,
spatialite_mod=spatialite_mod,
Expand Down
8 changes: 2 additions & 6 deletions shapefile_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def import_features(
db_path,
table,
features,
pk=None,
alter=False,
spatialite=False,
spatialite_mod=None,
Expand Down Expand Up @@ -63,14 +62,11 @@ def yield_features():
features_iter = itertools.chain(first_100, features_iter)
column_types = sqlite_utils.suggest_column_types(first_100)
column_types.pop("geometry")
db[table].create(column_types, pk=pk)
db[table].create(column_types, pk="id")
ensure_table_has_geometry(db, table)
conversions = {"geometry": "GeomFromText(?, 4326)"}

if pk:
db[table].upsert_all(features_iter, conversions=conversions, pk=pk, alter=alter)
else:
db[table].insert_all(features_iter, conversions=conversions, alter=alter)
db[table].insert_all(features_iter, conversions=conversions, alter=alter, pk="id", replace=True)
return db[table]


Expand Down
2 changes: 1 addition & 1 deletion tests/test_shapefile_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ def test_import_features_spatialite(tmpdir):
},
]

assert ["rowid"] == db["features"].pks
assert ["id"] == db["features"].pks
assert expected_rows == rows

0 comments on commit 22a5611

Please sign in to comment.