在{a1}中,它说
A
GeoDataFrame
may also contain other columns with geometrical (shapely) objects, but only one column can be the active geometry at a time. To change which column is the active geometry column, use theset_geometry
method.
如果目标是灵活地将这些不同列中的几何数据重新投影到一个或多个其他坐标参考系,我想知道如何使用这样的地理数据框。这是我试过的
import geopandas as gpd
from shapely.geometry import Point
crs_lonlat = 'epsg:4326' #geometries entered in this crs (lon, lat in degrees)
crs_new = 'epsg:3395' #geometries needed in (among others) this crs
gdf = gpd.GeoDataFrame(crs=crs_lonlat)
gdf['geom1'] = [Point(9,53), Point(9,54)]
gdf['geom2'] = [Point(8,63), Point(8,64)]
#Working: setting geometry and reprojecting for first time.
gdf = gdf.set_geometry('geom1')
gdf = gdf.to_crs(crs_new) #geom1 is reprojected to crs_new, geom2 still in crs_lonlat
gdf
Out:
geom1 geom2
0 POINT (1001875.417 6948849.385) POINT (8 63)
1 POINT (1001875.417 7135562.568) POINT (8 64)
gdf.crs
Out: 'epsg:3395'
到目前为止,一切顺利。如果我想将geom2
设置为geometry列,并重新投影该列,那么情况就不正常了:
#Not working: setting geometry and reprojecting for second time.
gdf = gdf.set_geometry('geom2') #still in crs_lonlat...
gdf.crs #...but this still says crs_new...
Out: 'epsg:3395'
gdf = gdf.to_crs(crs_new) #...so this doesn't do anything! (geom2 unchanged)
gdf
Out:
geom1 geom2
0 POINT (1001875.417 6948849.385) POINT (8.00000 63.00000)
1 POINT (1001875.417 7135562.568) POINT (8.00000 64.00000)
好的,显然,当更改用作几何体的列时,gdf
的.crs
属性没有重置为其原始值-似乎没有为各个列存储crs。如果是这种情况,我认为对这个数据帧使用重投影的唯一方法是回溯:start-->;选择柱作为几何图形-->;将gdf重新投影到crs_new-->;使用/可视化/--&燃气轮机;将gdf重新投影回crs_lonlat-->;开始吧。如果我想在一个图中同时显示两列,这是不可用的
我的第二次尝试是,通过将上面脚本中的相应行更改为:
gdf = gpd.GeoDataFrame()
gdf['geom1'] = gpd.GeoSeries([Point(9,53), Point(9,54)], crs=crs_lonlat)
gdf['geom2'] = gpd.GeoSeries([Point(8,63), Point(8,64)], crs=crs_lonlat)
然而,很快就清楚了,虽然初始化为GeoSeries
,但这些列是正常的pandas
{GeoSeries
相同的.crs
属性:
gdf['geom1'].crs
AttributeError: 'Series' object has no attribute 'crs'
s = gpd.GeoSeries([Point(9,53), Point(9,54)], crs=crs_lonlat)
s.crs
Out: 'epsg:4326'
这里有我遗漏的东西吗?
唯一的解决方案是,事先决定“最终”crs,并在添加列之前进行所有重投影吗?就像这样
gdf = gpd.GeoDataFrame(crs=crs_new)
gdf['geom1'] = gpd.GeoSeries([Point(9,53), Point(9,54)], crs=crs_lonlat).to_crs(crs_new)
gdf['geom2'] = gpd.GeoSeries([Point(8,63), Point(8,64)], crs=crs_lonlat).to_crs(crs_new)
#no more reprojecting done/necessary/possible! :/
…然后,当需要另一个crs时,从头开始重建整个gdf
?这不可能是本打算使用的方式
不幸的是,目前不可能达到预期的行为。由于包中的限制,
geopandas
目前不适合此用例,如this issue in the github repo中所示我的解决方法是根本不使用}与形状几何体数据的几个单独的}组合起来。每个
GeoDataFrame
,而是将非形状数据的正常pandas
{geopandas
{GeoSeries
都有自己的crs,并且可以在必要时正确地重新投影相关问题 更多 >
编程相关推荐