[Arcpy] 按属性选择并生成shp文件

完整Demo:https://blog.csdn.net/summer_dew/article/details/80712591

功能:按属性条件查询出要素–>将要素生成shp文件–>将shp文件打包成zip–>返回zip的全路径
涉及内容:
1. Arcpy读取 输入的参数
1. Arcpy按属性选择要素
2. Python生成不同名字的文件
2. Arcpy将要素输出成shp文件
3. Python将shp文件打包成zip
4. Arcpy设置输出参数

制作成GP模型:https://blog.csdn.net/summer_dew/article/details/80713666

# coding:utf8
# Author:PasserQi
# Time:2018/6/15
# Vesrion:0.2.1
# Param:
#   - input
#       - input_path:the feature class will be selected
#       - out_dir:the output dir of the zip file
#       - sql:query condition
#   - output
#       - out_path:the out path of the zip file
# Decs:
#   1. Select elements by attribute
#   2. Generates the SHP file from the selected element in the specified directory
#   3. Package the SHP file to generate the zip file
#   4. Reture the path to the zip file
import arcpy
import os
import sys
import zipfile

# Generate file names without repetition
# Maximum 100 files
def getFileName(inputfilepath,outdir):
    ret_filename = None

    inputname = os.path.basename(input_path)
    if '.' in inputname: #Get the prefix
        inputname = inputname.split('.')[0]

    # Find name without repetition between inputname0 and inputname99
    for cnt in range(0,100):
        outname = inputname + str(cnt) #Get outname
        outpath = os.path.join(outdir, outname + ".zip")
        if os.path.exists(outpath): #If this filename exists
            continue
        else: #This filename don't exist
            ret_filename = outname
    if not ret_filename:
        print 'More than 100 documents have been created.Created faild'
    return ret_filename

# Package the SHP file to generate the zip file
def shpFilesToZips(outpath):
    shpname = os.path.basename(outpath)
    shppath = os.path.dirname(outpath)

    name = shpname.replace(".shp","")
    zippath = os.path.join(shppath, name + ".zip")
    # Get the files will be packaged
    files =[]
    # package
    pre_len = len(os.path.dirname(shppath))
    zipf = zipfile.ZipFile(zippath, 'w')
    for parent, dirnames, filenames in os.walk(shppath):
        for filename in filenames:
            if '.zip' in filename:
                continue
            if name in filename:
                pathfile = os.path.join(parent, filename)
                arcname = pathfile.replace(shppath, '')
                zipf.write(pathfile, arcname)
                files.append(pathfile)
    zipf.close()

    print files

    # delete files
    for file in files:
        if os.path.exists(file):
            os.remove(file)

    return zippath


if __name__ == '__main__':
    input_path = sys.argv[1]
    out_dir = sys.argv[2]
    sql = sys.argv[3]

    # Make a layer from the feature class
    arcpy.MakeFeatureLayer_management(input_path, "lyr") 

    # Within selected features, further select only those cities which have a population > 10,000   
    arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", sql)

    # Generate file names
    input_name = getFileName(input_path, out_dir)
    if not input_name:
        arcpy.SetParameter(3, 'None')
        sys.exit()
    out_name = input_name + '.shp'
    out_path = os.path.join(out_dir, out_name)

    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("lyr", out_path)

    # Zip files
    zip_path = shpFilesToZips(out_path)


    arcpy.SetParameter(3, zip_path)

猜你喜欢

转载自blog.csdn.net/summer_dew/article/details/80712643