mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-17 14:04:11 +02:00
Add new gis.py module with spatialite helper methods
This commit is contained in:
parent
74586d3cb2
commit
b579a9c8ae
7 changed files with 216 additions and 20 deletions
44
sqlite_utils/gis.py
Normal file
44
sqlite_utils/gis.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import os
|
||||
from .db import Database, Table
|
||||
|
||||
SPATIALITE_PATHS = (
|
||||
"/usr/lib/x86_64-linux-gnu/mod_spatialite.so",
|
||||
"/usr/local/lib/mod_spatialite.dylib",
|
||||
)
|
||||
|
||||
|
||||
def find_spatialite() -> str:
|
||||
for path in SPATIALITE_PATHS:
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
def init_spatialite(db: Database, path: str) -> None:
|
||||
"Load spatialite extension for a database"
|
||||
db.conn.enable_load_extension(True)
|
||||
db.conn.load_extension(path)
|
||||
# Initialize SpatiaLite if not yet initialized
|
||||
if "spatial_ref_sys" in db.table_names():
|
||||
return
|
||||
db.conn.execute("select InitSpatialMetadata(1)")
|
||||
|
||||
|
||||
def add_geometry_column(
|
||||
table: Table,
|
||||
geometry_type: str,
|
||||
column_name: str = "geometry",
|
||||
srid: int = 4326,
|
||||
coord_dimension: str = "XY",
|
||||
not_null: bool = False,
|
||||
) -> None:
|
||||
"Add a geometry column to a table"
|
||||
table.db.conn.execute(
|
||||
"SELECT AddGeometryColumn(?, ?, ?, ?, ?, ?);",
|
||||
[table.name, column_name, srid, geometry_type, coord_dimension, int(not_null)],
|
||||
)
|
||||
|
||||
|
||||
def create_spatial_index(table: Table, column_name: str = "geometry") -> None:
|
||||
"Create a spatial index for a table and column"
|
||||
table.db.conn.execute("select CreateSpatialIndex(?, ?)", [table.name, column_name])
|
||||
Loading…
Add table
Add a link
Reference in a new issue