GeoPandas 在点上设置 CRS

GeoPandas Set CRS on Points

给定以下 GeoDataFrame:

h=pd.DataFrame({'zip':[19152,19047],
               'Lat':[40.058841,40.202162],
               'Lon':[-75.042164,-74.924594]})
crs='none'
geometry = [Point(xy) for xy in zip(h.Lon, h.Lat)]
hg = GeoDataFrame(h, crs=crs, geometry=geometry)
hg

       Lat          Lon     zip     geometry
0   40.058841   -75.042164  19152   POINT (-75.042164 40.058841)
1   40.202162   -74.924594  19047   POINT (-74.924594 40.202162)

我需要像设置另一个 GeoDataFrame 那样设置 CRS(像这样):

c=c.to_crs("+init=epsg:3857 +ellps=GRS80 +datum=GGRS87 +units=mi +no_defs")

我试过这个:

crs={'init': 'epsg:3857'}

还有这个:

hg=hg.to_crs("+init=epsg:3857 +ellps=GRS80 +datum=GGRS87 +units=mi +no_defs")

...但运气不好。

一些重要说明:

  1. 上述 .to_crs 方法适用的另一个 GeoDataFrame 来自形状文件,几何列用于多边形,而不是点。 在应用 .to_crs 方法后,其 'geometry' 值如下所示:

    POLYGON ((-5973.005380655156 3399.646267693398... 当我使用 hg GeoDataFrame 尝试上述操作时,它们看起来仍然像常规 lat/long 坐标。

  2. If/when 这行得通,然后我会将这些点与多边形 GeoDataFrame 连接起来,以便绘制两者(多边形顶部的点)。

  3. 当我尝试在使用 .to_crs 方法之前首先连接 GeoDataFrames,然后同时在点和多边形行上使用该方法时,出现以下错误:

    ValueError:无法转换原始几何图形。请先在对象上设置一个crs。

提前致谢!

答案一直是here

hg=hg.to_crs(c.crs)

这会将 hg 的 crs 设置为 c 的 crs。

Geopandas API 得到清理,现在正常运行。确保使用最新的稳定版本并阅读 docs.

使用其 EPSG 代码在 GeoDataFrame 上设置 CRS 就像

一样简单
gdf.set_crs(epsg=4326, inplace=True)

其中 gdfgeopandas.geodataframe.GeoDataFrame。注意显式 inplace!

所以在上面的例子中它将是:

import pandas as pd
from shapely.geometry import Point
from geopandas import GeoDataFrame

df = pd.DataFrame({'zip':[19152,19047],
               'Lat':[40.058841,40.202162],
               'Lon':[-75.042164,-74.924594]})

geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)]
gdf = GeoDataFrame(df, geometry=geometry)

gdf.set_crs(epsg=4326, inplace=True)
# ^ comment out to get a "Cannot transform naive geometries" error below

# project to merkator
gdf.to_crs(epsg=3395)

     zip        Lat        Lon                          geometry
0  19152  40.058841 -75.042164  POINT (-8353655.485 4846992.030)
1  19047  40.202162 -74.924594  POINT (-8340567.652 4867777.107)

在 GeoPandas 中设置 CRS 的格式现在是

gdf.crs = "EPSG:4326"

早期格式已弃用

参考:https://geopandas.org/projections.html