├── .github └── workflows │ ├── publish.yml │ └── test.yml ├── .gitignore ├── LICENSE ├── README.md ├── geojson_to_sqlite ├── __init__.py ├── cli.py └── utils.py ├── setup.py └── tests ├── feature-collection-ids.geojson ├── feature-collection.geojson ├── feature.geojson ├── invalid.geojson ├── quakes.geojson ├── quakes.ndjson └── test_geojson_to_sqlite.py /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish Python Package 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | python-version: ["3.7", "3.8", "3.9", "3.10"] 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up Python ${{ matrix.python-version }} 16 | uses: actions/setup-python@v2 17 | with: 18 | python-version: ${{ matrix.python-version }} 19 | - uses: actions/cache@v2 20 | name: Configure pip caching 21 | with: 22 | path: ~/.cache/pip 23 | key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.py') }} 24 | restore-keys: | 25 | ${{ runner.os }}-pip- 26 | - name: Install SpatiaLite 27 | run: | 28 | sudo apt-get install libsqlite3-mod-spatialite 29 | - name: Install dependencies 30 | run: | 31 | pip install -e '.[test]' 32 | - name: Run tests 33 | run: | 34 | pytest 35 | deploy: 36 | runs-on: ubuntu-latest 37 | needs: [test] 38 | steps: 39 | - uses: actions/checkout@v2 40 | - name: Set up Python 41 | uses: actions/setup-python@v2 42 | with: 43 | python-version: "3.10" 44 | - uses: actions/cache@v2 45 | name: Configure pip caching 46 | with: 47 | path: ~/.cache/pip 48 | key: ${{ runner.os }}-publish-pip-${{ hashFiles('**/setup.py') }} 49 | restore-keys: | 50 | ${{ runner.os }}-publish-pip- 51 | - name: Install dependencies 52 | run: | 53 | pip install setuptools wheel twine build 54 | - name: Publish 55 | env: 56 | TWINE_USERNAME: __token__ 57 | TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} 58 | run: | 59 | python -m build 60 | twine upload dist/* 61 | 62 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | python-version: ["3.7", "3.8", "3.9", "3.10"] 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: Set up Python ${{ matrix.python-version }} 14 | uses: actions/setup-python@v2 15 | with: 16 | python-version: ${{ matrix.python-version }} 17 | - uses: actions/cache@v2 18 | name: Configure pip caching 19 | with: 20 | path: ~/.cache/pip 21 | key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.py') }} 22 | restore-keys: | 23 | ${{ runner.os }}-pip- 24 | - name: Install SpatiaLite 25 | run: | 26 | sudo apt-get install libsqlite3-mod-spatialite 27 | - name: Install dependencies 28 | run: | 29 | pip install -e '.[test]' 30 | - name: Run tests 31 | run: | 32 | pytest 33 | 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .venv 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | venv 6 | .eggs 7 | .pytest_cache 8 | *.egg-info 9 | *.db 10 | .vscode 11 | 12 | Pipfile 13 | Pipfile.lock 14 | build/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # geojson-to-sqlite 2 | 3 | [![PyPI](https://img.shields.io/pypi/v/geojson-to-sqlite.svg)](https://pypi.org/project/geojson-to-sqlite/) 4 | [![Changelog](https://img.shields.io/github/v/release/simonw/geojson-to-sqlite?include_prereleases&label=changelog)](https://github.com/simonw/geojson-to-sqlite/releases) 5 | [![Tests](https://github.com/simonw/geojson-to-sqlite/workflows/Test/badge.svg)](https://github.com/simonw/geojson-to-sqlite/actions?query=workflow%3ATest) 6 | [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/geojson-to-sqlite/blob/main/LICENSE) 7 | 8 | CLI tool for converting GeoJSON to SQLite (optionally with SpatiaLite) 9 | 10 | [RFC 7946: The GeoJSON Format](https://tools.ietf.org/html/rfc7946) 11 | 12 | ## How to install 13 | 14 | $ pip install geojson-to-sqlite 15 | 16 | ## How to use 17 | 18 | You can run this tool against a GeoJSON file like so: 19 | 20 | $ geojson-to-sqlite my.db features features.geojson 21 | 22 | This will load all of the features from the `features.geojson` file into a table called `features`. 23 | 24 | Each row will have a `geometry` column containing the feature geometry, and columns for each of the keys found in any `properties` attached to those features. (To bundle all properties into a single JSON object, use the `--properties` flag.) 25 | 26 | The table will be created the first time you run the command. 27 | 28 | On subsequent runs you can use the `--alter` option to add any new columns that are missing from the table. 29 | 30 | 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. 31 | 32 | 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. 33 | 34 | Specifying a primary key also will allow you to upsert data into the rows instead of insert data into new rows. 35 | 36 | If no primary key is specified, a SQLite `rowid` column will be used. 37 | 38 | You can use `-` as the filename to import from standard input. For example: 39 | 40 | $ curl https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_20m.json \ 41 | | geojson-to-sqlite my.db states - --pk GEO_ID 42 | 43 | ## Using with SpatiaLite 44 | 45 | By default, the `geometry` column will contain JSON. 46 | 47 | If you have installed the [SpatiaLite](https://www.gaia-gis.it/fossil/libspatialite/index) module for SQLite you can instead import the geometry into a geospatially indexed column. 48 | 49 | You can do this using the `--spatialite` option, like so: 50 | 51 | $ geojson-to-sqlite my.db features features.geojson --spatialite 52 | 53 | The tool will search for the SpatiaLite module in the following locations: 54 | 55 | - `/usr/lib/x86_64-linux-gnu/mod_spatialite.so` 56 | - `/usr/local/lib/mod_spatialite.dylib` 57 | 58 | If you have installed the module in another location, you can use the `--spatialite_mod=xxx` option to specify where: 59 | 60 | $ geojson-to-sqlite my.db features features.geojson \ 61 | --spatialite_mod=/usr/lib/mod_spatialite.dylib 62 | 63 | You can create a SpatiaLite spatial index on the `geometry` column using the `--spatial-index` option: 64 | 65 | $ geojson-to-sqlite my.db features features.geojson --spatial-index 66 | 67 | Using this option implies `--spatialite` so you do not need to add that. 68 | 69 | ## Streaming large datasets 70 | 71 | For large datasets, consider using newline-delimited JSON to stream features into the database without loading the entire feature collection into memory. 72 | 73 | For example, to load a day of earthquake reports from USGS: 74 | 75 | $ geojson-to-sqlite quakes.db quakes tests/quakes.ndjson \ 76 | --nl --pk=id --spatialite 77 | 78 | When using newline-delimited JSON, tables will also be created from the first feature, instead of guessing types based on the first 100 features. 79 | 80 | If you want to use a larger subset of your data to guess column types (for example, if some fields are inconsistent) you can use [fiona](https://fiona.readthedocs.io/en/latest/cli.html) to collect features into a single collection. 81 | 82 | $ head tests/quakes.ndjson | fio collect | \ 83 | geojson-to-sqlite quakes.db quakes - --spatialite 84 | 85 | This will take the first 10 lines from `tests/quakes.ndjson`, pass them to `fio collect`, which turns them into a single feature collection, and pass that, in turn, to `geojson-to-sqlite`. 86 | 87 | ## Using this with Datasette 88 | 89 | Databases created using this tool can be explored and published using [Datasette](https://datasette.readthedocs.io/). 90 | 91 | The Datasette documentation includes a section on [how to use it to browse SpatiaLite databases](https://datasette.readthedocs.io/en/stable/spatialite.html). 92 | 93 | The [datasette-leaflet-geojson](https://datasette.io/plugins/datasette-leaflet-geojson) plugin can be used to visualize columns containing GeoJSON geometries on a [Leaflet](https://leafletjs.com/) map. 94 | 95 | If you are using SpatiaLite you will need to output the geometry as GeoJSON in order for that plugin to work. You can do that using the SpaitaLite `AsGeoJSON()` function - something like this: 96 | 97 | ```sql 98 | select rowid, AsGeoJSON(geometry) from mytable limit 10 99 | ``` 100 | 101 | The [datasette-geojson-map](https://datasette.io/plugins/datasette-geojson-map) is an alternative plugin which will automatically render SpatiaLite geometries as a Leaflet map on the corresponding table page, without needing you to call `AsGeoJSON(geometry)`. 102 | -------------------------------------------------------------------------------- /geojson_to_sqlite/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonw/geojson-to-sqlite/9c6c1c8ce2f27c2e1374c9a5f2f90996268ade1f/geojson_to_sqlite/__init__.py -------------------------------------------------------------------------------- /geojson_to_sqlite/cli.py: -------------------------------------------------------------------------------- 1 | import click 2 | import sqlite_utils 3 | import json 4 | from . import utils 5 | 6 | import pdb 7 | 8 | 9 | @click.command() 10 | @click.version_option() 11 | @click.argument( 12 | "db_path", 13 | type=click.Path(file_okay=True, dir_okay=False, allow_dash=False), 14 | required=True, 15 | ) 16 | @click.argument("table", required=True) 17 | @click.argument("geojson", type=click.File(), required=True, nargs=-1) 18 | @click.option("--nl", is_flag=True, help="Use newline-delimited GeoJSON features") 19 | @click.option("--pk", help="Column to use as a primary key") 20 | @click.option("--alter", is_flag=True, help="Add any missing columns") 21 | @click.option( 22 | "--properties", 23 | is_flag=False, 24 | flag_value="properties", 25 | default="", 26 | help="Bundle properties into a JSON object called 'properties' or a custom name", 27 | ) 28 | @click.option("--spatialite", is_flag=True, help="Use SpatiaLite") 29 | @click.option("--spatial-index", is_flag=True, help="Create spatial indexes") 30 | @click.option( 31 | "--spatialite_mod", 32 | help="Path to SpatiaLite module, for if --spatialite cannot find it automatically", 33 | ) 34 | def cli( 35 | db_path, 36 | table, 37 | geojson, 38 | nl, 39 | pk, 40 | alter, 41 | properties, 42 | spatialite, 43 | spatial_index, 44 | spatialite_mod, 45 | ): 46 | """ 47 | Import GeoJSON into a SQLite database 48 | 49 | To insert cities.geojson into a cities table in my.db: 50 | 51 | geojson-to-sqlite my.db cities cities.geojson 52 | 53 | This command can be passed more than one GeoJSON file 54 | """ 55 | for file in geojson: 56 | try: 57 | features = utils.get_features(file, nl) 58 | utils.import_features( 59 | db_path, 60 | table, 61 | features, 62 | pk=pk, 63 | alter=alter, 64 | properties=properties, 65 | spatialite=spatialite, 66 | spatialite_mod=spatialite_mod, 67 | spatial_index=spatial_index, 68 | ) 69 | except (TypeError, ValueError) as e: 70 | raise click.ClickException(str(e)) 71 | -------------------------------------------------------------------------------- /geojson_to_sqlite/utils.py: -------------------------------------------------------------------------------- 1 | from shapely.geometry import shape 2 | import sqlite_utils 3 | from sqlite_utils.utils import find_spatialite 4 | 5 | import inspect 6 | import itertools 7 | import json 8 | import os 9 | 10 | 11 | class SpatiaLiteError(Exception): 12 | pass 13 | 14 | 15 | def yield_records(features, pk, properties, spatialite): 16 | for feature in features: 17 | record = {} 18 | if "id" in feature: 19 | record["id"] = feature["id"] 20 | if properties: 21 | record[properties] = feature.get("properties") or {} 22 | else: 23 | record.update(feature.get("properties") or {}) 24 | geometry = feature.get("geometry") 25 | if spatialite and geometry: 26 | geometry = shape(geometry).wkt 27 | 28 | record["geometry"] = geometry 29 | yield record 30 | 31 | 32 | def import_features( 33 | db_path, 34 | table, 35 | features, 36 | pk=None, 37 | alter=False, 38 | properties="", 39 | spatialite=False, 40 | spatialite_mod=None, 41 | spatial_index=False, 42 | ): 43 | db = sqlite_utils.Database(db_path) 44 | features = iter(features) 45 | 46 | # grab a sample, for checking ids 47 | sample_geojson = list(itertools.islice(features, 100)) 48 | features = itertools.chain(sample_geojson, features) 49 | sample_records = list(yield_records(sample_geojson, pk, properties, spatialite)) 50 | 51 | if pk is None and has_ids(sample_records): 52 | pk = "id" 53 | 54 | conversions = {} 55 | if spatialite_mod or spatial_index: 56 | spatialite = True 57 | 58 | if spatialite: 59 | lib = spatialite_mod or find_spatialite() 60 | 61 | if not lib: 62 | raise SpatiaLiteError("Could not find SpatiaLite module") 63 | 64 | db.init_spatialite(lib) 65 | 66 | if table not in db.table_names(): 67 | # Create the table, using detected column types 68 | column_types = sqlite_utils.suggest_column_types(sample_records) 69 | column_types.pop("geometry") 70 | remove_tmp_column = False 71 | if not column_types: 72 | column_types["_tmp"] = str 73 | remove_tmp_column = True 74 | db[table].create(column_types, pk=pk) 75 | ensure_table_has_geometry(db, table) 76 | if remove_tmp_column: 77 | db[table].transform(drop={"_tmp"}) 78 | 79 | conversions = {"geometry": "GeomFromText(?, 4326)"} 80 | 81 | if pk: 82 | db[table].upsert_all( 83 | yield_records(features, pk, properties, spatialite), 84 | conversions=conversions, 85 | pk=pk, 86 | alter=alter, 87 | ) 88 | 89 | else: 90 | db[table].insert_all( 91 | yield_records(features, pk, properties, spatialite), conversions=conversions 92 | ) 93 | 94 | if spatial_index: 95 | # db.conn.execute("select CreateSpatialIndex(?, ?)", [table, "geometry"]) 96 | db[table].create_spatial_index("geometry") 97 | 98 | return db[table] 99 | 100 | 101 | def get_features(geojson_file, nl=False): 102 | """ 103 | Get a list of features from something resembling geojson. 104 | 105 | Note that if the `nl` option is True, this will return a generator 106 | that yields a single feature from each line in the source file. 107 | """ 108 | if nl: 109 | return (json.loads(line) for line in geojson_file if line.strip()) 110 | 111 | # if not nl, load the whole file 112 | geojson = json.load(geojson_file) 113 | if not isinstance(geojson, dict): 114 | raise TypeError("GeoJSON root must be an object") 115 | 116 | if geojson.get("type") not in ("Feature", "FeatureCollection"): 117 | raise ValueError("GeoJSON must be a Feature or a FeatureCollection") 118 | 119 | if geojson["type"] == "Feature": 120 | return [geojson] 121 | 122 | return geojson.get("features", []) 123 | 124 | 125 | def ensure_table_has_geometry(db, table): 126 | if "geometry" not in db[table].columns_dict: 127 | db[table].add_geometry_column("geometry", "GEOMETRY") 128 | 129 | 130 | def has_ids(features): 131 | return all(f.get("id") is not None for f in features) 132 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | import os 3 | 4 | VERSION = "1.1.1" 5 | 6 | 7 | def get_long_description(): 8 | with open( 9 | os.path.join(os.path.dirname(os.path.abspath(__file__)), "README.md"), 10 | encoding="utf8", 11 | ) as fp: 12 | return fp.read() 13 | 14 | 15 | setup( 16 | name="geojson-to-sqlite", 17 | description="CLI tool for converting GeoJSON to SQLite (optionally with SpatiaLite)", 18 | long_description=get_long_description(), 19 | long_description_content_type="text/markdown", 20 | author="Simon Willison", 21 | url="https://github.com/simonw/geojson-to-sqlite", 22 | license="Apache License, Version 2.0", 23 | version=VERSION, 24 | packages=["geojson_to_sqlite"], 25 | entry_points=""" 26 | [console_scripts] 27 | geojson-to-sqlite=geojson_to_sqlite.cli:cli 28 | """, 29 | install_requires=["sqlite-utils>=3.23", "shapely"], 30 | extras_require={"test": ["pytest", "dirty-equals"]}, 31 | ) 32 | -------------------------------------------------------------------------------- /tests/feature-collection-ids.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "FeatureCollection", 3 | "features": [ 4 | { 5 | "type": "Feature", 6 | "id": 3, 7 | "properties": { 8 | "slug": "uk", 9 | "description": "Rough area around the UK" 10 | }, 11 | "geometry": { 12 | "type": "Polygon", 13 | "coordinates": [ 14 | [ 15 | [ 16 | -8.0859375, 17 | 60.930432202923335 18 | ], 19 | [ 20 | -16.875, 21 | 50.28933925329178 22 | ], 23 | [ 24 | -5.9765625, 25 | 48.922499263758255 26 | ], 27 | [ 28 | 4.21875, 29 | 52.26815737376817 30 | ], 31 | [ 32 | 1.0546875, 33 | 60.06484046010452 34 | ], 35 | [ 36 | -8.0859375, 37 | 60.930432202923335 38 | ] 39 | ] 40 | ] 41 | } 42 | }, 43 | { 44 | "type": "Feature", 45 | "id": 8, 46 | "properties": { 47 | "slug": "usa", 48 | "description": "Very rough area around the USA", 49 | "continent": "North America" 50 | }, 51 | "geometry": { 52 | "type": "Polygon", 53 | "coordinates": [ 54 | [ 55 | [ 56 | -129.375, 57 | 47.754097979680026 58 | ], 59 | [ 60 | -119.53125, 61 | 33.43144133557529 62 | ], 63 | [ 64 | -96.6796875, 65 | 25.48295117535531 66 | ], 67 | [ 68 | -85.4296875, 69 | 24.206889622398023 70 | ], 71 | [ 72 | -77.34374999999999, 73 | 25.48295117535531 74 | ], 75 | [ 76 | -61.52343749999999, 77 | 44.33956524809713 78 | ], 79 | [ 80 | -84.375, 81 | 51.39920565355378 82 | ], 83 | [ 84 | -100.8984375, 85 | 50.064191736659104 86 | ], 87 | [ 88 | -115.31249999999999, 89 | 50.736455137010665 90 | ], 91 | [ 92 | -129.375, 93 | 47.754097979680026 94 | ] 95 | ] 96 | ] 97 | } 98 | } 99 | ] 100 | } -------------------------------------------------------------------------------- /tests/feature-collection.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "FeatureCollection", 3 | "features": [ 4 | { 5 | "type": "Feature", 6 | "properties": { 7 | "slug": "uk", 8 | "description": "Rough area around the UK" 9 | }, 10 | "geometry": { 11 | "type": "Polygon", 12 | "coordinates": [ 13 | [ 14 | [ 15 | -8.0859375, 16 | 60.930432202923335 17 | ], 18 | [ 19 | -16.875, 20 | 50.28933925329178 21 | ], 22 | [ 23 | -5.9765625, 24 | 48.922499263758255 25 | ], 26 | [ 27 | 4.21875, 28 | 52.26815737376817 29 | ], 30 | [ 31 | 1.0546875, 32 | 60.06484046010452 33 | ], 34 | [ 35 | -8.0859375, 36 | 60.930432202923335 37 | ] 38 | ] 39 | ] 40 | } 41 | }, 42 | { 43 | "type": "Feature", 44 | "properties": { 45 | "slug": "usa", 46 | "description": "Very rough area around the USA", 47 | "continent": "North America" 48 | }, 49 | "geometry": { 50 | "type": "Polygon", 51 | "coordinates": [ 52 | [ 53 | [ 54 | -129.375, 55 | 47.754097979680026 56 | ], 57 | [ 58 | -119.53125, 59 | 33.43144133557529 60 | ], 61 | [ 62 | -96.6796875, 63 | 25.48295117535531 64 | ], 65 | [ 66 | -85.4296875, 67 | 24.206889622398023 68 | ], 69 | [ 70 | -77.34374999999999, 71 | 25.48295117535531 72 | ], 73 | [ 74 | -61.52343749999999, 75 | 44.33956524809713 76 | ], 77 | [ 78 | -84.375, 79 | 51.39920565355378 80 | ], 81 | [ 82 | -100.8984375, 83 | 50.064191736659104 84 | ], 85 | [ 86 | -115.31249999999999, 87 | 50.736455137010665 88 | ], 89 | [ 90 | -129.375, 91 | 47.754097979680026 92 | ] 93 | ] 94 | ] 95 | } 96 | } 97 | ] 98 | } -------------------------------------------------------------------------------- /tests/feature.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "Feature", 3 | "properties": { 4 | "slug": "uk", 5 | "description": "Rough area around the UK" 6 | }, 7 | "geometry": { 8 | "type": "Polygon", 9 | "coordinates": [ 10 | [ 11 | [ 12 | -8.0859375, 13 | 60.930432202923335 14 | ], 15 | [ 16 | -16.875, 17 | 50.28933925329178 18 | ], 19 | [ 20 | -5.9765625, 21 | 48.922499263758255 22 | ], 23 | [ 24 | 4.21875, 25 | 52.26815737376817 26 | ], 27 | [ 28 | 1.0546875, 29 | 60.06484046010452 30 | ], 31 | [ 32 | -8.0859375, 33 | 60.930432202923335 34 | ] 35 | ] 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/invalid.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "Property", 3 | "properties": { 4 | "slug": "uk", 5 | "description": "Rough area around the UK" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/quakes.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "features": [ 3 | { 4 | "geometry": { 5 | "coordinates": [-172.0991, -18.8187, 10], 6 | "type": "Point" 7 | }, 8 | "id": "0", 9 | "properties": { 10 | "alert": null, 11 | "cdi": null, 12 | "code": "70006tcn", 13 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006tcn&format=geojson", 14 | "dmin": 2.071, 15 | "felt": null, 16 | "gap": 74, 17 | "id": "us70006tcn", 18 | "ids": ",us70006tcn,", 19 | "mag": 5, 20 | "magType": "mb", 21 | "mmi": null, 22 | "net": "us", 23 | "nst": null, 24 | "place": "199km E of Neiafu, Tonga", 25 | "rms": 1.1, 26 | "sig": 385, 27 | "sources": ",us,", 28 | "status": "reviewed", 29 | "time": 1577920374126, 30 | "title": "M 5.0 - 199km E of Neiafu, Tonga", 31 | "tsunami": 0, 32 | "type": "earthquake", 33 | "types": ",geoserve,origin,phase-data,", 34 | "tz": -660, 35 | "updated": 1579312938040, 36 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006tcn" 37 | }, 38 | "type": "Feature" 39 | }, 40 | { 41 | "id": "1", 42 | "properties": { 43 | "alert": null, 44 | "cdi": null, 45 | "code": "700070tk", 46 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070tk&format=geojson", 47 | "dmin": 2.108, 48 | "felt": null, 49 | "gap": 103, 50 | "id": "us700070tk", 51 | "ids": ",us700070tk,", 52 | "mag": 4.4, 53 | "magType": "mb", 54 | "mmi": null, 55 | "net": "us", 56 | "nst": null, 57 | "place": "202km E of Neiafu, Tonga", 58 | "rms": 0.66, 59 | "sig": 298, 60 | "sources": ",us,", 61 | "status": "reviewed", 62 | "time": 1577918987058, 63 | "title": "M 4.4 - 202km E of Neiafu, Tonga", 64 | "tsunami": 0, 65 | "type": "earthquake", 66 | "types": ",geoserve,origin,phase-data,", 67 | "tz": -660, 68 | "updated": 1579310941040, 69 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070tk" 70 | }, 71 | "type": "Feature" 72 | }, 73 | { 74 | "geometry": null, 75 | "id": "2", 76 | "properties": { 77 | "alert": null, 78 | "cdi": null, 79 | "code": "70006tb0", 80 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006tb0&format=geojson", 81 | "dmin": 1.672, 82 | "felt": null, 83 | "gap": 50, 84 | "id": "us70006tb0", 85 | "ids": ",us70006tb0,", 86 | "mag": 4.3, 87 | "magType": "mb", 88 | "mmi": null, 89 | "net": "us", 90 | "nst": null, 91 | "place": "73km WSW of Abra Pampa, Argentina", 92 | "rms": 0.9, 93 | "sig": 284, 94 | "sources": ",us,", 95 | "status": "reviewed", 96 | "time": 1577915983016, 97 | "title": "M 4.3 - 73km WSW of Abra Pampa, Argentina", 98 | "tsunami": 0, 99 | "type": "earthquake", 100 | "types": ",geoserve,origin,phase-data,", 101 | "tz": -180, 102 | "updated": 1579309960040, 103 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006tb0" 104 | }, 105 | "type": "Feature" 106 | }, 107 | { 108 | "geometry": { 109 | "coordinates": [-178.4354, -33.3675, 10], 110 | "type": "Point" 111 | }, 112 | "id": "3", 113 | "properties": { 114 | "alert": null, 115 | "cdi": null, 116 | "code": "700070tm", 117 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070tm&format=geojson", 118 | "dmin": 6.42, 119 | "felt": null, 120 | "gap": 195, 121 | "id": "us700070tm", 122 | "ids": ",us700070tm,", 123 | "mag": 4.4, 124 | "magType": "mb", 125 | "mmi": null, 126 | "net": "us", 127 | "nst": null, 128 | "place": "218km SSE of L'Esperance Rock, New Zealand", 129 | "rms": 0.92, 130 | "sig": 298, 131 | "sources": ",us,", 132 | "status": "reviewed", 133 | "time": 1577914724149, 134 | "title": "M 4.4 - 218km SSE of L'Esperance Rock, New Zealand", 135 | "tsunami": 0, 136 | "type": "earthquake", 137 | "types": ",geoserve,origin,phase-data,", 138 | "tz": -720, 139 | "updated": 1579309310040, 140 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070tm" 141 | }, 142 | "type": "Feature" 143 | }, 144 | { 145 | "geometry": { 146 | "coordinates": [-178.3726, -33.0633, 10], 147 | "type": "Point" 148 | }, 149 | "id": "4", 150 | "properties": { 151 | "alert": null, 152 | "cdi": null, 153 | "code": "70006tb2", 154 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006tb2&format=geojson", 155 | "dmin": 3.825, 156 | "felt": null, 157 | "gap": 130, 158 | "id": "us70006tb2", 159 | "ids": ",us70006tb2,", 160 | "mag": 4.8, 161 | "magType": "mb", 162 | "mmi": null, 163 | "net": "us", 164 | "nst": null, 165 | "place": "187km SSE of L'Esperance Rock, New Zealand", 166 | "rms": 0.95, 167 | "sig": 354, 168 | "sources": ",us,", 169 | "status": "reviewed", 170 | "time": 1577914374402, 171 | "title": "M 4.8 - 187km SSE of L'Esperance Rock, New Zealand", 172 | "tsunami": 0, 173 | "type": "earthquake", 174 | "types": ",geoserve,origin,phase-data,", 175 | "tz": -720, 176 | "updated": 1579242222040, 177 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006tb2" 178 | }, 179 | "type": "Feature" 180 | }, 181 | { 182 | "geometry": { 183 | "coordinates": [-176.8485, -33.3138, 10], 184 | "type": "Point" 185 | }, 186 | "id": "5", 187 | "properties": { 188 | "alert": null, 189 | "cdi": null, 190 | "code": "70006taw", 191 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006taw&format=geojson", 192 | "dmin": 4.16, 193 | "felt": null, 194 | "gap": 146, 195 | "id": "us70006taw", 196 | "ids": ",us70006taw,", 197 | "mag": 4.8, 198 | "magType": "mb", 199 | "mmi": null, 200 | "net": "us", 201 | "nst": null, 202 | "place": "284km SE of L'Esperance Rock, New Zealand", 203 | "rms": 0.43, 204 | "sig": 354, 205 | "sources": ",us,", 206 | "status": "reviewed", 207 | "time": 1577911091204, 208 | "title": "M 4.8 - 284km SE of L'Esperance Rock, New Zealand", 209 | "tsunami": 0, 210 | "type": "earthquake", 211 | "types": ",geoserve,origin,phase-data,", 212 | "tz": -720, 213 | "updated": 1579653290040, 214 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006taw" 215 | }, 216 | "type": "Feature" 217 | }, 218 | { 219 | "geometry": { 220 | "coordinates": [-178.2074, -33.4458, 10], 221 | "type": "Point" 222 | }, 223 | "id": "6", 224 | "properties": { 225 | "alert": null, 226 | "cdi": null, 227 | "code": "70006ur8", 228 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006ur8&format=geojson", 229 | "dmin": 4.995, 230 | "felt": null, 231 | "gap": 191, 232 | "id": "us70006ur8", 233 | "ids": ",us70006ur8,", 234 | "mag": 4.5, 235 | "magType": "mb", 236 | "mmi": null, 237 | "net": "us", 238 | "nst": null, 239 | "place": "232km SSE of L'Esperance Rock, New Zealand", 240 | "rms": 0.55, 241 | "sig": 312, 242 | "sources": ",us,", 243 | "status": "reviewed", 244 | "time": 1577911078216, 245 | "title": "M 4.5 - 232km SSE of L'Esperance Rock, New Zealand", 246 | "tsunami": 0, 247 | "type": "earthquake", 248 | "types": ",geoserve,origin,phase-data,", 249 | "tz": -720, 250 | "updated": 1579653419040, 251 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006ur8" 252 | }, 253 | "type": "Feature" 254 | }, 255 | { 256 | "geometry": { 257 | "coordinates": [-172.206, -18.6577, 10], 258 | "type": "Point" 259 | }, 260 | "id": "7", 261 | "properties": { 262 | "alert": null, 263 | "cdi": null, 264 | "code": "70006t9g", 265 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t9g&format=geojson", 266 | "dmin": 2.197, 267 | "felt": null, 268 | "gap": 48, 269 | "id": "us70006t9g", 270 | "ids": ",us70006t9g,", 271 | "mag": 4.9, 272 | "magType": "mb", 273 | "mmi": null, 274 | "net": "us", 275 | "nst": null, 276 | "place": "187km E of Neiafu, Tonga", 277 | "rms": 0.83, 278 | "sig": 369, 279 | "sources": ",us,", 280 | "status": "reviewed", 281 | "time": 1577910734577, 282 | "title": "M 4.9 - 187km E of Neiafu, Tonga", 283 | "tsunami": 0, 284 | "type": "earthquake", 285 | "types": ",geoserve,origin,phase-data,", 286 | "tz": -660, 287 | "updated": 1579653068040, 288 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t9g" 289 | }, 290 | "type": "Feature" 291 | }, 292 | { 293 | "geometry": { 294 | "coordinates": [-178.2297, -33.212, 10], 295 | "type": "Point" 296 | }, 297 | "id": "8", 298 | "properties": { 299 | "alert": null, 300 | "cdi": null, 301 | "code": "70006t8t", 302 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t8t&format=geojson", 303 | "dmin": 3.963, 304 | "felt": null, 305 | "gap": 67, 306 | "id": "us70006t8t", 307 | "ids": ",us70006t8t,", 308 | "mag": 5, 309 | "magType": "mb", 310 | "mmi": null, 311 | "net": "us", 312 | "nst": null, 313 | "place": "207km SSE of L'Esperance Rock, New Zealand", 314 | "rms": 1.35, 315 | "sig": 385, 316 | "sources": ",us,", 317 | "status": "reviewed", 318 | "time": 1577908887231, 319 | "title": "M 5.0 - 207km SSE of L'Esperance Rock, New Zealand", 320 | "tsunami": 0, 321 | "type": "earthquake", 322 | "types": ",geoserve,origin,phase-data,", 323 | "tz": -720, 324 | "updated": 1579652896040, 325 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t8t" 326 | }, 327 | "type": "Feature" 328 | }, 329 | { 330 | "geometry": { 331 | "coordinates": [170.3813, 53.142, 10], 332 | "type": "Point" 333 | }, 334 | "id": "9", 335 | "properties": { 336 | "alert": null, 337 | "cdi": null, 338 | "code": "700070th", 339 | "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070th&format=geojson", 340 | "dmin": 2.287, 341 | "felt": null, 342 | "gap": 147, 343 | "id": "us700070th", 344 | "ids": ",us700070th,", 345 | "mag": 4.1, 346 | "magType": "mb", 347 | "mmi": null, 348 | "net": "us", 349 | "nst": null, 350 | "place": "191km W of Attu Station, Alaska", 351 | "rms": 0.91, 352 | "sig": 259, 353 | "sources": ",us,", 354 | "status": "reviewed", 355 | "time": 1577908618275, 356 | "title": "M 4.1 - 191km W of Attu Station, Alaska", 357 | "tsunami": 0, 358 | "type": "earthquake", 359 | "types": ",geoserve,origin,phase-data,", 360 | "tz": 660, 361 | "updated": 1580942302040, 362 | "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070th" 363 | }, 364 | "type": "Feature" 365 | } 366 | ], 367 | "type": "FeatureCollection" 368 | } 369 | -------------------------------------------------------------------------------- /tests/quakes.ndjson: -------------------------------------------------------------------------------- 1 | {"geometry": {"coordinates": [-172.0991, -18.8187, 10.0], "type": "Point"}, "id": "0", "properties": {"alert": null, "cdi": null, "code": "70006tcn", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006tcn&format=geojson", "dmin": 2.071, "felt": null, "gap": 74, "id": "us70006tcn", "ids": ",us70006tcn,", "mag": 5.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "199km E of Neiafu, Tonga", "rms": 1.1, "sig": 385, "sources": ",us,", "status": "reviewed", "time": 1577920374126, "title": "M 5.0 - 199km E of Neiafu, Tonga", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -660, "updated": 1579312938040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006tcn"}, "type": "Feature"} 2 | {"geometry": {"coordinates": [-172.0708, -18.5072, 10.0], "type": "Point"}, "id": "1", "properties": {"alert": null, "cdi": null, "code": "700070tk", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070tk&format=geojson", "dmin": 2.108, "felt": null, "gap": 103, "id": "us700070tk", "ids": ",us700070tk,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "202km E of Neiafu, Tonga", "rms": 0.66, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577918987058, "title": "M 4.4 - 202km E of Neiafu, Tonga", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -660, "updated": 1579310941040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070tk"}, "type": "Feature"} 3 | {"geometry": {"coordinates": [-66.3645, -22.9587, 245.75], "type": "Point"}, "id": "2", "properties": {"alert": null, "cdi": null, "code": "70006tb0", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006tb0&format=geojson", "dmin": 1.672, "felt": null, "gap": 50, "id": "us70006tb0", "ids": ",us70006tb0,", "mag": 4.3, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "73km WSW of Abra Pampa, Argentina", "rms": 0.9, "sig": 284, "sources": ",us,", "status": "reviewed", "time": 1577915983016, "title": "M 4.3 - 73km WSW of Abra Pampa, Argentina", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -180, "updated": 1579309960040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006tb0"}, "type": "Feature"} 4 | {"geometry": {"coordinates": [-178.4354, -33.3675, 10.0], "type": "Point"}, "id": "3", "properties": {"alert": null, "cdi": null, "code": "700070tm", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070tm&format=geojson", "dmin": 6.42, "felt": null, "gap": 195, "id": "us700070tm", "ids": ",us700070tm,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "218km SSE of L'Esperance Rock, New Zealand", "rms": 0.92, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577914724149, "title": "M 4.4 - 218km SSE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1579309310040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070tm"}, "type": "Feature"} 5 | {"geometry": {"coordinates": [-178.3726, -33.0633, 10.0], "type": "Point"}, "id": "4", "properties": {"alert": null, "cdi": null, "code": "70006tb2", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006tb2&format=geojson", "dmin": 3.825, "felt": null, "gap": 130, "id": "us70006tb2", "ids": ",us70006tb2,", "mag": 4.8, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "187km SSE of L'Esperance Rock, New Zealand", "rms": 0.95, "sig": 354, "sources": ",us,", "status": "reviewed", "time": 1577914374402, "title": "M 4.8 - 187km SSE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1579242222040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006tb2"}, "type": "Feature"} 6 | {"geometry": {"coordinates": [-176.8485, -33.3138, 10.0], "type": "Point"}, "id": "5", "properties": {"alert": null, "cdi": null, "code": "70006taw", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006taw&format=geojson", "dmin": 4.16, "felt": null, "gap": 146, "id": "us70006taw", "ids": ",us70006taw,", "mag": 4.8, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "284km SE of L'Esperance Rock, New Zealand", "rms": 0.43, "sig": 354, "sources": ",us,", "status": "reviewed", "time": 1577911091204, "title": "M 4.8 - 284km SE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1579653290040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006taw"}, "type": "Feature"} 7 | {"geometry": {"coordinates": [-178.2074, -33.4458, 10.0], "type": "Point"}, "id": "6", "properties": {"alert": null, "cdi": null, "code": "70006ur8", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006ur8&format=geojson", "dmin": 4.995, "felt": null, "gap": 191, "id": "us70006ur8", "ids": ",us70006ur8,", "mag": 4.5, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "232km SSE of L'Esperance Rock, New Zealand", "rms": 0.55, "sig": 312, "sources": ",us,", "status": "reviewed", "time": 1577911078216, "title": "M 4.5 - 232km SSE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1579653419040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006ur8"}, "type": "Feature"} 8 | {"geometry": {"coordinates": [-172.206, -18.6577, 10.0], "type": "Point"}, "id": "7", "properties": {"alert": null, "cdi": null, "code": "70006t9g", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t9g&format=geojson", "dmin": 2.197, "felt": null, "gap": 48, "id": "us70006t9g", "ids": ",us70006t9g,", "mag": 4.9, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "187km E of Neiafu, Tonga", "rms": 0.83, "sig": 369, "sources": ",us,", "status": "reviewed", "time": 1577910734577, "title": "M 4.9 - 187km E of Neiafu, Tonga", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -660, "updated": 1579653068040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t9g"}, "type": "Feature"} 9 | {"geometry": {"coordinates": [-178.2297, -33.212, 10.0], "type": "Point"}, "id": "8", "properties": {"alert": null, "cdi": null, "code": "70006t8t", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t8t&format=geojson", "dmin": 3.963, "felt": null, "gap": 67, "id": "us70006t8t", "ids": ",us70006t8t,", "mag": 5.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "207km SSE of L'Esperance Rock, New Zealand", "rms": 1.35, "sig": 385, "sources": ",us,", "status": "reviewed", "time": 1577908887231, "title": "M 5.0 - 207km SSE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1579652896040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t8t"}, "type": "Feature"} 10 | {"geometry": {"coordinates": [170.3813, 53.142, 10.0], "type": "Point"}, "id": "9", "properties": {"alert": null, "cdi": null, "code": "700070th", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070th&format=geojson", "dmin": 2.287, "felt": null, "gap": 147, "id": "us700070th", "ids": ",us700070th,", "mag": 4.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "191km W of Attu Station, Alaska", "rms": 0.91, "sig": 259, "sources": ",us,", "status": "reviewed", "time": 1577908618275, "title": "M 4.1 - 191km W of Attu Station, Alaska", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 660, "updated": 1580942302040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070th"}, "type": "Feature"} 11 | {"geometry": {"coordinates": [126.5968, 1.9266, 35.0], "type": "Point"}, "id": "10", "properties": {"alert": null, "cdi": null, "code": "700070tg", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070tg&format=geojson", "dmin": 1.381, "felt": null, "gap": 124, "id": "us700070tg", "ids": ",us700070tg,", "mag": 4.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "152km NW of Kota Ternate, Indonesia", "rms": 1.0, "sig": 259, "sources": ",us,", "status": "reviewed", "time": 1577908246097, "title": "M 4.1 - 152km NW of Kota Ternate, Indonesia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 480, "updated": 1580317119040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070tg"}, "type": "Feature"} 12 | {"geometry": {"coordinates": [-68.389, -21.079, 139.26], "type": "Point"}, "id": "11", "properties": {"alert": null, "cdi": null, "code": "70006t8c", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t8c&format=geojson", "dmin": 1.026, "felt": null, "gap": 43, "id": "us70006t8c", "ids": ",us70006t8c,", "mag": 4.3, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "163km NNE of Calama, Chile", "rms": 1.1, "sig": 284, "sources": ",us,", "status": "reviewed", "time": 1577906942276, "title": "M 4.3 - 163km NNE of Calama, Chile", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -240, "updated": 1579652189040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t8c"}, "type": "Feature"} 13 | {"geometry": {"coordinates": [-178.3288, -32.9741, 18.15], "type": "Point"}, "id": "12", "properties": {"alert": "green", "cdi": null, "code": "70006t8d", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t8d&format=geojson", "dmin": 3.733, "felt": null, "gap": 56, "id": "us70006t8d", "ids": ",us70006t8d,", "mag": 5.4, "magType": "mww", "mmi": 0, "net": "us", "nst": null, "place": "179km SSE of L'Esperance Rock, New Zealand", "rms": 1.03, "sig": 449, "sources": ",us,", "status": "reviewed", "time": 1577906931925, "title": "M 5.4 - 179km SSE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,losspager,moment-tensor,origin,phase-data,shakemap,", "tz": -720, "updated": 1579652533221, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t8d"}, "type": "Feature"} 14 | {"geometry": {"coordinates": [150.0839, 44.0685, 10.0], "type": "Point"}, "id": "13", "properties": {"alert": null, "cdi": null, "code": "70006t83", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t83&format=geojson", "dmin": 5.395, "felt": null, "gap": 136, "id": "us70006t83", "ids": ",us70006t83,", "mag": 4.7, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "217km SE of Kuril'sk, Russia", "rms": 0.86, "sig": 340, "sources": ",us,", "status": "reviewed", "time": 1577904438754, "title": "M 4.7 - 217km SE of Kuril'sk, Russia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 600, "updated": 1579652035040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t83"}, "type": "Feature"} 15 | {"geometry": {"coordinates": [170.3104, 53.2551, 10.0], "type": "Point"}, "id": "14", "properties": {"alert": null, "cdi": null, "code": "70006t7e", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t7e&format=geojson", "dmin": 2.349, "felt": null, "gap": 78, "id": "us70006t7e", "ids": ",at00q3fx53,ak0201yc6h1,us70006t7e,", "mag": 5.2, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "198km WNW of Attu Station, Alaska", "rms": 0.64, "sig": 416, "sources": ",at,ak,us,", "status": "reviewed", "time": 1577902211687, "title": "M 5.2 - 198km WNW of Attu Station, Alaska", "tsunami": 1, "type": "earthquake", "types": ",geoserve,impact-link,origin,phase-data,", "tz": 660, "updated": 1579651921040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t7e"}, "type": "Feature"} 16 | {"geometry": {"coordinates": [25.0675, 34.2543, 10.0], "type": "Point"}, "id": "15", "properties": {"alert": null, "cdi": null, "code": "700070te", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070te&format=geojson", "dmin": 1.041, "felt": null, "gap": 64, "id": "us700070te", "ids": ",us700070te,", "mag": 4.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "83km S of Pirgos, Greece", "rms": 0.93, "sig": 259, "sources": ",us,", "status": "reviewed", "time": 1577900239599, "title": "M 4.1 - 83km S of Pirgos, Greece", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 120, "updated": 1580310890040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070te"}, "type": "Feature"} 17 | {"geometry": {"coordinates": [-71.5582, -30.2857, 40.88], "type": "Point"}, "id": "16", "properties": {"alert": null, "cdi": 4.1, "code": "70006t6h", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t6h&format=geojson", "dmin": 0.392, "felt": 10, "gap": 117, "id": "us70006t6h", "ids": ",us70006t6h,", "mag": 5.0, "magType": "mwr", "mmi": null, "net": "us", "nst": null, "place": "42km SSW of Coquimbo, Chile", "rms": 0.72, "sig": 389, "sources": ",us,", "status": "reviewed", "time": 1577897492285, "title": "M 5.0 - 42km SSW of Coquimbo, Chile", "tsunami": 0, "type": "earthquake", "types": ",dyfi,geoserve,moment-tensor,origin,phase-data,", "tz": -240, "updated": 1579223792859, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t6h"}, "type": "Feature"} 18 | {"geometry": {"coordinates": [9.5001, -53.0907, 10.0], "type": "Point"}, "id": "17", "properties": {"alert": null, "cdi": null, "code": "70006t6a", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t6a&format=geojson", "dmin": 19.424, "felt": null, "gap": 105, "id": "us70006t6a", "ids": ",us70006t6a,", "mag": 5.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "Southwest of Africa", "rms": 0.83, "sig": 385, "sources": ",us,", "status": "reviewed", "time": 1577896499178, "title": "M 5.0 - Southwest of Africa", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 60, "updated": 1579222638040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t6a"}, "type": "Feature"} 19 | {"geometry": {"coordinates": [-178.3732, -32.8014, 10.0], "type": "Point"}, "id": "18", "properties": {"alert": null, "cdi": null, "code": "700070td", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070td&format=geojson", "dmin": 3.565, "felt": null, "gap": 174, "id": "us700070td", "ids": ",us700070td,", "mag": 4.6, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "159km SSE of L'Esperance Rock, New Zealand", "rms": 1.02, "sig": 326, "sources": ",us,", "status": "reviewed", "time": 1577895770404, "title": "M 4.6 - 159km SSE of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1579222068040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070td"}, "type": "Feature"} 20 | {"geometry": {"coordinates": [142.137, 24.5787, 10.0], "type": "Point"}, "id": "19", "properties": {"alert": null, "cdi": null, "code": "700070tc", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070tc&format=geojson", "dmin": 2.506, "felt": null, "gap": 151, "id": "us700070tc", "ids": ",us700070tc,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "84km ESE of Iwo Jima, Japan", "rms": 0.5, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577894480910, "title": "M 4.4 - 84km ESE of Iwo Jima, Japan", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 540, "updated": 1579221709040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070tc"}, "type": "Feature"} 21 | {"geometry": {"coordinates": [-178.1302, 51.2023, 37.99], "type": "Point"}, "id": "20", "properties": {"alert": null, "cdi": null, "code": "70006t5u", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t5u&format=geojson", "dmin": 0.554, "felt": null, "gap": 61, "id": "us70006t5u", "ids": ",at00q3folw,ak0201wi1ij,us70006t5u,", "mag": 4.8, "magType": "mww", "mmi": null, "net": "us", "nst": null, "place": "68km E of Amatignak Island, Alaska", "rms": 0.97, "sig": 354, "sources": ",at,ak,us,", "status": "reviewed", "time": 1577891157267, "title": "M 4.8 - 68km E of Amatignak Island, Alaska", "tsunami": 1, "type": "earthquake", "types": ",geoserve,impact-link,moment-tensor,origin,phase-data,", "tz": -720, "updated": 1579216713040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t5u"}, "type": "Feature"} 22 | {"geometry": {"coordinates": [126.8206, 2.1446, 39.39], "type": "Point"}, "id": "21", "properties": {"alert": null, "cdi": null, "code": "70006t5p", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t5p&format=geojson", "dmin": 1.468, "felt": null, "gap": 79, "id": "us70006t5p", "ids": ",us70006t5p,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "140km WNW of Tobelo, Indonesia", "rms": 0.69, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577889383685, "title": "M 4.4 - 140km WNW of Tobelo, Indonesia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 480, "updated": 1579427878040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t5p"}, "type": "Feature"} 23 | {"geometry": {"coordinates": [45.5741, -13.0674, 10.0], "type": "Point"}, "id": "22", "properties": {"alert": null, "cdi": null, "code": "70006t54", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t54&format=geojson", "dmin": 6.126, "felt": null, "gap": 96, "id": "us70006t54", "ids": ",us70006t54,", "mag": 4.6, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "43km SE of Pamandzi, Mayotte", "rms": 1.07, "sig": 326, "sources": ",us,", "status": "reviewed", "time": 1577884728035, "title": "M 4.6 - 43km SE of Pamandzi, Mayotte", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 180, "updated": 1579425128040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t54"}, "type": "Feature"} 24 | {"geometry": {"coordinates": [51.6449, 30.6259, 10.0], "type": "Point"}, "id": "23", "properties": {"alert": null, "cdi": null, "code": "70006t50", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t50&format=geojson", "dmin": 6.699, "felt": null, "gap": 76, "id": "us70006t50", "ids": ",us70006t50,", "mag": 4.2, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "7km SE of Yasuj, Iran", "rms": 0.58, "sig": 271, "sources": ",us,", "status": "reviewed", "time": 1577883543756, "title": "M 4.2 - 7km SE of Yasuj, Iran", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 210, "updated": 1580382081040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t50"}, "type": "Feature"} 25 | {"geometry": {"coordinates": [121.5572, 0.1936, 141.27], "type": "Point"}, "id": "24", "properties": {"alert": null, "cdi": null, "code": "700070t6", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t6&format=geojson", "dmin": 1.196, "felt": null, "gap": 140, "id": "us700070t6", "ids": ",us700070t6,", "mag": 4.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "38km S of Lemito, Indonesia", "rms": 0.24, "sig": 259, "sources": ",us,", "status": "reviewed", "time": 1577881714164, "title": "M 4.1 - 38km S of Lemito, Indonesia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 480, "updated": 1580381139040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t6"}, "type": "Feature"} 26 | {"geometry": {"coordinates": [-177.5342, 49.6097, 10.0], "type": "Point"}, "id": "25", "properties": {"alert": null, "cdi": null, "code": "70006t71", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t71&format=geojson", "dmin": 2.11, "felt": null, "gap": 221, "id": "us70006t71", "ids": ",ak0201tnsph,us70006t71,", "mag": 4.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "215km SSE of Amatignak Island, Alaska", "rms": 0.95, "sig": 246, "sources": ",ak,us,", "status": "reviewed", "time": 1577874531333, "title": "M 4.0 - 215km SSE of Amatignak Island, Alaska", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1580377630040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t71"}, "type": "Feature"} 27 | {"geometry": {"coordinates": [-172.066, -16.9737, 38.61], "type": "Point"}, "id": "26", "properties": {"alert": null, "cdi": null, "code": "700070t5", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t5&format=geojson", "dmin": 2.917, "felt": null, "gap": 204, "id": "us700070t5", "ids": ",us700070t5,", "mag": 4.7, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "212km ESE of Hihifo, Tonga", "rms": 0.76, "sig": 340, "sources": ",us,", "status": "reviewed", "time": 1577867845488, "title": "M 4.7 - 212km ESE of Hihifo, Tonga", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -660, "updated": 1581054737040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t5"}, "type": "Feature"} 28 | {"geometry": {"coordinates": [-172.4897, -17.427, 10.0], "type": "Point"}, "id": "27", "properties": {"alert": "green", "cdi": null, "code": "70006t3c", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t3c&format=geojson", "dmin": 2.936, "felt": null, "gap": 40, "id": "us70006t3c", "ids": ",us70006t3c,", "mag": 5.5, "magType": "mww", "mmi": 0, "net": "us", "nst": null, "place": "208km NE of Neiafu, Tonga", "rms": 1.28, "sig": 465, "sources": ",us,", "status": "reviewed", "time": 1577867484041, "title": "M 5.5 - 208km NE of Neiafu, Tonga", "tsunami": 0, "type": "earthquake", "types": ",geoserve,losspager,origin,phase-data,shakemap,", "tz": -660, "updated": 1580735058310, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t3c"}, "type": "Feature"} 29 | {"geometry": {"coordinates": [-70.7215, -22.7979, 10.0], "type": "Point"}, "id": "28", "properties": {"alert": null, "cdi": null, "code": "70006t37", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t37&format=geojson", "dmin": 0.482, "felt": null, "gap": 185, "id": "us70006t37", "ids": ",us70006t37,", "mag": 4.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "94km SW of Tocopilla, Chile", "rms": 0.49, "sig": 246, "sources": ",us,", "status": "reviewed", "time": 1577865838562, "title": "M 4.0 - 94km SW of Tocopilla, Chile", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -300, "updated": 1580998937040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t37"}, "type": "Feature"} 30 | {"geometry": {"coordinates": [146.3212, 44.6423, 167.96], "type": "Point"}, "id": "29", "properties": {"alert": null, "cdi": null, "code": "700070t8", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t8&format=geojson", "dmin": 2.723, "felt": null, "gap": 136, "id": "us700070t8", "ids": ",us700070t8,", "mag": 4.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "73km NNE of Otrada, Russia", "rms": 0.93, "sig": 246, "sources": ",us,", "status": "reviewed", "time": 1577864861464, "title": "M 4.0 - 73km NNE of Otrada, Russia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 660, "updated": 1580998605040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t8"}, "type": "Feature"} 31 | {"geometry": {"coordinates": [143.8215, 36.1566, 10.0], "type": "Point"}, "id": "30", "properties": {"alert": null, "cdi": null, "code": "700070t7", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t7&format=geojson", "dmin": 4.485, "felt": null, "gap": 120, "id": "us700070t7", "ids": ",us700070t7,", "mag": 4.3, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "273km E of Hasaki, Japan", "rms": 0.91, "sig": 284, "sources": ",us,", "status": "reviewed", "time": 1577862045869, "title": "M 4.3 - 273km E of Hasaki, Japan", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 600, "updated": 1580997648040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t7"}, "type": "Feature"} 32 | {"geometry": {"coordinates": [129.7667, -6.7009, 155.06], "type": "Point"}, "id": "31", "properties": {"alert": null, "cdi": null, "code": "70006t2q", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t2q&format=geojson", "dmin": 1.982, "felt": null, "gap": 97, "id": "us70006t2q", "ids": ",us70006t2q,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "219km NW of Saumlaki, Indonesia", "rms": 0.56, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577858474184, "title": "M 4.4 - 219km NW of Saumlaki, Indonesia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 540, "updated": 1580458070040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t2q"}, "type": "Feature"} 33 | {"geometry": {"coordinates": [-178.8663, -33.5696, 10.0], "type": "Point"}, "id": "32", "properties": {"alert": null, "cdi": null, "code": "700070t2", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t2&format=geojson", "dmin": 4.385, "felt": null, "gap": 161, "id": "us700070t2", "ids": ",us700070t2,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "236km S of L'Esperance Rock, New Zealand", "rms": 0.73, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577857452126, "title": "M 4.4 - 236km S of L'Esperance Rock, New Zealand", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1581053663040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t2"}, "type": "Feature"} 34 | {"geometry": {"coordinates": [126.7211, 2.0047, 50.97], "type": "Point"}, "id": "33", "properties": {"alert": null, "cdi": null, "code": "700070t0", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t0&format=geojson", "dmin": 1.384, "felt": null, "gap": 74, "id": "us700070t0", "ids": ",us700070t0,", "mag": 4.4, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "146km WNW of Tobelo, Indonesia", "rms": 0.6, "sig": 298, "sources": ",us,", "status": "reviewed", "time": 1577853487864, "title": "M 4.4 - 146km WNW of Tobelo, Indonesia", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 480, "updated": 1580777682040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t0"}, "type": "Feature"} 35 | {"geometry": {"coordinates": [-177.3907, -17.1905, 392.48], "type": "Point"}, "id": "34", "properties": {"alert": null, "cdi": null, "code": "700070t3", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070t3&format=geojson", "dmin": 9.231, "felt": null, "gap": 133, "id": "us700070t3", "ids": ",us700070t3,", "mag": 4.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "Fiji region", "rms": 0.85, "sig": 246, "sources": ",us,", "status": "reviewed", "time": 1577852504097, "title": "M 4.0 - Fiji region", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": -720, "updated": 1580860164040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070t3"}, "type": "Feature"} 36 | {"geometry": {"coordinates": [75.3789, 38.2179, 103.77], "type": "Point"}, "id": "35", "properties": {"alert": null, "cdi": null, "code": "70006t2b", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t2b&format=geojson", "dmin": 3.021, "felt": null, "gap": 72, "id": "us70006t2b", "ids": ",us70006t2b,", "mag": 4.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "123km E of Murghob, Tajikistan", "rms": 0.79, "sig": 259, "sources": ",us,", "status": "reviewed", "time": 1577851155645, "title": "M 4.1 - 123km E of Murghob, Tajikistan", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 480, "updated": 1580449199040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t2b"}, "type": "Feature"} 37 | {"geometry": {"coordinates": [159.1614, 52.6367, 58.46], "type": "Point"}, "id": "36", "properties": {"alert": null, "cdi": 3.4, "code": "70006t2a", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t2a&format=geojson", "dmin": 0.496, "felt": 4, "gap": 94, "id": "us70006t2a", "ids": ",us70006t2a,", "mag": 5.0, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "57km SE of Petropavlovsk-Kamchatskiy, Russia", "rms": 0.66, "sig": 386, "sources": ",us,", "status": "reviewed", "time": 1577850809023, "title": "M 5.0 - 57km SE of Petropavlovsk-Kamchatskiy, Russia", "tsunami": 0, "type": "earthquake", "types": ",dyfi,geoserve,origin,phase-data,", "tz": 660, "updated": 1580422251239, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t2a"}, "type": "Feature"} 38 | {"geometry": {"coordinates": [-176.6892, 51.6564, 53.64], "type": "Point"}, "id": "37", "properties": {"alert": null, "cdi": 3.4, "code": "70006t22", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t22&format=geojson", "dmin": 0.226, "felt": 1, "gap": 93, "id": "us70006t22", "ids": ",ak0201pf7cb,us70006t22,", "mag": 4.6, "magType": "mww", "mmi": null, "net": "us", "nst": null, "place": "24km S of Adak, Alaska", "rms": 0.89, "sig": 326, "sources": ",ak,us,", "status": "reviewed", "time": 1577848828279, "title": "M 4.6 - 24km S of Adak, Alaska", "tsunami": 0, "type": "earthquake", "types": ",dyfi,geoserve,moment-tensor,origin,phase-data,", "tz": -600, "updated": 1580429755249, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t22"}, "type": "Feature"} 39 | {"geometry": {"coordinates": [105.0195, 29.0289, 10.0], "type": "Point"}, "id": "38", "properties": {"alert": null, "cdi": null, "code": "70006t1x", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t1x&format=geojson", "dmin": 4.088, "felt": null, "gap": 33, "id": "us70006t1x", "ids": ",us70006t1x,", "mag": 4.8, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "17km S of Fushi, China", "rms": 0.73, "sig": 354, "sources": ",us,", "status": "reviewed", "time": 1577847718092, "title": "M 4.8 - 17km S of Fushi, China", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 480, "updated": 1580685414040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t1x"}, "type": "Feature"} 40 | {"geometry": {"coordinates": [19.3395, 41.569, 16.06], "type": "Point"}, "id": "39", "properties": {"alert": null, "cdi": 1.0, "code": "70006t1w", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t1w&format=geojson", "dmin": 0.862, "felt": 1, "gap": 51, "id": "us70006t1w", "ids": ",us70006t1w,", "mag": 4.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "28km NNW of Durres, Albania", "rms": 0.8, "sig": 259, "sources": ",us,", "status": "reviewed", "time": 1577847221778, "title": "M 4.1 - 28km NNW of Durres, Albania", "tsunami": 0, "type": "earthquake", "types": ",dyfi,geoserve,origin,phase-data,", "tz": 60, "updated": 1579238088161, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t1w"}, "type": "Feature"} 41 | {"geometry": {"coordinates": [142.5714, 41.8487, 68.53], "type": "Point"}, "id": "40", "properties": {"alert": null, "cdi": null, "code": "700070sw", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070sw&format=geojson", "dmin": 0.467, "felt": null, "gap": 120, "id": "us700070sw", "ids": ",us700070sw,", "mag": 4.3, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "56km SSE of Shizunai, Japan", "rms": 0.65, "sig": 284, "sources": ",us,", "status": "reviewed", "time": 1577841137070, "title": "M 4.3 - 56km SSE of Shizunai, Japan", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 600, "updated": 1579226962040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070sw"}, "type": "Feature"} 42 | {"geometry": {"coordinates": [138.3309, 32.2879, 321.34], "type": "Point"}, "id": "41", "properties": {"alert": null, "cdi": null, "code": "70006t17", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t17&format=geojson", "dmin": 1.478, "felt": null, "gap": 89, "id": "us70006t17", "ids": ",us70006t17,", "mag": 4.5, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "163km WSW of Hachijo-jima, Japan", "rms": 0.65, "sig": 312, "sources": ",us,", "status": "reviewed", "time": 1577840934688, "title": "M 4.5 - 163km WSW of Hachijo-jima, Japan", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 540, "updated": 1579225659040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t17"}, "type": "Feature"} 43 | {"geometry": {"coordinates": [152.6003, -5.3373, 32.93], "type": "Point"}, "id": "42", "properties": {"alert": null, "cdi": null, "code": "700070sx", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us700070sx&format=geojson", "dmin": 1.218, "felt": null, "gap": 45, "id": "us700070sx", "ids": ",us700070sx,", "mag": 4.6, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "107km SSW of Taron, Papua New Guinea", "rms": 1.08, "sig": 326, "sources": ",us,", "status": "reviewed", "time": 1577838948020, "title": "M 4.6 - 107km SSW of Taron, Papua New Guinea", "tsunami": 0, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 600, "updated": 1579216158040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us700070sx"}, "type": "Feature"} 44 | {"geometry": {"coordinates": [152.5514, -5.3245, 40.24], "type": "Point"}, "id": "43", "properties": {"alert": null, "cdi": null, "code": "70006t13", "detail": "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us70006t13&format=geojson", "dmin": 1.19, "felt": null, "gap": 74, "id": "us70006t13", "ids": ",us70006t13,", "mag": 5.1, "magType": "mb", "mmi": null, "net": "us", "nst": null, "place": "108km SSW of Taron, Papua New Guinea", "rms": 0.79, "sig": 400, "sources": ",us,", "status": "reviewed", "time": 1577838500289, "title": "M 5.1 - 108km SSW of Taron, Papua New Guinea", "tsunami": 1, "type": "earthquake", "types": ",geoserve,origin,phase-data,", "tz": 600, "updated": 1579215623040, "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us70006t13"}, "type": "Feature"} 45 | -------------------------------------------------------------------------------- /tests/test_geojson_to_sqlite.py: -------------------------------------------------------------------------------- 1 | from click.testing import CliRunner 2 | from dirty_equals import IsApprox 3 | from geojson_to_sqlite import cli 4 | import pytest 5 | import sqlite_utils 6 | from sqlite_utils.utils import find_spatialite 7 | import pathlib 8 | import json 9 | 10 | 11 | testdir = pathlib.Path(__file__).parent 12 | 13 | 14 | def test_version(): 15 | result = CliRunner().invoke(cli.cli, ["--version"]) 16 | assert result.exit_code == 0 17 | assert result.output.startswith("cli, version ") 18 | 19 | 20 | def test_invalid(tmpdir): 21 | db_path = str(tmpdir / "output.db") 22 | result = CliRunner().invoke( 23 | cli.cli, [db_path, "features", str(testdir / "invalid.geojson")] 24 | ) 25 | assert 1 == result.exit_code 26 | assert ( 27 | "Error: GeoJSON must be a Feature or a FeatureCollection" 28 | == result.stdout.strip() 29 | ) 30 | 31 | 32 | def test_single_feature(tmpdir): 33 | db_path = str(tmpdir / "output.db") 34 | result = CliRunner().invoke( 35 | cli.cli, [db_path, "features", str(testdir / "feature.geojson")] 36 | ) 37 | assert 0 == result.exit_code, result.stdout 38 | db = sqlite_utils.Database(db_path) 39 | assert ["features"] == db.table_names() 40 | rows = list(db["features"].rows) 41 | assert [ 42 | { 43 | "slug": "uk", 44 | "description": "Rough area around the UK", 45 | "geometry": '{"type": "Polygon", "coordinates": [[[-8.0859375, 60.930432202923335], [-16.875, 50.28933925329178], [-5.9765625, 48.922499263758255], [4.21875, 52.26815737376817], [1.0546875, 60.06484046010452], [-8.0859375, 60.930432202923335]]]}', 46 | } 47 | ] == rows 48 | 49 | 50 | @pytest.mark.skipif(not find_spatialite(), reason="Could not find SpatiaLite") 51 | def test_single_feature_no_properties(tmpdir): 52 | feature = json.loads((testdir / "feature.geojson").read_text()) 53 | feature["properties"] = {} 54 | db_path = str(tmpdir / "output.db") 55 | result = CliRunner().invoke( 56 | cli.cli, [db_path, "features", "-", "--spatialite"], input=json.dumps(feature) 57 | ) 58 | assert 0 == result.exit_code, result.stdout 59 | db = sqlite_utils.Database(db_path) 60 | assert "features" in db.table_names() 61 | assert set(db["features"].columns_dict.keys()) == {"geometry"} 62 | 63 | 64 | @pytest.mark.skipif(not find_spatialite(), reason="Could not find SpatiaLite") 65 | def test_feature_collection_spatialite(tmpdir): 66 | db_path = str(tmpdir / "output.db") 67 | result = CliRunner().invoke( 68 | cli.cli, 69 | [ 70 | db_path, 71 | "features", 72 | str(testdir / "feature-collection.geojson"), 73 | "--spatialite", 74 | "--alter", 75 | "--pk=slug", 76 | ], 77 | catch_exceptions=False, 78 | ) 79 | assert 0 == result.exit_code, result.stdout 80 | db = sqlite_utils.Database(db_path) 81 | db.init_spatialite() 82 | assert {"features", "spatial_ref_sys"}.issubset(db.table_names()) 83 | rows = db.execute_returning_dicts( 84 | "select slug, AsGeoJSON(geometry) as geometry from features" 85 | ) 86 | assert len(rows) == 2 87 | uk, usa = rows 88 | assert uk["slug"] == "uk" 89 | assert usa["slug"] == "usa" 90 | assert json.loads(uk["geometry"]) == { 91 | "type": "Polygon", 92 | "coordinates": [ 93 | [ 94 | [IsApprox(-8.0859374), IsApprox(60.93043220292332)], 95 | [IsApprox(-16.875), IsApprox(50.28933925329177)], 96 | [IsApprox(-5.9765625), IsApprox(48.92249926375824)], 97 | [IsApprox(4.21875), IsApprox(52.26815737376816)], 98 | [IsApprox(1.0546875), IsApprox(60.06484046010452)], 99 | [IsApprox(-8.0859375), IsApprox(60.93043220292332)], 100 | ] 101 | ], 102 | } 103 | assert json.loads(usa["geometry"]) == { 104 | "type": "Polygon", 105 | "coordinates": [ 106 | [ 107 | [IsApprox(-129.375), IsApprox(47.75409797968003)], 108 | [IsApprox(-119.53125), IsApprox(33.43144133557529)], 109 | [IsApprox(-96.6796875), IsApprox(25.48295117535531)], 110 | [IsApprox(-85.4296875), IsApprox(24.20688962239802)], 111 | [IsApprox(-77.34374999999998), IsApprox(25.48295117535531)], 112 | [IsApprox(-61.52343749999999), IsApprox(44.33956524809713)], 113 | [IsApprox(-84.375), IsApprox(51.39920565355377)], 114 | [IsApprox(-100.8984375), IsApprox(50.06419173665909)], 115 | [IsApprox(-115.3125), IsApprox(50.73645513701067)], 116 | [IsApprox(-129.375), IsApprox(47.75409797968003)], 117 | ] 118 | ], 119 | } 120 | initial_rows = db.execute_returning_dicts( 121 | "select slug, AsGeoJSON(geometry) as geometry from features" 122 | ) 123 | assert ["slug"] == db["features"].pks 124 | # Run it once more to check that upserting should work 125 | result = CliRunner().invoke( 126 | cli.cli, 127 | [ 128 | db_path, 129 | "features", 130 | str(testdir / "feature-collection.geojson"), 131 | "--spatialite", 132 | "--alter", 133 | "--pk=slug", 134 | ], 135 | catch_exceptions=False, 136 | ) 137 | assert 0 == result.exit_code 138 | # Rows should have stayed the same 139 | assert initial_rows == db.execute_returning_dicts( 140 | "select slug, AsGeoJSON(geometry) as geometry from features" 141 | ) 142 | 143 | 144 | @pytest.mark.skipif(not find_spatialite(), reason="Could not find SpatiaLite") 145 | @pytest.mark.parametrize("use_spatial_index", (True, False)) 146 | def test_spatial_index(tmpdir, use_spatial_index): 147 | db_path = str(tmpdir / "output.db") 148 | result = CliRunner().invoke( 149 | cli.cli, 150 | [ 151 | db_path, 152 | "features", 153 | str(testdir / "feature-collection.geojson"), 154 | ] 155 | + (["--spatial-index"] if use_spatial_index else ["--spatialite"]), 156 | catch_exceptions=False, 157 | ) 158 | assert 0 == result.exit_code, result.stdout 159 | db = sqlite_utils.Database(db_path) 160 | db.init_spatialite() 161 | has_idx = "idx_features_geometry" in db.table_names() 162 | assert has_idx == use_spatial_index 163 | has_spatial_index_geometry_columns = bool( 164 | list(db["geometry_columns"].rows_where("spatial_index_enabled = 1")) 165 | ) 166 | assert has_spatial_index_geometry_columns == use_spatial_index 167 | 168 | 169 | @pytest.mark.skipif(not find_spatialite(), reason="Could not find SpatiaLite") 170 | def test_spatial_index_twice(tmpdir): 171 | db_path = str(tmpdir / "output.db") 172 | args = [ 173 | db_path, 174 | "features", 175 | str(testdir / "feature-collection.geojson"), 176 | "--spatialite", 177 | "--spatial-index", 178 | ] 179 | result = CliRunner().invoke( 180 | cli.cli, 181 | args, 182 | catch_exceptions=False, 183 | ) 184 | assert 0 == result.exit_code, result.stdout 185 | 186 | db = sqlite_utils.Database(db_path) 187 | db.init_spatialite() 188 | assert "idx_features_geometry" in db.table_names() 189 | 190 | # now do it again 191 | result = CliRunner().invoke( 192 | cli.cli, 193 | args, 194 | catch_exceptions=False, 195 | ) 196 | assert 0 == result.exit_code, result.stdout 197 | 198 | 199 | def test_feature_collection(tmpdir): 200 | db_path = str(tmpdir / "output.db") 201 | result = CliRunner().invoke( 202 | cli.cli, [db_path, "features", str(testdir / "feature-collection.geojson")] 203 | ) 204 | assert 0 == result.exit_code, result.stdout 205 | db = sqlite_utils.Database(db_path) 206 | assert ["features"] == db.table_names() 207 | rows = list(db["features"].rows) 208 | assert [ 209 | { 210 | "slug": "uk", 211 | "description": "Rough area around the UK", 212 | "geometry": '{"type": "Polygon", "coordinates": [[[-8.0859375, 60.930432202923335], [-16.875, 50.28933925329178], [-5.9765625, 48.922499263758255], [4.21875, 52.26815737376817], [1.0546875, 60.06484046010452], [-8.0859375, 60.930432202923335]]]}', 213 | "continent": None, 214 | }, 215 | { 216 | "slug": "usa", 217 | "description": "Very rough area around the USA", 218 | "geometry": '{"type": "Polygon", "coordinates": [[[-129.375, 47.754097979680026], [-119.53125, 33.43144133557529], [-96.6796875, 25.48295117535531], [-85.4296875, 24.206889622398023], [-77.34374999999999, 25.48295117535531], [-61.52343749999999, 44.33956524809713], [-84.375, 51.39920565355378], [-100.8984375, 50.064191736659104], [-115.31249999999999, 50.736455137010665], [-129.375, 47.754097979680026]]]}', 219 | "continent": "North America", 220 | }, 221 | ] == rows 222 | assert ["rowid"] == db["features"].pks 223 | 224 | 225 | def test_multiple_files(tmpdir): 226 | db_path = str(tmpdir / "output.db") 227 | result = CliRunner().invoke( 228 | cli.cli, 229 | [ 230 | db_path, 231 | "features", 232 | str(testdir / "feature-collection.geojson"), 233 | str(testdir / "feature.geojson"), 234 | ], 235 | ) 236 | assert 0 == result.exit_code, result.stdout 237 | db = sqlite_utils.Database(db_path) 238 | assert ["features"] == db.table_names() 239 | rows = list(db["features"].rows) 240 | assert sorted([r["slug"] for r in rows]) == ["uk", "uk", "usa"] 241 | 242 | 243 | def test_feature_collection_pk_and_alter(tmpdir): 244 | db_path = str(tmpdir / "output.db") 245 | result = CliRunner().invoke( 246 | cli.cli, [db_path, "features", str(testdir / "feature.geojson"), "--pk=slug"] 247 | ) 248 | assert 0 == result.exit_code, result.stdout 249 | db = sqlite_utils.Database(db_path) 250 | assert ["features"] == db.table_names() 251 | rows = db.execute_returning_dicts("select slug, description from features") 252 | assert [{"slug": "uk", "description": "Rough area around the UK"}] == rows 253 | assert ["slug"] == db["features"].pks 254 | 255 | # Running it again should insert usa 256 | result = CliRunner().invoke( 257 | cli.cli, 258 | [ 259 | db_path, 260 | "features", 261 | str(testdir / "feature-collection.geojson"), 262 | "--pk=slug", 263 | "--alter", 264 | ], 265 | ) 266 | assert 0 == result.exit_code, result.stdout 267 | rows = db.execute_returning_dicts("select slug, description from features") 268 | assert [ 269 | {"slug": "uk", "description": "Rough area around the UK"}, 270 | {"slug": "usa", "description": "Very rough area around the USA"}, 271 | ] == rows 272 | 273 | 274 | def test_feature_collection_id_as_pk(tmpdir): 275 | db_path = str(tmpdir / "output.db") 276 | result = CliRunner().invoke( 277 | cli.cli, [db_path, "features", str(testdir / "feature-collection-ids.geojson")] 278 | ) 279 | assert 0 == result.exit_code, result.stdout 280 | db = sqlite_utils.Database(db_path) 281 | features = db["features"] 282 | 283 | # check that we're setting the right pk 284 | assert "id" in features.columns_dict and ["id"] == features.pks 285 | 286 | uk = features.get(3) 287 | usa = features.get(8) 288 | 289 | assert "uk" == uk["slug"] 290 | assert "usa" == usa["slug"] 291 | 292 | assert [c.name for c in features.columns] == [ 293 | "id", 294 | "slug", 295 | "description", 296 | "geometry", 297 | "continent", 298 | ] 299 | 300 | 301 | def test_feature_collection_override_id(tmpdir): 302 | db_path = str(tmpdir / "output.db") 303 | result = CliRunner().invoke( 304 | cli.cli, 305 | [ 306 | db_path, 307 | "features", 308 | str(testdir / "feature-collection-ids.geojson"), 309 | "--pk=slug", 310 | ], 311 | ) 312 | assert 0 == result.exit_code, result.stdout 313 | db = sqlite_utils.Database(db_path) 314 | features = db["features"] 315 | 316 | assert ["slug"] == features.pks 317 | 318 | uk = features.get("uk") 319 | usa = features.get("usa") 320 | 321 | assert "Rough area around the UK" == uk["description"] 322 | assert "North America" == usa["continent"] 323 | 324 | 325 | def test_ndjson(tmpdir): 326 | ndjson = testdir / "quakes.ndjson" 327 | db_path = str(tmpdir / "output.db") 328 | 329 | with open(ndjson) as f: 330 | data = [json.loads(line) for line in f] 331 | 332 | result = CliRunner().invoke(cli.cli, [db_path, "features", str(ndjson), "--nl"]) 333 | assert 0 == result.exit_code, result.stdout 334 | 335 | db = sqlite_utils.Database(db_path) 336 | features = db["features"] 337 | 338 | assert features.count == 44 339 | 340 | # the quakes dataset has an id attribute set, 341 | # so check that we're setting the right pk 342 | assert "id" in features.columns_dict and ["id"] == features.pks 343 | 344 | 345 | @pytest.mark.skipif(not find_spatialite(), reason="Could not find SpatiaLite") 346 | def test_ndjson_with_spatial_index(tmpdir): 347 | ndjson = testdir / "quakes.ndjson" 348 | db_path = str(tmpdir / "output.db") 349 | result = CliRunner().invoke( 350 | cli.cli, [db_path, "features", str(ndjson), "--nl", "--spatial-index"] 351 | ) 352 | assert 0 == result.exit_code, result.stdout 353 | # There should be a spatial index 354 | db = sqlite_utils.Database(db_path) 355 | db.init_spatialite() 356 | has_spatial_index_geometry_columns = bool( 357 | list(db["geometry_columns"].rows_where("spatial_index_enabled = 1")) 358 | ) 359 | assert has_spatial_index_geometry_columns 360 | # And 44 rows in the quakes table 361 | assert db["features"].count == 44 362 | 363 | 364 | def test_missing_geometry(tmpdir): 365 | quakes = testdir / "quakes.geojson" 366 | db_path = str(tmpdir / "output.db") 367 | result = CliRunner().invoke(cli.cli, [db_path, "features", str(quakes)]) 368 | 369 | assert 0 == result.exit_code, result.stdout 370 | 371 | db = sqlite_utils.Database(db_path) 372 | assert db["features"].count == 10 373 | 374 | # this should have a null geometry 375 | rows = list(db["features"].rows) 376 | nulls = [row for row in rows if row["geometry"] is None] 377 | 378 | assert len(rows) == 10 379 | assert len(nulls) == 2 380 | 381 | 382 | def test_bundle_properties(tmpdir): 383 | db_path = str(tmpdir / "output.db") 384 | result = CliRunner().invoke( 385 | cli.cli, 386 | [ 387 | db_path, 388 | "features", 389 | str(testdir / "feature-collection.geojson"), 390 | "--properties", 391 | ], 392 | ) 393 | assert 0 == result.exit_code, result.stdout 394 | db = sqlite_utils.Database(db_path) 395 | assert ["features"] == db.table_names() 396 | 397 | assert db["features"].columns_dict == { 398 | "properties": str, 399 | "geometry": str, 400 | } 401 | 402 | 403 | def test_bundle_properties_colname(tmpdir): 404 | db_path = str(tmpdir / "output.db") 405 | result = CliRunner().invoke( 406 | cli.cli, 407 | [ 408 | db_path, 409 | "features", 410 | str(testdir / "feature-collection.geojson"), 411 | "--properties", 412 | "props", 413 | ], 414 | ) 415 | assert 0 == result.exit_code, result.stdout 416 | db = sqlite_utils.Database(db_path) 417 | assert ["features"] == db.table_names() 418 | 419 | assert db["features"].columns_dict == { 420 | "props": str, 421 | "geometry": str, 422 | } 423 | 424 | 425 | @pytest.mark.skipif(not find_spatialite(), reason="Could not find SpatiaLite") 426 | def test_bundle_properties_spatialite(tmpdir): 427 | db_path = str(tmpdir / "output.db") 428 | result = CliRunner().invoke( 429 | cli.cli, 430 | [ 431 | db_path, 432 | "features", 433 | str(testdir / "feature-collection.geojson"), 434 | "--properties", 435 | "--spatialite", 436 | ], 437 | ) 438 | assert 0 == result.exit_code, result.stdout 439 | db = sqlite_utils.Database(db_path) 440 | assert "features" in db.table_names() 441 | 442 | assert db["features"].columns_dict == { 443 | "properties": str, 444 | "geometry": float, # no idea why this is float, but it's consistent 445 | } 446 | --------------------------------------------------------------------------------