mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-16 05:24:26 +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
|
|
@ -19,7 +19,6 @@ import tabulate
|
|||
from .utils import (
|
||||
_compile_code,
|
||||
file_progress,
|
||||
find_spatialite,
|
||||
sqlite3,
|
||||
decode_base64_values,
|
||||
progressbar,
|
||||
|
|
@ -28,6 +27,8 @@ from .utils import (
|
|||
TypeTracker,
|
||||
)
|
||||
|
||||
from .gis import find_spatialite
|
||||
|
||||
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
||||
|
||||
VALID_COLUMN_TYPES = ("INTEGER", "TEXT", "FLOAT", "BLOB")
|
||||
|
|
|
|||
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])
|
||||
|
|
@ -22,11 +22,6 @@ except ImportError:
|
|||
|
||||
OperationalError = sqlite3.OperationalError
|
||||
|
||||
SPATIALITE_PATHS = (
|
||||
"/usr/lib/x86_64-linux-gnu/mod_spatialite.so",
|
||||
"/usr/local/lib/mod_spatialite.dylib",
|
||||
)
|
||||
|
||||
|
||||
def suggest_column_types(records):
|
||||
all_column_types = {}
|
||||
|
|
@ -96,13 +91,6 @@ def decode_base64_values(doc):
|
|||
return dict(doc, **{k: base64.b64decode(doc[k]["encoded"]) for k in to_fix})
|
||||
|
||||
|
||||
def find_spatialite():
|
||||
for path in SPATIALITE_PATHS:
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
class UpdateWrapper:
|
||||
def __init__(self, wrapped, update):
|
||||
self._wrapped = wrapped
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue