import sedonadb as sd
db = sd.connect()
# Create spatial points with GF(3) trits
result = db.sql("""
SELECT
'Brass Factory' as name,
ST_Point(-73.9615, 40.7175) as geom,
0 as gf3_trit
UNION ALL
SELECT 'Domino Park', ST_Point(-73.9654, 40.7141), 1
UNION ALL
SELECT 'McCarren Park', ST_Point(-73.9502, 40.7203), -1
""")
# GF(3) verification
result = db.sql("""
SELECT SUM(gf3_trit) % 3 = 0 as balanced FROM locations
""")
OLC Tile Hierarchy
# Create Plus Code hierarchy
result = db.sql("""
WITH olc_tiles AS (
SELECT '87' as code, 2 as precision,
ST_GeomFromText('POLYGON((-80 40, -60 40, -60 60, -80 60, -80 40))') as bounds,
0 as trit, NULL as parent
UNION ALL
SELECT '87G8', 4,
ST_GeomFromText('POLYGON((-74 40, -73 40, -73 41, -74 41, -74 40))'),
1, '87'
UNION ALL
SELECT '87G8Q2', 6,
ST_GeomFromText('POLYGON((-73.98 40.71, -73.93 40.71, -73.93 40.76, -73.98 40.76, -73.98 40.71))'),
0, '87G8'
)
SELECT code, precision, ST_AsText(ST_Centroid(bounds)) as centroid, trit
FROM olc_tiles
""")
Morphism Up-Traversal
# P6 → P4 → P2 via recursive CTE
result = db.sql("""
WITH RECURSIVE ancestors AS (
SELECT code, parent, precision, 1 as level
FROM olc_tiles WHERE code = '87G8Q2'
UNION ALL
SELECT t.code, t.parent, t.precision, a.level + 1
FROM olc_tiles t
JOIN ancestors a ON t.code = a.parent
)
SELECT code, precision, level FROM ancestors ORDER BY level
""")
# Returns: 87G8Q2 (level 1) → 87G8 (level 2) → 87 (level 3)
Spatial Containment (Morphism-like)
# Which tiles contain Brass Factory?
result = db.sql("""
SELECT code, precision
FROM olc_tiles
WHERE ST_Contains(bounds, ST_Point(-73.9615, 40.7175))
ORDER BY precision
""")
# Returns: 87 (P2), 87G8 (P4), 87G8Q2 (P6)
KNN Query
# 2 nearest tiles to query point
result = db.sql("""
SELECT code, ST_Distance(centroid, ST_Point(-73.9615, 40.7175)) * 111 as km
FROM olc_tiles
ORDER BY ST_Distance(centroid, ST_Point(-73.9615, 40.7175))
LIMIT 2
""")
Fokker-Planck Neighbors
# Find touching tiles for diffusion
result = db.sql("""
SELECT a.code as tile, b.code as neighbor
FROM olc_tiles a, olc_tiles b
WHERE a.code < b.code AND ST_Touches(a.bounds, b.bounds)
""")
# Install
uv pip install "apache-sedona[db]"
# Test
uv run python3 -c "
import sedonadb as sd
db = sd.connect()
print(db.sql('SELECT ST_Point(0,1)'))
"
# Run full test suite
uv run python3 ~/.claude/skills/sedonadb-geoacset/test_geoacset.py