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

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

功能:按属性条件查询出要素–>将要素生成shp文件–>将shp文件打包成zip–>返回zip的全路径
涉及内容:

  1. Arcpy读取 输入的参数
  2. Arcpy按属性选择要素
  3. Python生成文件(文件名注意不能重复)
  4. Arcpy将要素输出成shp文件
  5. Python将shp文件打包成zip
  6. 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)
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geodoer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值