Skip to content

Commit

Permalink
Improved SpatiaLite example, closes #401
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 6, 2022
1 parent aa24903 commit fea8c9b
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1672,28 +1672,21 @@ A more useful example: if you are working with `SpatiaLite <https://www.gaia-gis
import sqlite3
import sqlite_utils
from shapely.geometry import shape
import requests
import httpx
# 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")
db = sqlite_utils.Database("places.db")
# Initialize SpatiaLite
db.init_spatialite()
# Use sqlite-utils to create a places table
places = db["places"].create({"id": int, "name": str})
# 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.execute("select InitSpatialMetadata(1)")
db.execute(
"SELECT AddGeometryColumn('places', 'geometry', 4326, 'MULTIPOLYGON', 2);"
)
# Add a SpatiaLite 'geometry' column
places.add_geometry_column("geometry", "MULTIPOLYGON")
# Fetch some GeoJSON from Who's On First:
geojson = requests.get(
"https://data.whosonfirst.org/404/227/475/404227475.geojson"
geojson = httpx.get(
"https://raw.githubusercontent.com/whosonfirst-data/"
"whosonfirst-data-admin-gb/master/data/404/227/475/404227475.geojson"
).json()
# Convert to "Well Known Text" format using shapely
Expand All @@ -1705,6 +1698,8 @@ A more useful example: if you are working with `SpatiaLite <https://www.gaia-gis
conversions={"geometry": "GeomFromText(?, 4326)"},
)
This example uses gographical data from [Who's On First](https://whosonfirst.org/) and depends on the [Shapely](https://shapely.readthedocs.io/en/stable/manual.html) and [HTTPX](https://www.python-httpx.org/) Python libraries.
.. _python_api_introspection:
Introspecting tables and views
Expand Down

0 comments on commit fea8c9b

Please sign in to comment.