我是 python 新手,我正在尝试更舒适地使用 python 自动化 GIS 任务。任何帮助表示赞赏
我有两个包含点的层,我试图在 python 中使用 Ogr 将它们合并为一个层。以下是我在网站上找到的代码,但它给了我一个错误
AttributeError:“NoneType”对象没有属性“GetLayer”
我认为导致此错误的行是:
ds = ogr.Open(目录+文件)
我想知道为什么在这一步没有生成任何东西,我还想知道是否有不同/更好的方法来使用 gdal/ogr python 合并图层
outputMergefn = 'Merge.shp'
directory = "C:/Users/Robin/Documents/Python Final Project/Final_Project/Output"
filestartswith = 'C'
FileEndsWith = '.shp'
drivername = 'ESRI Shapefile'
geometrytype = ogr.wkbMultiPoint
ptdriver = ogr.GetDriverByName('ESRI Shapefile')
if os.path.exists(outputMergefn):
ptdriver.DeleteDataSource(outputMergefn)
out_ds = ptdriver.CreateDataSource(outputMergefn)
out_layer = out_ds.CreateLayer(outputMergefn, geom_type = geometrytype)
filelist = os.listdir(directory)
for file in filelist:
if file.startswith(filestartswith) and file.endswith(FileEndsWith):
print file
ds = ogr.Open(directory + file)
if ds is None:
print "This is None"
lyr = ds.GetLayer()
for feat in lyr:
out_feat = ogr.Feature(out_layer.GetLayerDefn())
out_feat.SetGeometry(feat.GetGeometryRef().Clone())
out_layer.CreateFeature(out_feat)
out_layer.SyncToDisk()