Package: geojson-to-sqlite
geojson-to-sqliteCLI tool for converting GeoJSON to SQLite (optionally with SpatiaLite)RFC 7946: The GeoJSON FormatHow to install$ pip install geojson-to-sqliteHow to useYou can run this tool against a GeoJSON file like so:$ geojson-to-sqlite my.db features features.geojsonThis will load all of the features from thefeatures.geojsonfile into a table calledfeatures.Each row will have ageometrycolumn containing the feature geometry, and columns for each of the keys found in anypropertiesattached to those features. (To bundle all properties into a single JSON object, use the--propertiesflag.)The table will be created the first time you run the command.On subsequent runs you can use the--alteroption to add any new columns that are missing from the table.You can pass more than one GeoJSON file, in which case the contents of all of the files will be inserted into the same table.If your features have an"id"property it will be used as the primary key for the table. You can also use--pk=PROPERTYwith the name of a different property to use that as the primary key instead. If you don't want to use the"id"as the primary key (maybe it contains duplicate values) you can use--pk ''to specify no primary key.Specifying a primary key also will allow you to upsert data into the rows instead of insert data into new rows.If no primary key is specified, a SQLiterowidcolumn will be used.You can use-as the filename to import from standard input. For example:$ curl https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_20m.json \
    | geojson-to-sqlite my.db states - --pk GEO_IDUsing with SpatiaLiteBy default, thegeometrycolumn will contain JSON.If you have installed theSpatiaLitemodule for SQLite you can instead import the geometry into a geospatially indexed column.You can do this using the--spatialiteoption, like so:$ geojson-to-sqlite my.db features features.geojson --spatialiteThe tool will search for the SpatiaLite module in the following locations:/usr/lib/x86_64-linux-gnu/mod_spatialite.so/usr/local/lib/mod_spatialite.dylibIf you have installed the module in another location, you can use the--spatialite_mod=xxxoption to specify where:$ geojson-to-sqlite my.db features features.geojson \
    --spatialite_mod=/usr/lib/mod_spatialite.dylibYou can create a SpatiaLite spatial index on thegeometrycolumn using the--spatial-indexoption:$ geojson-to-sqlite my.db features features.geojson --spatial-indexUsing this option implies--spatialiteso you do not need to add that.Streaming large datasetsFor large datasets, consider using newline-delimited JSON to stream features into the database without loading the entire feature collection into memory.For example, to load a day of earthquake reports from USGS:$ geojson-to-sqlite quakes.db quakes tests/quakes.ndjson \
  --nl --pk=id --spatialiteWhen using newline-delimited JSON, tables will also be created from the first feature, instead of guessing types based on the first 100 features.If you want to use a larger subset of your data to guess column types (for example, if some fields are inconsistent) you can usefionato collect features into a single collection.$ head tests/quakes.ndjson | fio collect | \
  geojson-to-sqlite quakes.db quakes - --spatialiteThis will take the first 10 lines fromtests/quakes.ndjson, pass them tofio collect, which turns them into a single feature collection, and pass that, in turn, togeojson-to-sqlite.Using this with DatasetteDatabases created using this tool can be explored and published usingDatasette.The Datasette documentation includes a section onhow to use it to browse SpatiaLite databases.Thedatasette-leaflet-geojsonplugin can be used to visualize columns containing GeoJSON geometries on aLeafletmap.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 SpaitaLiteAsGeoJSON()function - something like this:selectrowid,AsGeoJSON(geometry)frommytablelimit10Thedatasette-geojson-mapis an alternative plugin which will automatically render SpatiaLite geometries as a Leaflet map on the corresponding table page, without needing you to callAsGeoJSON(geometry).