读写文件#

读取空间数据#

地貌熊猫 使用命令::可以读取几乎任何基于矢量的空间数据格式,包括ESRI shapefile、GeoJSON文件等

geopandas.read_file()

它返回一个GeoDataFrame对象。这是可能的,因为 地貌熊猫 利用了伟大的 fiona 库,而该库又利用了一个名为 GDAL/OGR 旨在促进空间数据转换。

传递给 geopandas.read_file() 之后,该文件名将直接传递给 fiona.open() ,它执行实际的数据导入。总体而言, geopandas.read_file() 非常聪明,应该在没有额外参数的情况下执行您想要的操作,但要获得更多帮助,请键入::

import fiona; help(fiona.open)

除了其他功能外,还可以使用显式设置驱动程序(shapefile、GeoJSON driver 关键字,或从多层文件中选择一个层 layer 关键词::

countries_gdf = geopandas.read_file("package.gpkg", layer='countries')

目前,菲奥娜只公开默认的驱动程序。要显示这些内容,请键入::

import fiona; fiona.supported_drivers

有一个 array 未公开但受支持(取决于GDAL版本)的驱动程序。可以在运行时通过更新 supported_drivers 词典,如::

fiona.supported_drivers["NAS"] = "raw"

受支持的位置 fiona地貌熊猫 也可以直接从Web URL加载资源,例如来自的GeoJSON文件 geojson.xyz ::

url = "http://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_land.geojson"
df = geopandas.read_file(url)

您还可以加载包含数据的ZIP文件::

zipfile = "zip:///Users/name/Downloads/cb_2017_us_state_500k.zip"
states = geopandas.read_file(zipfile)

如果数据集位于ZIP文件的文件夹中,则必须附加其名称::

zipfile = "zip:///Users/name/Downloads/gadm36_AFG_shp.zip!data"

如果ZIP文件中的一个文件夹中有多个数据集,则还必须指定文件名::

zipfile = "zip:///Users/name/Downloads/gadm36_AFG_shp.zip!data/gadm36_AFG_1.shp"

还可以使用 os.read() 方法,如文件处理程序(例如,通过内置 open() 函数)或 StringIO ::

filename = "test.geojson"
file = open(filename)
df = geopandas.read_file(file)

类似文件的对象来自 fsspec 还可用于读取数据,允许该项目支持的存储后端和缓存的任意组合:

path = "simplecache::http://download.geofabrik.de/antarctica-latest-free.shp.zip"
with fsspec.open(path) as file:
    df = geopandas.read_file(file)

您还可以读取路径对象::

import pathlib
path_object = pathlib.path(filename)
df = geopandas.read_file(path_object)

读取数据的子集#

由于Geopandas由Fiona提供支持,而Fiona由GDAL提供支持,因此在加载更大的数据集时,您可以利用预过滤。这可以使用几何图形或边界框在地理空间上完成。您还可以筛选加载了切片的行。有关更多信息,请访问 geopandas.read_file()

几何体过滤器#

0.7.0 新版功能.

几何图形过滤器仅加载与几何图形相交的数据。

gdf_mask = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_lowres")
)
gdf = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_cities"),
    mask=gdf_mask[gdf_mask.continent=="Africa"],
)

边界框过滤器#

0.1.0 新版功能.

边界框过滤器仅加载与边界框相交的数据。

bbox = (
    1031051.7879884212, 224272.49231459625, 1047224.3104931959, 244317.30894023244
)
gdf = geopandas.read_file(
    geopandas.datasets.get_path("nybb"),
    bbox=bbox,
)

行过滤器#

0.7.0 新版功能.

使用整数(对于前n行)或切片对象筛选从文件加载的行。

gdf = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_lowres"),
    rows=10,
)
gdf = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_lowres"),
    rows=slice(10, 20),
)

字段/列筛选器#

从文件中加载字段的子集:

备注

需要Fiona 1.8+

gdf = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_lowres"),
    ignore_fields=["iso_a3", "gdp_md_est"],
)

跳过从文件加载几何体:

备注

需要Fiona 1.8+

备注

退货 pandas.DataFrame

pdf = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_lowres"),
    ignore_geometry=True,
)

写入空间数据#

GeoDataFrame可以使用 geopandas.GeoDataFrame.to_file() 方法。要查看受支持格式的完整列表,请键入 import fiona; fiona.supported_drivers

此外,还可以将GeoDataFrames上传到 PostGIS 数据库(从GeoPandas 0.8开始),使用 geopandas.GeoDataFrame.to_postgis() 方法。

备注

GeoDataFrame可以包含比大多数文件格式支持的更多的字段类型。例如,元组或列表可以很容易地存储在GeoDataFrame中,但将它们保存到例如GeoPackage或Shapefile中会引发ValueError。在保存到文件之前,需要将它们转换为选定驱动程序支持的格式。

正在写入Shapefile ::

countries_gdf.to_file("countries.shp")

正在写入GeoJSON ::

countries_gdf.to_file("countries.geojson", driver='GeoJSON')

正在写入GeoPackage ::

countries_gdf.to_file("package.gpkg", layer='countries', driver="GPKG")
cities_gdf.to_file("package.gpkg", layer='cities', driver="GPKG")

空间数据库#

地貌熊猫 还可以使用从PostGIS数据库获取数据 geopandas.read_postgis() 指挥部。

写信给PostGIS::

from sqlalchemy import create_engine
db_connection_url = "postgresql://myusername:mypassword@myhost:5432/mydatabase";
engine = create_engine(db_connection_url)
countries_gdf.to_postgis("countries_table", con=engine)

ApacheParquet和Feather文件格式#

0.8.0 新版功能.

GeoPandas支持写入和读取ApacheParquet和Feather文件格式。

Apache Parquet 是一种高效的柱状存储格式(源自Hadoop生态系统)。它是一种广泛使用的表格数据二进制文件格式。羽化文件格式是磁盘上的 Apache Arrow Memory Format,一种用于内存中分栏数据的开放标准。

这个 geopandas.read_parquet()geopandas.read_feather()GeoDataFrame.to_parquet()GeoDataFrame.to_feather() 方法实现了从GeoPandas到这些二进制文件格式的快速往返,同时保留了空间信息。

警告

这是对拼图文件支持和关联元数据的初始实现。这是追踪元数据规范的0.1.0版,网址为:https://github.com/geopandas/geo-arrow-spec

这个元数据规范还没有做出稳定性承诺。因此,我们还不建议在生产设置中使用此选项,除非您能够重写您的镶木或羽毛文件。