【问题标题】:PostGIS not recognizing geometry type after writing to database with geopandas使用 geopandas 写入数据库后 PostGIS 无法识别几何类型
【发布时间】:2022-10-30 22:19:43
【问题描述】:

我有一个启用空间的 PostGIS 数据库。

engine = create_engine(f'postgresql://user:password@localhost:5432/{dbname}')

我有一个有效的 geopandas 数据框(即,如果我导出为 .shp 或 .gpkg,我可以毫无错误地加载到 GIS 应用程序中),其中包含一个名为“geometry”的几何列。我将这些数据写入数据库:

gdf.to_postgis('table_name', con=engine, if_exists='append', index_label='id')

如果我从该表中读取所有记录:

gpd.read_postgis('table_name', con=engine, geom_col='geometry')

我收到以下警告:

SAWarning: Did not recognize type 'geometry' of column 'geometry' pandas_sql.meta.reflect(only=[sql])

如果我尝试执行 postgresql 语句,例如:

gpd.read_postgis('SELECT DISTINCT(st_cnty_fips) FROM table_name', con=self.engine, geom_col='geometry')

我收到一个错误:

line 65, in _df_to_geodf
    raise ValueError("Query missing geometry column '{}'".format(geom_col))
ValueError: Query missing geometry column 'geometry'

使用 PGAdmin4 我可以看到该列存在。我究竟做错了什么?

【问题讨论】:

    标签: postgis geopandas


    【解决方案1】:

    Geopandas 在额外过滤掉几何图形或添加几何图形后需要一个几何图形柱。

    参数 geom_col='geometry' 允许您指定 geopandas 需要的此列的名称。

    需要添加几何图形。

    例如:

    SELECT your_query_with_out_geom, table_name.geom FROM table_name;
    

    在您的情况下,可以:

    gpd.read_postgis('SELECT DISTINCT(st_cnty_fips), table_name.geometry FROM table_name', con=self.engine, geom_col='geometry')
    

    【讨论】:

      猜你喜欢
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      相关资源
      最近更新 更多