Python 由于类型 'double' 的参数 2,脚本因坐标变量(纬度、经度)而失败

Python script fails with coordinate variables (lat, lon) due to argument 2 of type 'double'

我在 Python (Python 3!) 中使用 ogr 绘制点,如果我将我的坐标作为数字输入代码,它工作正常,但如果我尝试以下代码失败并使用 csv 文件中的坐标。

工作代码:

from osgeo import ogr

point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(40.729047, -73.957472)
print(point.ExportToWkt())

破坏的代码:

from osgeo import ogr

datatest = pd.read_csv('data.csv')

p1_lat, p1_lon = datatest['POINT1_LAT'], datatest['POINT1_LON']

point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(p1_lat, p1_lon)
print(point.ExportToWkt())

第二个示例失败并出现错误 TypeError: in method 'Geometry_AddPoint', argument 2 of type 'double'

我做错了什么,我该如何修复它以便代码可以从我的 csv 文件中调用坐标?谢谢。

编辑:根据要求,如果我打印我得到的两个变量:

0    40.729047
Name: POINT1_LAT, dtype: float64
0   -73.957472
Name: POINT1_LON, dtype: float64

EDIT2:这是 Python 3

您正在使用 AddPoint 而不是浮点数添加 pandas 数据帧。

import pandas as pd

pts = pd.DataFrame({'p': [3.4532], 'q' : [5.674]})

print(pts['p'])
print(pts['q'])

print(float(pts['p'][0]))
print(float(pts['q'][0]))
0    3.4532
Name: p, dtype: float64
0    5.674
Name: q, dtype: float64
3.4532
5.674

这应该有效:-

from osgeo import ogr

datatest = pd.read_csv('data.csv')

p1_lat, p1_lon = datatest['POINT1_LAT'], datatest['POINT1_LON']
p1_lat = float(p1_lat[0])
p1_lon = float(p1_lon[0])

point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(p1_lat, p1_lon)
print(point.ExportToWkt())