当我尝试在 Jupyter Notebook 中运行以下代码时出现此错误:
“NoneType”对象没有属性“ExportToWkt”
问题是什么?
这是我的代码:
shapefile = osgeo.ogr.Open("C:/Users/chavoshi/Anaconda3/Example Data/Remorquages/pointdata.shp")
layer = shapefile.GetLayer(0)
#First delete the existing contents of this table in case we want to run the code multiple times.
cursor.execute("DELETE FROM B_B")
for i in range(layer.GetFeatureCount()):
feature = layer.GetFeature(i)
lats_o = feature.GetField("latitude_o")
#print(lats_o)
lons_o = feature.GetField("longitude_")
#Get feature geometry
geometry = feature.GetGeometryRef()
#Convert geometry to WKT format
wkt = geometry.ExportToWkt()
#Insert data into database, converting WKT geometry to a PostGIS geography
cursor.execute("INSERT INTO B_B (lats, lons, outline) VALUES ({}, {}, ST_GeogFromText('{}'))".format(lats_o, lons_o, wkt))
connection.commit()