geopandas.GeoDataFrame.set_crs#

GeoDataFrame.set_crs(crs=None, epsg=None, inplace=False, allow_override=False)[来源]#

设置GeoDataFrame的坐标参考系统(CRS)。

如果GeoDataFrame中有多个几何列,则仅设置活动几何列的CRS。

传递 None 以从活动几何列中移除 CRS。

Parameters:
crspyproj.CRS | None, optional

值可以是任何被 pyproj.CRS.from_user_input() 接受的内容,例如一个授权字符串(例如“EPSG:4326”)或一个WKT字符串。

epsgint, optional

指定投影的 EPSG 代码。

inplacebool, default False

如果为真,GeoDataFrame 的 CRS 将在原地更改 (同时仍返回结果),而不是创建 GeoDataFrame 的副本。

allow_overridebool, default False

如果GeoDataFrame已经有一个CRS,允许替换现有的CRS,即使它们不相等。

另请参阅

GeoDataFrame.to_crs

重新投影到另一个坐标参考系统

笔记

底层几何图形没有转换为这个坐标参考系统(CRS)。要将几何图形转换为新的坐标参考系统,请使用 to_crs 方法。

示例

>>> from shapely.geometry import Point
>>> d = {'col1': ['name1', 'name2'], 'geometry': [Point(1, 2), Point(2, 1)]}
>>> gdf = geopandas.GeoDataFrame(d)
>>> gdf
    col1     geometry
0  name1  POINT (1 2)
1  name2  POINT (2 1)

为没有坐标参考系统的GeoDataFrame设置CRS:

>>> gdf.crs is None
True
>>> gdf = gdf.set_crs('epsg:3857')
>>> gdf.crs  
<Projected CRS: EPSG:3857>
Name: WGS 84 / Pseudo-Mercator
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: World - 85°S to 85°N
- bounds: (-180.0, -85.06, 180.0, 85.06)
Coordinate Operation:
- name: Popular Visualisation Pseudo-Mercator
- method: Popular Visualisation Pseudo Mercator
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

覆盖现有的坐标参考系统:

>>> gdf = gdf.set_crs(4326, allow_override=True)

如果没有 allow_override=True,那么在您尝试覆盖 CRS 时, set_crs 将返回错误。