-
-
Notifications
You must be signed in to change notification settings - Fork 121
Closed
Labels
Description
As demonstrated by this piece of documentation, using SpatiaLite with sqlite-utils requires a fair bit of boilerplate:
sqlite-utils/docs/python-api.rst
Lines 880 to 909 in f728917
# Open a database and load the SpatiaLite extension: | |
import sqlite3 | |
conn = sqlite3.connect("places.db") | |
conn.enable_load_extension(True) | |
conn.load_extension("/usr/local/lib/mod_spatialite.dylib") | |
# Use sqlite-utils to create a places table: | |
db = sqlite_utils.Database(conn) | |
places = db["places"].create({"id": int, "name": str,}) | |
# Add a SpatiaLite 'geometry' column: | |
db.conn.execute("select InitSpatialMetadata(1)") | |
db.conn.execute( | |
"SELECT AddGeometryColumn('places', 'geometry', 4326, 'MULTIPOLYGON', 2);" | |
) | |
# Fetch some GeoJSON from Who's On First: | |
geojson = requests.get( | |
"https://data.whosonfirst.org/404/227/475/404227475.geojson" | |
).json() | |
# Convert to "Well Known Text" format using shapely | |
wkt = shape(geojson["geometry"]).wkt | |
# Insert the record, converting the WKT to a SpatiaLite geometry: | |
db["places"].insert( | |
{"name": "Wales", "geometry": wkt}, | |
conversions={"geometry": "GeomFromText(?, 4326)"}, | |
) |