我有一个线串几何图形,我想计算其相应的宽度为 1 公里的缓冲区。尽管设置了 CRS,我似乎无法返回正确的几何形状
我正在运行以下代码来定义地理数据框
import pandas as pd
from shapely import LineString
import geopandas as gpd
test = pd.DataFrame(
[["Test", LineString([(41.000, 18.583), (40.892, 18.952)])]],
columns=["Nome", "geometry"]
)
test = gpd.GeoDataFrame(test, crs='EPSG:3003', geometry="geometry")
crs 设置正确且单位为米
>>> test.crs
<Projected CRS: EPSG:3003>
Name: Monte Mario / Italy zone 1
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: Italy - onshore and offshore - west of 12°E.
- bounds: (5.93, 36.53, 12.0, 47.04)
Coordinate Operation:
- name: Italy zone 1
- method: Transverse Mercator
Datum: Monte Mario
- Ellipsoid: International 1924
- Prime Meridian: Greenwich
但是,当我尝试计算缓冲区时,我得到了这个
>>> test.buffer(1000)
POLYGON ((-918.845 -261.947, -941.757 -166.523...
如您所见,缓冲区显然是错误的,因为坐标甚至不是有效坐标(纬度是 -918.845!) 我究竟做错了什么?预先感谢
那是因为您将 EPSG:3003 视为地理 CRS。由于您的输入是 lat/lon,因此您应该使用 EPSG:4326
初始化
GeoDataFrame
,然后在制作 to_crs
之前使用 buffer
:
gdf = gpd.GeoDataFrame(test, geometry="geometry", crs=4326).to_crs(3003)
ax = gdf.to_crs(4326).plot(color="r")
gdf.buffer(1000).to_crs(4326).plot(ax=ax)
输出:
>>> test.buffer(1000).to_crs(4326)
0 POLYGON ((40.88408 18.94991, 40.88390 18.95066...
dtype: geometry