SQL/SpatiaLite: 如何将列声明为几何?

SQL/SpatiaLite: how to declare a column as geometry?

我正在通过来自空间 table 的 SQL 查询创建一个新的 table:

CREATE TABLE SomeShapes AS
SELECT ash.id, ash.Geometry
FROM AllShapes ash
WHERE ash.id = 30

但是,这个 returns "normal" table,所以当我尝试在 GIS 程序 (QGIS) 中加载它时,它没有显示几何图形。我如何声明几何列包含几何?

您需要创建一个 "non-spatial" table,然后向其中添加 Geometry 列。

然后,您可以将数据插入 table。

不能一步完成(create table as select)。来自 the documentation:

Creating a Geometry-type at the same time the corresponding table is created isn't allowed. You always must first create the table, then adding the Geometry-column in a second time and as a separate step.

CREATE TABLE test_geom (
  id INTEGER NOT NULL
    PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  measured_value DOUBLE NOT NULL);

SELECT AddGeometryColumn('test_geom', 'Geometry', 4326, 'POINT', 'XY');

此外,考虑到您可能希望使用 spatial indexes 来提高性能

SELECT CreateSpatialIndex('test_geom', 'Geometry');