• GDAL Python 过滤Shape Polygon中的面积小于某个阈值的小图斑


    # -*- coding: utf-8 -*-
    # !/usr/bin/mgdal_env
    # @Time : 2023/9/6 9:36
    # @Author : Hexk
    # 过滤矢量文件中的面积小于某个阈值的小图斑
    
    from osgeo import ogr, gdal, osr
    import os
    
    
    def ShapeFiltratePitch(_input_path, _output_path, _area_threshold):
        """
        过滤POLYGON Shape中的细小图斑,根据面积来过滤。
        :param _input_path: 输入文件路径
        :param _output_path: 输出文件的路径,不包括shp名称
        :param _area_threshold: 设定过滤面积阈值,单位平方米
        :return: None
        """
        driver = ogr.GetDriverByName('ESRI SHAPEFILE')
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
        gdal.SetConfigOption("SHAPE_ENCODING", "UTF8")
    
        src_ds = driver.Open(_input_path)
        src_layer = src_ds.GetLayer(0)
        src_ref = src_layer.GetSpatialRef()
        src_geom_type = src_layer.GetGeomType()
    
        output_name = os.path.splitext(os.path.split(_input_path)[1])[0]
        output_filename = f'{output_name}_ClearPitch.shp'
        output_abs_path = os.path.join(_output_path, output_filename)
        if os.path.exists(_output_path):
            print('当前输出文件夹路径已经存在.')
        else:
            os.makedirs(_output_path)
    
        if os.path.exists(output_abs_path):
            driver.DeleteDataSource(output_abs_path)
        else:
            print('需要输出的shape文件并不存在.')
        output_ds = driver.CreateDataSource(output_abs_path)
        output_layer = output_ds.CreateLayer(output_filename, srs=src_ref, geom_type=src_geom_type)
    
    
        src_layer.ResetReading()
        i = 0
        print(f'源文件包含Feature个数:{src_layer.GetFeatureCount()}')
        while i < src_layer.GetFeatureCount():
            feature = src_layer.GetFeature(i)
            i += 1
            geometry = feature.GetGeometryRef()
            geometry_area = geometry.Area()
            if geometry_area >= _area_threshold:
                # src_layer.DeleteFeature(i)
                # print(f'删除第{i}个Feature'.center(20, '-'))
                output_layer.CreateFeature(feature)
        print(f'过滤斑块后文件包含Feature个数:{output_layer.GetFeatureCount()}')
        output_ds.Release()
        src_ds.Destroy()
    
    
    if __name__ == '__main__':
        input_path = r'E:\***\***.shp'
        output_path = r'E:\***\***'
        ShapeFiltratePitch(input_path, output_path,20000)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    在这里插入图片描述

  • 相关阅读:
    nginx离线安装
    7个新的ES2022 JavaScript 功能,你千万不要错过了
    【华为机试真题 JAVA】最长元音子串的长度-100
    [架构设计-1]:系统架构部门的主要角色
    oracle plsql如何debug触发器
    windows server 2016 ftp搭建详细教程
    JAVA计算机毕业设计颜如玉图书销售网站的设计与实现Mybatis+系统+数据库+调试部署
    网络运维的重要性
    华为机试 - 字符串筛选排序
    LC 239.滑动窗口最大值
  • 原文地址:https://blog.csdn.net/Asphy/article/details/132711174