首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python验证shapefile是shapefile (fiona,ogr)

使用python验证shapefile是shapefile (fiona,ogr)
EN

Stack Overflow用户
提问于 2015-04-23 19:09:10
回答 2查看 1.9K关注 0票数 2

在Fiona1.5.0上(我很困惑为什么不同的文件(如.dbf和.gdb)没有打印我的“!”(这是我希望在文件不是.shp的任何时候)退出前的警告。

代码语言:javascript
复制
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会让我感到惊讶)-并且没有print语句:

代码语言:javascript
复制
>> fiona.errors.DriverError: unsupported driver: u'OpenFileGDB'

对于一个.dbf,我实际上得到了以下内容:

代码语言:javascript
复制
>> Verified that in_file is a Shapefile!
>> Verified that repair_file is a Shapefile!
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-03 01:51:31

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

代码语言:javascript
复制
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')

此外,对OpenFileGDB的只读支持最近被添加到fiona中。更新您的包,看看它是否有效。

票数 1
EN

Stack Overflow用户

发布于 2015-04-28 21:09:14

支持的驱动程序的fiona数量比由ogr支持的驱动程序的数量要低得多,甚至fiona也是ogr的包装器。

ESRI shapefile文件具有误导性,因为该格式包含一个具有公共文件名前缀的文件集合,存储在同一个目录中。有三个强制文件

  • .shp -形状格式;特征几何本身
  • .shx形状索引格式;特征几何的位置索引,允许快速地向前和向后查找
  • .dbf -属性格式.每个形状的柱状属性( dBase IV格式)

所以dbf是一个ESRI格式文件。

因为需要存在一个.shp文件,所以可以先测试该文件的.shp扩展名,然后使用fiona测试它是否是“exist”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29832301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档