Skip to content

Instantly share code, notes, and snippets.

@plouvart
Last active May 4, 2023 14:07
Show Gist options
  • Save plouvart/f9096e79744a801f99d01b55fe89240e to your computer and use it in GitHub Desktop.
Save plouvart/f9096e79744a801f99d01b55fe89240e to your computer and use it in GitHub Desktop.
Python sqlite3 spatialite setup

Install sqlite3.

sudo apt install sqlite3

Install spatialite.

sudo apt install libsqlite3-mod-spatialite

Compile python3 with extension support for sqlite3. Here I'm using pyenv to manage the installation.

LDFLAGS="-L/lib/x86_64-linux-gnu/" PYTHON_CONFIGURE_OPTS="--enable-loadable-sqlite-extensions" pyenv install 3.10.5

Now spatialite is available and geometries can be loaded from sqlite3 databases using geopandas directly. Make sure to use the python you just installed.

import sqlite3
import geopandas as gpd

con = sqlite3.connect("EcrRiv.sqlite")
con.enable_load_extension(True)
con.execute("select load_extension('mod_spatialite');")
gpd.GeoDataFrame.from_postgis("SELECT CATCHMENT_,Hex(ST_AsBinary(Geometry)) as geom FROM c_tr", con, geom_col="geom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment