【问题标题】:find which of the columns in postgis table are of type geometry and which are attributes查找 postgis 表中的哪些列是几何类型,哪些是属性
【发布时间】:2022-01-15 10:52:14
【问题描述】:

我需要在 postgres 表中找到所有包含几何值的列,我试图让它像这样工作。但我不确定它是否会一直有效。

list_geometry_columns = pd.read_sql_query(f"""select column_name from information_schema.columns where table_schema = '{schema_name}' and table_name = '{tbl_name}' and data_type='USER-DEFINED'""",con=engine)['column_name'].tolist()
list_attrib_columns = pd.read_sql_query(f"""select column_name from information_schema.columns where table_schema = '{schema_name}' and table_name = '{tbl_name}' and data_type!='USER-DEFINED'""",con=engine)['column_name'].tolist()

在我当前的实现中,“USER-DEFINED”也可能出现在其他列中。

【问题讨论】:

    标签: python postgresql geometry postgis


    【解决方案1】:

    您可以查询geometry_column 视图(或geography_column)以获取几何图形

    SELECT *
    FROM geometry_columns
    WHERE f_table_schema = 'myschema'
    AND f_table_name = 'myTable';
    

    或者,您可以获取每个列类型/格式类型的列表并过滤几何:

    SELECT
        pg_attribute.attname AS column_name,
        pg_type.typname AS data_type,
        pg_catalog.format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS format_type
    FROM
        pg_catalog.pg_attribute
    JOIN pg_catalog.pg_type
        ON pg_attribute.atttypid = pg_type.oid
    INNER JOIN
        pg_catalog.pg_class ON pg_class.oid = pg_attribute.attrelid
    INNER JOIN
        pg_catalog.pg_namespace ON pg_namespace.oid = pg_class.relnamespace
    WHERE
        pg_attribute.attnum > 0
        AND NOT pg_attribute.attisdropped
        --AND pg_type.typname = 'geometry'
        AND pg_namespace.nspname = 'public'
        AND pg_class.relname = 'my_table'
    ORDER BY
        attnum ASC;
    

    【讨论】:

    • 感谢@JGH,但我的视图显示为空,所以我没有得到任何列名。
    • @bhavesh 这很奇怪...查看我的编辑以另一种方式获取依赖于 pg_catalog 的信息
    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2021-10-24
    • 2015-06-30
    • 2015-08-09
    • 2012-10-27
    相关资源
    最近更新 更多