目录
❓❓问题
解决方案
1.首先定位gpd的地方,肯定是矢量操作的问题
2.如果一个空的shp文件不能直接调用gpd.creatbuffer函数,不然会报错
整理不易,欢迎一键三连!!!
使用geopandas是报错std::bad_alloc,详细报错信息:
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
一步步debug打印发现是gdf = gpd.read_file(inShp_path)这一句报错,有时能正常使用,有时就不灵了,报错信息也看不出来是什么,后来打开这个矢量文件,原来这个矢量文件是个空的。
原来的错误代码(用geopandas写的creatbuffer函数):
#错误代码:
def createBuffer(inShp_path, outShp_path, bufferDist):
driver = ogr.GetDriverByName('ESRI Shapefile')
if os.path.exists(outShp_path):
driver.DeleteDataSource(outShp_path)
gdf = gpd.read_file(inShp_path)
new_gdf = gdf.buffer(bufferDist)
new_gdf.to_file(outShp_path)
修改后的正确代码(用gdal写的creatbuffer函数):
#正确代码:
def createBuffer(inShp_path, outShp_path, bufferDist):
inputds = ogr.Open(inShp_path)
inputlyr = inputds.GetLayer()
driver = ogr.GetDriverByName('ESRI Shapefile')
if os.path.exists(outShp_path):
driver.DeleteDataSource(outShp_path)
ds = driver.CreateDataSource(outShp_path)
layer = ds.CreateLayer(outShp_path, geom_type=ogr.wkbPolygon)
featureDefn = layer.GetLayerDefn()
for feature in inputlyr:
ingeom = feature.GetGeometryRef()
geomBuffer = ingeom.Buffer(bufferDist)
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(geomBuffer)
layer.CreateFeature(outFeature)
outFeature = None
总结:
报错std::bad_alloc大概率是geopandas的问题,因为python里的geopandas是用C++写的,报错内容也是C++,只需要找到使用geopandas的位置,替换个包使用就行了。
切记:空的矢量文件不能用geopandas来调用creatbuffer函数。要用gdal,还是gdal牛啊!!!
送你们一条美丽的--分割线--
⛵⛵⭐⭐