kj-9 / setup-spatialite-macos

getting started with spatialite on macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  • run brew to install sqlite3 (with extension support) and spatialite-tools
  • run brew to install python3 (with sqlite3 extension support)
  • create a python virtual environment at .venv (you can change the path as needed)

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

  • macOS's system sqlite3 is not compiled with extension (like spatialite) support
  • if you use spatialite from python, it needs to be built with sqlite extension support (see also: here)

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

  • sample.db is the sqlite database file, created by sqlite-utils
  • use python -m to run the sqlite-utils module to ensure the brew isntalled python venv environment is used

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

  • SPATIALITE_SECURITY=relaxed is required for some functions like ImportGeoJSON / ExportGeoJSON to work

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')"

About

getting started with spatialite on macOS


Languages

Language:Shell 78.4%Language:Makefile 21.6%