Python 3.x GeoPandas在点上设置CRS
给定以下GeoDataFrame:Python 3.x GeoPandas在点上设置CRS,python-3.x,geopandas,Python 3.x,Geopandas,给定以下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=geo
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)
我需要设置CRS,就像我对另一个GeoDataFrame所做的那样(如下所示):
我试过这个:
crs={'init': 'epsg:3857'}
这是:
hg=hg.to_crs("+init=epsg:3857 +ellps=GRS80 +datum=GGRS87 +units=mi +no_defs")
……但运气不好
一些重要注意事项:
hg=hg.to_crs(c.crs)
这将hg的crs设置为c。Geopandas API已清理完毕,现在可以正常工作。请确保使用最新的稳定版本并阅读 使用EPSG代码在GeoDataFrame上设置CRS非常简单
gdf.set_crs(epsg=4326, inplace=True)
其中,gdf
是一个geopandas.geodataframe.geodataframe
。注意显式就地
因此,在上面的示例中,它将是:
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”
不推荐使用早期格式
ref:答案也是错误的。to_crs
从一个crs转换到另一个crs,而.crs={…}
设置初始crs。@ufo你说“还”,你是说这个线程中的所有答案都不正确吗?@baxx我的另一条评论被删除,因为答案中的链接已修复。评论是“链接已死亡”.那么,现在“也”没有意义,但我无法编辑评论。如果您查看“目前正在工作”链接,您将看到to_crs
用于转换坐标,而不是设置crs。在这种情况下,最好建议并编辑为已接受的答案,或者至少留下评论。我已更新了答案,更改应继续进行有人可以查看它们。GeoDataFrame(df,geometry=geometry,crs='EPSG:4326')
work@JanPisl,它在最新稳定版本的geopandas(0.8.2
)上运行。您所说的“不再有效”是什么意思?您使用的是什么版本的python和geopandas?好的,对不起,我的错误!我使用0.7。让我自己更正一下:如果您得到***AttributeError:“GeoDataFrame”对象没有属性“set_crs”,请尝试从shyam获得答案
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)