GeoPandas在点上设置CRS [英] GeoPandas Set CRS on Points
问题描述
给出以下GeoDataFrame:
Given the following 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(如下所示):
I need to set the CRS as I did with another GeoDataFrame (like this):
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")
...但是没有运气.
...but no luck.
一些重要说明:
-
上面的.to_crs方法适用的另一个GeoDataFrame来自形状文件,而geometry列用于多边形,而不是点. 应用.to_crs方法后,其几何"值如下所示:
The other GeoDataFrame for which the above .to_crs method worked was from a shape file and the geometry column was for polygons, not points. Its 'geometry' values looked like this after the .to_crs method was applied:
POLYGON((-5973.005380655156 3399.646267693398 ...当我尝试使用hg GeoDataFrame进行上述操作时,它们仍然看起来像常规的经/纬度坐标.
POLYGON ((-5973.005380655156 3399.646267693398... and when I try the above with the hg GeoDataFrame, they still look like regular lat/long coordinates.
如果/当可行时,我将这些点与多边形GeoDataFrame连接起来,以便同时绘制这两个点(点在多边形顶部).
If/when this works out, I'll then concatenate these points with the polygon GeoDataFrame in order to plot both (points on top of polygons).
当我尝试首先使用.to_crs方法连接GeoDataFrames时,然后同时在点和多边形行上使用该方法时,出现以下错误:
When I try concatenating the GeoDataFrames first before using the .to_crs method, and then I use the method on both the point and polygon rows at once, I get the following error:
ValueError:无法转换原始几何.请先在对象上设置crs.
ValueError: Cannot transform naive geometries. Please set a crs on the object first.
提前谢谢!
推荐答案
Geopandas API已被清理,现在可以正常使用了.确保使用最新的稳定版本并阅读文档.
Geopandas API got cleaned up, and now works without surprises. Make sure to use the lastest stable version and read the docs.
使用EPSG代码在GeoDataFrame上设置CRS很简单
Setting the CRS on a GeoDataFrame using its EPSG code is as simple as
gdf.set_crs(epsg=4326, inplace=True)
,其中gdf
是geopandas.geodataframe.GeoDataFrame
.注意显式的inplace
!
where gdf
is a geopandas.geodataframe.GeoDataFrame
. Watch out for the explicit inplace
!
因此在上面的示例中为:
So in the example above it would be:
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(h.Lon, h.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!