QGIS 在 Postgis 中显示来自查询的点

QGIS display points from query in Postgis

我正在从 postgresql/postgis table.

中的 x,y 列的视图中创建点几何

我愿意:

create or replace view my_points as select ST_SetSRID(ST_MakePoint(geo.x,geo.y), 2154) as geom, pg_typeof(geom) as type from table1 join table2 geo on table1.id = geo.id

在 postgresql 中一切正常,我可以看到这个 returns 视图在列 geom 中有一个几何图形。只是为了检查类型,还有第二个列类型,用于检查 geom 的格式是否为几何。

现在,当我在 QGIS 中并尝试将视图放入图层列表时,会出现以下消息:

Layer is not valid: The layer dbname='x' host=x port=5432 user='x' password='x' sslmode=x key='geom' srid=2154 type=POINT table="public"."my_points" (geom) sql= is not a valid layer and can not be added to the map

这是怎么回事?无法在要导入 QGIS 的查询中创建 geom?

ps: 我使用 QGIS 2.8.1

您可以在 QGIS 中加载 PostGIS 视图。但是,您的视图需要有一列,每一行的值都可以作为主键。在 QGIS 中,在 "Add PostGIS Table(s)" 对话框中,您需要在 "Primary Key" 菜单中指定此列(在下面的屏幕截图中,此列称为 id_tronc;菜单显示所有可用列)。

编辑: 以上解决方案可行,但答案并不完全正确。 QGIS 不需要主键,至少较新的版本是这样。问题可能是列类型,请参阅@tommaso-di-bucchianico 的

问题出在 geom 类型的字段 regtype 上。我不知道为什么这会打扰 Qgis,但确实如此。

将其更改为 text 视图将起作用:

create or replace view my_points as
   select 
   ST_SetSRID(ST_MakePoint(geo.x,geo.y), 2154) as geom,
   pg_typeof(geom)::TEXT as type
   from table1
   join table2 geo on table1.id = geo.id

虽然在 "Add PostGIS Table(s)" 对话框中指定主键列肯定有效,但我希望通过简单的拖放操作(从浏览器面板到图层面板)带来便利。对我有用的是重新排序视图定义中的列,以便主键列位于第一位。然后我可以简单地将浏览器面板中的视图直接拖到图层面板中而不会出错。