geopandas.GeoDataFrame.from_postgis#
- classmethod GeoDataFrame.from_postgis(sql, con, geom_col='geom', crs=None, index_col=None, coerce_float=True, parse_dates=None, params=None, chunksize=None)#
替代构造函数以创建
GeoDataFrame
来自包含WKB表示形式的几何图形列的SQL查询。- 参数
- sql字符串
- conSqlalChemy.Eng.Connection或SqlalChemy.Engine
- geom_col字符串,默认‘geom’
要转换为形状几何图形的列名
- crs可选
用于返回的GeoDataFrame的坐标参考系
- index_col字符串或字符串列表,可选,默认:无
要设置为索引的列(多索引)
- coerce_float布尔值,默认为True
尝试将非字符串、非数值对象(如decimal.Decimal)的值转换为浮点数,这对SQL结果集很有用
- parse_dates列表或词典,默认为无
要解析为日期的列名的列表。
判决的日期
{{column_name: format string}}
其中,在解析字符串时间的情况下,格式字符串与strftime兼容,或者在解析整数时间戳的情况下,格式字符串是(D、s、ns、ms、us)之一。判决的日期
{{column_name: arg dict}}
,其中arg dict对应于的关键字参数pandas.to_datetime()
。对于没有本机DATETIME支持的数据库(如SQLite)尤其有用。
- params列表、元组或字典、可选、默认无
要传递给Execute方法的参数列表。
- chunksizeInt,默认为无
如果指定,则返回迭代器,其中ChunkSize是要包括在每个块中的行数。
参见
geopandas.read_postgis
将PostGIS数据库读取到GeoDataFrame
示例
PostGIS
>>> from sqlalchemy import create_engine >>> db_connection_url = "postgresql://myusername:mypassword@myhost:5432/mydb" >>> con = create_engine(db_connection_url) >>> sql = "SELECT geom, highway FROM roads" >>> df = geopandas.GeoDataFrame.from_postgis(sql, con)
SpatiaLite
>>> sql = "SELECT ST_Binary(geom) AS geom, highway FROM roads" >>> df = geopandas.GeoDataFrame.from_postgis(sql, con)
建议从PostGIS中读取的方法是
geopandas.read_postgis()
:>>> df = geopandas.read_postgis(sql, con)