eyeseast / geocode-sqlite

Geocode rows in a SQLite database table
Apache License 2.0
231 stars 6 forks source link

Spatialite helper? #4

Closed eyeseast closed 2 years ago

eyeseast commented 4 years ago

Would it be worth helping convert lat/lng fields to a spatial index? https://docs.datasette.io/en/stable/spatialite.html#spatial-indexing-latitude-longitude-columns

Here's the whole code block:

import sqlite3
conn = sqlite3.connect('museums.db')
# Lead the spatialite extension:
conn.enable_load_extension(True)
conn.load_extension('/usr/local/lib/mod_spatialite.dylib')
# Initialize spatial metadata for this database:
conn.execute('select InitSpatialMetadata(1)')
# Add a geometry column called point_geom to our museums table:
conn.execute("SELECT AddGeometryColumn('museums', 'point_geom', 4326, 'POINT', 2);")
# Now update that geometry column with the lat/lon points
conn.execute('''
    UPDATE events SET
    point_geom = GeomFromText('POINT('||"longitude"||' '||"latitude"||')',4326);
''')
# Now add a spatial index to that column
conn.execute('select CreateSpatialIndex("museums", "point_geom");')
# If you don't commit your changes will not be persisted:
conn.commit()
conn.close()

I don't know if it belongs in this package or not.

eyeseast commented 2 years ago

This is tied into #22 (and #23). If you want to use spatialite, I think you probably have to use geojson.

eyeseast commented 2 years ago

This is now in sqlite-utils.