this repo is a simple way to setup python virtual environment with spatialite support on macos.
run:
curl -sL https://raw.githubusercontent.com/kj-9/setup-spatialite-macos/main/scripts/setup.sh | \
bash -s -- .venv
does:
.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.
using brew to install sqlite3 and python3 and using these are the basis for this setup.
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-utilspython -m
to run the sqlite-utils module to ensure the brew isntalled python venv environment is usedusing 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')"