python - 使用 python 验证一个 shapefile 是一个 shapefile (fiona, ogr)

标签 python python-2.7 gis shapely fiona

在 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!

最佳答案

有了 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 的只读支持。更新您的包,看看它是否有效。

关于python - 使用 python 验证一个 shapefile 是一个 shapefile (fiona, ogr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832301/

相关文章:

python - 为什么 pip 重新安装 install_requires 中列出的标准包

python - 如何找到数据框列内数组中的最大值?

python - ffmpeg 无法检测文件,而 os 可以

python - 为什么同时读取多个文件比顺序读取慢?

python - jinja2 - 如何在 if 语句中放置一个 block ?

java - Geotools 解决方案读取 EPSG3035 中的 shapefile 以在 WGS84 中获取长/纬度?

python - 解释一个python结构

python - 从 pandas 数据框中的多行中提取非 nan 值

mysql - 如何确定地球表面的哪些图像与任意给定图像重叠?

gis - RTree : Count points in the neighbourhoods within each point of another set of points