geopandas.GeoDataFrame.to_json#
- GeoDataFrame.to_json(na='null', show_bbox=False, drop_id=False, to_wgs84=False, **kwargs)[来源]#
返回
GeoDataFrame
的GeoJSON表示形式作为字符串。- Parameters:
- na{‘null’, ‘drop’, ‘keep’}, default ‘null’
指示如何在GeoDataFrame中输出缺失的(NaN)值。
见下文。- show_bboxbool, optional, default: False
在geojson中包含bbox(边界)
- drop_idbool, default: False
是否将GeoDataFrame的索引保留为生成的GeoJSON中的id属性。默认为False,但如果索引只是任意的行号,可以考虑设为True。
- to_wgs84: bool, optional, default: False
如果活动几何列上设置了CRS,它将被导出为WGS84 (EPSG:4326),以符合2016 GeoJSON规范。设置为True以强制重投影,设置为False以忽略CRS。默认为False。
另请参阅
GeoDataFrame.to_file
将 GeoDataFrame 写入文件
笔记
其余的 kwargs 将被传递给 json.dumps()。
GeoDataFrame中的缺失值(NaN)可以表示如下:
null
: 将缺失的条目输出为 JSON null。drop
: 从特征中移除属性。此操作适用于每个特征,因此特征可以具有不同的属性。keep
: 将缺失的条目输出为 NaN。
如果GeoDataFrame定义了CRS,其定义将包含在输出中,除非它等于WGS84(默认的GeoJSON CRS)或者无法在URN OGC格式中表示,或者除非指定了
to_wgs84=True
。示例
>>> from shapely.geometry import Point >>> d = {'col1': ['name1', 'name2'], 'geometry': [Point(1, 2), Point(2, 1)]} >>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:3857") >>> gdf col1 geometry 0 name1 POINT (1 2) 1 name2 POINT (2 1)
>>> gdf.to_json() '{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {"col1": "name1"}, "geometry": {"type": "Point", "coordinates": [1.0, 2.0]}}, {"id": "1", "type": "Feature", "properties": {"col1": "name2"}, "geometry": {"type": "Point", "coordinates": [2.0, 1.0]}}], "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::3857"}}}'
或者,您可以将GeoJSON写入文件:
>>> gdf.to_file(path, driver="GeoJSON")