给定以下 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")
...但没有运气。
一些重要说明:
应用 .to_crs 方法后,它的“几何”值如下所示:
POLYGON ((-5973.005380655156 3399.646267693398 ......当我用 hg GeoDataFrame 尝试上述操作时,它们看起来仍然像常规的纬度/经度坐标。
ValueError:无法转换原始几何图形。请先在对象上设置一个 crs。
提前致谢!
最佳答案
Geopandas API 得到了清理,现在可以正常工作了。确保使用最新的稳定版本并阅读 docs .
使用 EPSG 代码在 GeoDataFrame 上设置 CRS 非常简单
gdf.set_crs(epsg=4326, inplace=True)
哪里gdf
是 geopandas.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)
关于python-3.x - GeoPandas 在点上设置 CRS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961816/