繁体   English   中英

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 方法后,它的“几何”值如下所示:

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

  2. 如果/当这可行时,我会将这些点与多边形 GeoDataFrame 连接起来,以便绘制两者(多边形顶部的点)。

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

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

提前致谢!

Geopandas API 得到了清理,现在可以正常工作了。 确保使用最新的稳定版本并阅读文档

使用 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

答案一直在这里

hg=hg.to_crs(c.crs)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM