使用Arcpy导出矢量数据的各个部分

使用Arcpy导出矢量数据的各个部分


发布日期: 1970-01-01 更新日期: 2016-10-28 编辑:zhangxiang 浏览次数: 5362

标签:

摘要: 大家很多时候都有这个需求,将多边形图层中的每一个要素(如各个省、或县),批量单个导出。 代码如下所示,希望能有用。 # -*- coding:utf-8 -*- import arcpy import os def export_single_part(fea...

大家很多时候都有这个需求,将多边形图层中的每一个要素(如各个省、或县),批量单个导出。 代码如下所示,希望能有用。

# -*- coding:utf-8 -*-
import arcpy
import os
def export_single_part(feapath , outpath, field, prename=''):
    # TODO: export attribute .
    arcpy.env.workspace = outpath
    polys = arcpy.SearchCursor(feapath)
    for poly in polys:
        hao = str(poly.getValue(field))
        print(hao)
        feat = poly.shape
        a = 0
        outfea = prename + hao
        if arcpy.Exists(outfea):
            poly = polys.next()
            continue
        # clear_feature(os.path.join(outpath, outfea))
        # 构造输出的路径
        outdir = os.path.join(outpath, hao[:3], hao)
        if os.path.exists(outdir):
            pass
        else:
            os.makedirs(outdir)
        res_fea =  os.path.join(outdir, outfea + '.shp')
        arcpy.CreateFeatureclass_management(outdir , outfea + '.shp', "Polygon", feapath)
        cur = arcpy.InsertCursor(res_fea)
        lineArray = arcpy.CreateObject("Array")
        # pntout = gp.CreateObject("Point")  
        polyArray = feat.getPart(a)
        pnt = polyArray.next()
        while pnt:
            # print str(pnt.id) + "; " + str(pnt.x) + "; " + str(pnt.y)
            lineArray.add(pnt)
            pnt = polyArray.next()
        feat = cur.newRow()
        feat.shape = lineArray
        cur.insertRow(feat)   
        lineArray.removeAll()
        del polyArray
        del pnt
        del feat    
if __name__ == '__main__':
    outws = r'D:\cmi\w10'
    infea = r'D:\china25w_wgs84.gdb\w10fenfu_wgs84\fn_poly'
    fd = 'result'
    # prename = 'w10_'
    export_single_part(infea, outws, fd)

关注公众号
获取免费资源

随机推荐


Copyright © Since 2014. 开源地理空间基金会中文分会 吉ICP备05002032号

Powered by TorCMS

OSGeo 中国中心 邮件列表

问题讨论 : 要订阅或者退订列表,请点击 订阅

发言 : 请写信给: osgeo-china@lists.osgeo.org