Usage

On the console

Geometry-to-spatialite installs two commands: shapefile-to-spatialite and geojson-to-spatialite. Both provide the same arguments.

Basic usage

shapefile-to-spatialite myfile.shp mydatabase.db

This will create a new SQLite database called mydatabase.db containing a single table, myfile

You can provide multiple files:

shapefile-to-spatialite one.shp two.shp bundle.db

The bundle.db database will contain two tables, one and two.

This means you can use wildcards:

shapefile-to-spatialite ~/Downloads/*.shp mydownloads.db

If you pass a path to one or more directories, the script will recursively search those directories for files and create tables for each one:

shapefile-to-spatialite ~/path/to/directory all-my-shapefiles.db

For more help on usage and arguments, run

shapefile-to-spatialite --help

or

geojson-to-spatialite --help

As a library

Basic usage

from geometry_to_spatialite import (
    geojson_to_spatialite,
    shp_to_spatialite,
    DataImportError
)


try:
    # Import myfile.geojson into mydatabase.db using the default settings
    geojson_to_spatialite('mydatabase.db', 'myfile.geojson')
except DataImportError:
    raise

API Reference

geometry_to_spatialite.geojson_to_spatialite(sqlite_db, geojson_file, table_name=None, spatialite_extension=None, srid=4326, pk=None, write_mode=None, geom_type='GEOMETRY')

Load a GeoJSON file into a SpatiaLite database

Parameters:
  • sqlite_db (str) – Name of the SQLite database file

  • geojson_file (str) – Path to a GeoJSON file to import

  • table_name (str, optional) – Custom table name. Default: None (use the GeoJSON file name)

  • spatialite_extension (str, optional) – Path to mod_spatialite extension. In most cases the spatialite extension can be automatically detected and loaded. If not you can manully pass a path to the .so .dylib or .dll file. Default: None (attempt to load automatically)

  • srid (int, optional) – Spatial Reference ID (SRID). Default: 4326

  • pk (Union[str, list, tuple], optional) – Field (str) or fields (list/tuple) to use as a primary key. Default: None (no primary key)

  • write_mode (str, optional) – By default we assume the target table does not already exist. Pass ‘replace’ or ‘append’ to overwrite or append to an existing table. Default: None (assume the table doesn’t already exist)

  • geom_type (str, optional) – Data type to use for the geometry column. Default: "GEOMETRY"

Returns:

None

Raises:

DataImportError

geometry_to_spatialite.shp_to_spatialite(sqlite_db, shp_file, table_name=None, spatialite_extension=None, srid=4326, pk=None, write_mode=None, geom_type='GEOMETRY')

Load a SHP file into a SpatiaLite database

Parameters:
  • sqlite_db (str) – Name of the SQLite database file

  • shp_file (str) – Path to a SHP file to import

  • table_name (str, optional) – Custom table name. Default: None (use the SHP file name)

  • spatialite_extension (str, optional) – Path to mod_spatialite extension. In most cases the spatialite extension can be automatically detected and loaded. If not you can manully pass a path to the .so .dylib or .dll file. Default: None (attempt to load automatically)

  • srid (int, optional) – Spatial Reference ID (SRID). Default: 4326

  • pk (Union[str, list, tuple], optional) – Field (str) or fields (list/tuple) to use as a primary key. Default: None (no primary key)

  • write_mode (str, optional) – By default we assume the target table does not already exist. Pass ‘replace’ or ‘append’ to overwrite or append to an existing table. Default: None (assume the table doesn’t already exist)

  • geom_type (str, optional) – Data type to use for the geometry column. Default: "GEOMETRY"

Returns:

None

Raises:

DataImportError

exception geometry_to_spatialite.DataImportError