赞
踩
代码:
- #coding:gbk
- #author:yangyong
- #会报错,不过不影响正常使用
- #cunMing表示村落名称,可用于定义查询,注意用%
- import arcpy,os,re
- import arcpy.mapping as mapping
- global cunMing
-
- import sys
- reload(sys)
- sys.setdefaultencoding("utf-8")
-
- inputMxdPath = arcpy.GetParameterAsText(0) #floder eg:O:\arcgis_directory\MXD\RHYXT
- outputMxdPath = arcpy.GetParameterAsText(1) #floader eg:eg:O:\arcgis_directory\MXD\RHYXT\print
- lyrName = arcpy.GetParameterAsText(2) #string eg: XZQ
- sqlSentence = arcpy.GetParameter(3) #string eg: [XZQMC] = "%(cunMing)s" (cunMing 会自动替换为具体的村名)
-
-
-
- for root,dirs,names in os.walk(r"%s"%inputMxdPath):
- for name in names:
- if name[-4:].lower() == ".mxd":
- a = str('%s')%name
- if u"镇" in name:
- b = re.split(u"镇",a)[1]
- c = re.split(u"村",b)[0]
- cunMing = c + "村"
- elif "街道" in name:
- b = re.split(u"街道", a)[1]
- c = re.split(u"村", b)[0]
- cunMing = c + "村"
- arcpy.AddMessage("%s"%cunMing + "has be Recognition")
- mxd = mapping.MapDocument(r"%s"%os.path.join(root,name))
- df = mapping.ListDataFrames(mxd,"")[0]
- lyrs = arcpy.mapping.ListLayers(mxd,"",df)
- for lyr in lyrs:
- if lyr.name == "%s"%lyrName:
- whereClause = str(sqlSentence)%{"cunMing" : cunMing}
- lyr.definitionQuery = whereClause
- newName = os.path.join(outputMxdPath,name)
- del cunMing
- del name,b,c
- mxd.saveACopy(r"%s"%newName)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。