首页 > 解决方案 > GeoPandas 中坐标表示的不一致

问题描述

我有 GeoSeries 中的形状点列表。

coords.head():

0    POINT (-26.17690 80.81700)
1    POINT (-15.54390 80.61700)
2    POINT (-20.67690 80.36700)
3      POINT (6.10610 80.83300)
4     POINT (17.63910 79.88300)
Name: geometry, dtype: geometry

当尝试使用命令获取压缩坐标列表时,pd.Series(zip(coords.geometry.x, coords.geometry.y)).head()我得到下一个示例:

0              (-26.1769, 80.817)
1              (-15.5439, 80.617)
2              (-20.6769, 80.367)
3    (6.1061000000000005, 80.833)
4     (17.63909999999999, 79.883)
dtype: object

顺便说一句coords.geometry.x.head()

0   -26.1769
1   -15.5439
2   -20.6769
3     6.1061
4    17.6391
dtype: float64

同样奇怪的是,当我尝试重现结果时:

new_coords = [(-26.17690, 80.81700),
(-15.54390, 80.61700),
(-20.67690, 80.36700),
(6.10610, 80.83300),
(17.63910, 79.88300)]
new_coords = gpd.GeoSeries([Point(p) for p in new_coords])
pd.Series(zip(new_coords.geometry.x, new_coords.geometry.y))
new_coords

Zip 的行为并不奇怪:

0    POINT (-26.17690 80.81700)
1    POINT (-15.54390 80.61700)
2    POINT (-20.67690 80.36700)
3      POINT (6.10610 80.83300)
4     POINT (17.63910 79.88300)
dtype: geometry

这里的主要目标是获得准确的坐标值以合并数据帧,因此 zip 返回相似但不相同的值是不可接受的。

标签: pythonpandasfloating-pointgeopandasshapely

解决方案


似乎@juanpa.arrivillaga 是正确的。执行语句时[float(p) for p in coords.geometry.x][:5],我得到相同的数字:

[-26.1769, -15.5439, -20.6769, 6.1061000000000005, 17.63909999999999]

所以输入数据不会被 zip 改变。


推荐阅读