Skip to content

Commit

Permalink
Accept more than one GeoJSON file, closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 13, 2022
1 parent 71120f2 commit 00b1c4f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ The table will be created the first time you run the command.

On subsequent runs you can use the `--alter` option to add any new columns that are missing from the table.

You can pass more than one GeoJSON file, in which case the contents of all of the files will be inserted into the same table.

If your features have an `"id"` property it will be used as the primary key for the table. You can also use `--pk=PROPERTY` with the name of a different property to use that as the primary key instead. If you don't want to use the `"id"` as the primary key (maybe it contains duplicate values) you can use `--pk ''` to specify no primary key.

Specifying a primary key also will allow you to upsert data into the rows instead of insert data into new rows.
Expand Down
42 changes: 25 additions & 17 deletions geojson_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
required=True,
)
@click.argument("table", required=True)
@click.argument("geojson", type=click.File(), required=True)
@click.argument("geojson", type=click.File(), required=True, nargs=-1)
@click.option("--nl", is_flag=True, help="Use newline-delimited GeoJSON features")
@click.option("--pk", help="Column to use as a primary key")
@click.option("--alter", is_flag=True, help="Add any missing columns")
Expand Down Expand Up @@ -42,20 +42,28 @@ def cli(
spatial_index,
spatialite_mod,
):
"Import GeoJSON into a SQLite database" ""
try:
features = utils.get_features(geojson, nl)
except (TypeError, ValueError) as e:
raise click.ClickException(str(e))
"""
Import GeoJSON into a SQLite database
utils.import_features(
db_path,
table,
features,
pk=pk,
alter=alter,
properties=properties,
spatialite=spatialite,
spatialite_mod=spatialite_mod,
spatial_index=spatial_index,
)
To insert cities.geojson into a cities table in my.db:
geojson-to-sqlite my.db cities cities.geojson
This command can be passed more than one GeoJSON file
"""
for file in geojson:
try:
features = utils.get_features(file, nl)
utils.import_features(
db_path,
table,
features,
pk=pk,
alter=alter,
properties=properties,
spatialite=spatialite,
spatialite_mod=spatialite_mod,
spatial_index=spatial_index,
)
except (TypeError, ValueError) as e:
raise click.ClickException(str(e))
18 changes: 18 additions & 0 deletions tests/test_geojson_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ def test_feature_collection(tmpdir):
assert ["rowid"] == db["features"].pks


def test_multiple_files(tmpdir):
db_path = str(tmpdir / "output.db")
result = CliRunner().invoke(
cli.cli,
[
db_path,
"features",
str(testdir / "feature-collection.geojson"),
str(testdir / "feature.geojson"),
],
)
assert 0 == result.exit_code, result.stdout
db = sqlite_utils.Database(db_path)
assert ["features"] == db.table_names()
rows = list(db["features"].rows)
assert sorted([r["slug"] for r in rows]) == ["uk", "uk", "usa"]


def test_feature_collection_pk_and_alter(tmpdir):
db_path = str(tmpdir / "output.db")
result = CliRunner().invoke(
Expand Down

0 comments on commit 00b1c4f

Please sign in to comment.