1

我是 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()
4

1 回答 1

1

目录有问题。在您指定的目录中,不存在 shapefile。这就是GetLayer()函数给出错误的原因。因为根据目录,数据源应该是shapefile,但是由于目录错误,数据源不是shapefile(可能是文件夹或其他文件)。而当数据源不是 shapefile 时,则GetLayer()无法使用该函数。

于 2020-04-30T08:39:43.640 回答