ArcGIS将shp按照属性字段进行分割为多个polygon矢量

 将一个shp中所有的要素都导出为shp

流域.shp属性表:

复制代码
import arcpy
import os
from arcpy import env

shp ="D:/01RiverPro/01DATA/流域.shp" #要处理的shp文件路径
out_path="D:/01RiverPro/01DATA/子流域"#输出文件夹的路径

with arcpy.da.SearchCursor(shp, ["SHAPE@",'ID']) as cursor:
#SHAPE@指代单个要素,ID是一个字段,该字段也是我们想要作为每个polygon命名的值,也可以改为其他的字段
    for row in cursor:
        out_name=str(row[1])+'.shp'#输出文件名 确保均为字符串类型,注意命名是唯一的,这样就能将该shp数据中的所有要素都导出 
        arcpy.FeatureClassToFeatureClass_conversion (row[0],out_path,out_name)
复制代码

 注意:

row[0]代表的就是polygon要素

row[1]表示的是ID字段,最好确保该字段的唯一性

修改的时候,不需要该row[0] row[1] 只需要改‘ID’为其他即可

结果:

地图展示:

另外一种方式:

 参考:https://www.cnblogs.com/yhpan/p/13556106.html

复制代码
import re
import arcpy
from arcpy import env

def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|\,\.\ ]"  
    new_title = re.sub(rstr, "_", title)  
    return new_title


env.workspace = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test"

in_features = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test/三级河网.shp"

rows = arcpy.SearchCursor(in_features, fields="GRID_CODE")

#遍历每一行 对符合要求的polygon进行分割
for row in rows:
    selected_field_value = row.getValue("GRID_CODE")
    where_clause = '"GRID_CODE" =3'
    #outfilename = validateTitle(selected_field_value)
    out_feature_class = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test/main.shp"
    arcpy.Select_analysis(in_features, out_feature_class, where_clause)
    print(selected_field_value+' has done!!!')

#将 GRID_CODE字段作为名字 如果是“name” 则需改为%s
for row in rows:
    selected_field_value = row.getValue("GRID_CODE")
    where_clause = '"GRID_CODE" = %x'%(selected_field_value)
    #outfilename = validateTitle(selected_field_value)
    out_feature_class = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test/%x.shp"%(selected_field_value)
    arcpy.Select_analysis(in_features, out_feature_class, where_clause)
    #print(selected_field_value+' has done!!!')
复制代码

参考:

https://blog.csdn.net/qq_36196621/article/details/90171166

posted @   icydengyw  阅读(6044)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示