0
点赞
收藏
分享

微信扫一扫

利用geopandas与PostGIS进行交互

飞鸟不急 2022-08-20 阅读 97

文章授权转载,作者:费弗里 ,首发:Python大数据分析

本文完整代码及数据已上传至我的 Github 仓库 https:// github.com/CNFeffery/Da taScienceStudyNotes

1 简介

PostGIS 作为 postgresql 针对 地理空间数据 的拓展功能,可以帮助我们有效管理和固化空间矢量数据,以及开展空间数据分析,而 geopandas 作为 Python 生态中优秀的空间数据分析处理工具,自然在与 PostGIS 进行交互方面开发了相应的功能。

本文就将针对如何利用 geopandas 向 PostGIS 空间数据库写入及读取矢量数据进行介绍。

利用geopandas与PostGIS进行交互

图1

2 geopandas与PostGIS进行交互

为了能在 geopandas 中与 postgresql 和 PostGIS 建立连接,请确保以下3个库已经安装:

<pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">pip install sqlalchemy psycopg2 geoalchemy2</pre>

接下来我们需要保证 postgresql 中存在可以连接的空间数据库,在 pgAdmin 界面内新建数据库,譬如这里我们新建数据库 demo :

利用geopandas与PostGIS进行交互

图2

利用geopandas与PostGIS进行交互

图3

点击保存成功创建数据库之后,要注意这时我们的 demo 数据库只是个普通的 postgresql 数据库,是不支持空间相关功能的,在对应数据库上右键打开查询工具:

利用geopandas与PostGIS进行交互

图4

在弹出的界面中输入 CREATE EXTENSION postgis; 并执行,成功之后我们的数据库就变成了空间数据库,支持空间相关的各种功能:

利用geopandas与PostGIS进行交互

图5

至此我们的准备工作就已结束,接下来我们就可以直接在 geopandas 中读写 PostGIS 数据表。

2.1 利用geopandas向PostGIS写入数据

为方便演示,这里我们以简化版的重庆市区县矢量面数据为例,首先我们导入所需的 geopandas (注意 geopandas 版本必须大于等于0.8.0)与 sqlalchemy (后者用于创建数据库连接),并读入 重庆市.geojson 文件,你可以在开头的 Github 仓库找到它:

利用geopandas与PostGIS进行交互

图6

接着我们来演示如何通过 geopandas 向 PostGIS 推送矢量信息表,使用到的API为 to_postgis() ,其主要参数如下:

name :字符型,用于指定推送到 PostGIS 后的表名称

con : sqlalchemy.engine.Engine 对象,用于建立与数据库的连接

if_exists :字符型,用于指定当数据库中已存在同名表时的相应策略, 'fail' 表示抛出错误, 'replace' 指替换, 'append' 指向原表追加,默认为 fail

schema :字符型,用于指定 schema ,默认为 'public'

index :bool型,用于指定是否保留index信息

index_label :字符型或序列,当 index 被设置为True时为index信息指定字段名称

首先需要利用 sqlalchemy 中的 create_engine 来创建数据库连接,传入字符串包含了 数据库类型 、 用户名 、 密码 、 主机IP 、 端口 以及 数据库名称 ,格式为:

<pre class="hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">数据库类型://用户名:密码@主机IP:端口/数据库名称</pre>

对应本例:

利用geopandas与PostGIS进行交互

图7

在 pgAdmin 中随即就能查看到刚才写入的数据表:

利用geopandas与PostGIS进行交互

图8

利用geopandas与PostGIS进行交互

图9

2.2 利用geopandas从PostGIS读取数据

从 PostGIS 中读取数据要用到另一个API,对应 geopandas 的 read_postgis() ,其主要参数如下:

sql :字符型,对应从空间数据库中提取数据的SQL语句

con :同 to_postgis()

geom_col :字符型,用于指定将哪一列作为 GeoDataFrame 的矢量列

crs :用于指定坐标参考系,同 GeoDataFrame 的坐标参考系设定方式

index_col :字符型或列表,用于指定将哪些列作为索引

parse_dates :列表,用于预解析时间类型数据

接着我们从 PostGIS 中读取刚才写入的表:

利用geopandas与PostGIS进行交互

图10

简简单单,我们就实现了与 PostGIS 的交互。

以上就是本文的全部内容,如有疑问欢迎在评论区与我讨论~

举报

相关推荐

0 条评论