kj-9 / setup-spatialite-macos

getting started with spatialite on macOS
0 stars 0 forks source link

setup-spatialite-macos

this repo is a simple way to setup python virtual environment with spatialite support on macos.

setup

run:

curl -sL https://raw.githubusercontent.com/kj-9/setup-spatialite-macos/main/scripts/setup.sh | \
  bash -s -- .venv

does:

created venv uses brew installed python3, so it should have sqlite3 with extension support. after running the setup script, you need to use brew installed sqlite3, so you might want to run:

echo "export PATH=/usr/local/opt/sqlite/bin:$PATH" >> .zshrc

you can change venv path by changing the .venv in the command above.

if only runs bash -s without the venv path, it will not create a virtual environment, but will install the required packages.

why use brew installed python3

using brew to install sqlite3 and python3 and using these are the basis for this setup.

example usage of spatialite with python sqlite-utils cli

activate the virtual environment:

source .venv/bin/activate

install sqlite-utils:

pip install -r sqlite-utils

initialize a new sqlite database with spatialite extension:

python -m sqlite_utils create-database sample.db --init-spatialite

where

using sqlite-utils to load spatialite extension and check version:

python -m sqlite_utils --load-extension=spatialite \
    sample.db "select spatialite_version()"

loading geojson data into the database using spatialite function ImportGeoJSON:

SPATIALITE_SECURITY=relaxed python -m sqlite_utils --load-extension=spatialite \
    sample.db "select ImportGeoJSON('points.json', 'points')"

where

show table ddl:

python -m sqlite_utils schema sample.db points

CREATE TABLE "points" (
        pk_uid INTEGER PRIMARY KEY AUTOINCREMENT,
        "name" TEXT, "geometry" POINT)

you can export spatialite table to geojson using ExportGeoJSON:

SPATIALITE_SECURITY=relaxed python -m sqlite_utils --load-extension=spatialite \
    sample.db "select ExportGeoJSON2('points', 'geometry', 'points-export.json')"