【问题标题】:using python to verify that a shapefile is a shapefile (fiona, ogr)使用 python 验证 shapefile 是否为 shapefile (fiona, ogr)
【发布时间】:2015-04-23 19:09:10
【问题描述】:

在 fiona 1.5.0 上(我很困惑为什么各种文件(例如 .dbf 和 .gdb)没有打印我的“不是 Shapefile!”(这是我希望文件不是 . shp) 退出前的警告。

import fiona
import sys

   def process_file(self, in_file, repair_file):
        with fiona.open(in_file, 'r', encoding='utf-8') as input:
            # check that the file type is a shapefile
            if input.driver == 'ESRI Shapefile':
                print "in_file is a Shapefile!"
            else:
                print "NOT a Shapefile!"
                exit()
            with fiona.open(repair_file, 'r') as repair:
                # check that the file type is a shapefile
                if repair.driver == 'ESRI Shapefile':
                    print "Verified that repair_file is a Shapefile!"
                else:
                    print "NOT a Shapefile!"
                    exit()

对于 gdb,我收到 fiona 不支持驱动程序的错误(因为 ogr 确实让我感到惊讶)-并且没有打印语句:

>> fiona.errors.DriverError: unsupported driver: u'OpenFileGDB'

对于 .dbf,我实际上得到了这个:

>> Verified that in_file is a Shapefile!
>> Verified that repair_file is a Shapefile!

【问题讨论】:

    标签: python python-2.7 gis shapely fiona


    【解决方案1】:

    使用 OGR,ESRI Shapefile 驱动程序读取 DBF 文件。要检查数据源是否只有属性而没有几何(即只有一个 DBF 文件),请检查元数据中的几何类型以查看它是否为 'None'

    import fiona
    with fiona.open(file_name) as ds:
        geom_type = ds.meta['schema']['geometry']
        print('geometry type: ' + geom_type)
        if geom_type == 'None':
            print('no geometry column, so probably just a DBF file')
    

    此外,最近向 fiona 添加了对 OpenFileGDB 的只读支持。更新你的包,看看它是否有效。

    【讨论】:

      【解决方案2】:

      fiona 支持的驱动程序数量远低于 ogr 支持的驱动程序数量,即使 fiona 是 ogr 的包装器。

      ESRI shapefile 文件具有误导性,因为该格式由存储在同一目录中的具有通用文件名前缀的文件集合组成。有三个强制性文件

      • .shp — 形状格式;要素几何本身
      • .shx — 形状索引格式;特征几何的位置索引,允许快速向前和向后搜索
      • .dbf — 属性格式;每个形状的列属性,采用 dBase IV 格式

      所以 dbf 是一个 ESRI shapefile。

      因为要求存在一个 .shp 文件,所以您可以先测试该文件是否具有 .shp 扩展名,然后您可以使用 fiona 测试它是否为 `ESRI shapefile'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 2021-05-14
        • 1970-01-01
        • 2018-11-29
        • 2010-09-25
        • 2014-07-09
        相关资源
        最近更新 更多