geopandas.GeoDataFrame.set_crs#
- GeoDataFrame.set_crs(crs=None, epsg=None, inplace=False, allow_override=False)#
设置的坐标系(CRS)
GeoDataFrame
。如果GeoDataFrame中有多个几何图形列,则仅设置活动几何图形列的CRS。
注意:底层几何图形不会转换为此CRS。若要将几何图形转换为新的CRS,请使用
to_crs
方法。- 参数
- crs :pyproj.crs,如果是可选的 epsg 是指定的Pyproj.crs,可选,如果
该值可以是接受的任何值
pyproj.CRS.from_user_input()
例如,授权字符串(例如“EPSG:4326”)或WKT字符串。- epsg :int,在以下情况下可选 crs 是指定的Int,如果是可选的
指定投影的EPSG代码。
- inplace布尔值,默认为False
如果为True,则将就地更改GeoDataFrame的CRS(同时仍返回结果),而不是复制GeoDataFrame。
- allow_override布尔值,默认为False
如果GeoDataFrame已有CRS,则允许替换现有CRS,即使两者不相等。
参见
GeoDataFrame.to_crs
重新投影到另一个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.00000 2.00000) 1 name2 POINT (2.00000 1.00000)
将CRS设置为不带它的GeoDataFrame:
>>> 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
覆盖现有的CR:
>>> gdf = gdf.set_crs(4326, allow_override=True)
没有
allow_override=True
,set_crs
如果尝试覆盖CRS,则返回错误。