7.2 通用精确和模糊查询
#coding=utf8 import arcpy import os import sys import math def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count def getWorkspace(inFeature): desc = arcpy.Describe(inFeature) path=desc.path.lower() if path.endswith(".mdb") or path.endswith(".gdb"): return path if os.path.isdir(path):#shp return path outPath, outFC = os.path.split(path) return outPath def FieldExists(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return True break return False #获得字段长度 def getFieldlen(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field.length return -1 #获得字段类型是否为文本 def getFieldTypeIsText(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field.type=="String" return False def Main(): ## count=getCount(sFeature) ## if count < 1: ## arcpy.AddMessage(sFeature+u"没有数据") ## return desc = arcpy.Describe(sFeature) newFieldName=FieldName #arcpy.AddFieldDelimiters(sFeature, FieldName) #对应mdb有问题,不加 IsText=getFieldTypeIsText(sFeature, FieldName) dpath=getWorkspace(sFeature) match="%" if dpath.endswith(".mdb"): match="*" if IsText: #文本 if b: sql="{0} like '{1}{2}{1}'".format(newFieldName,match,myvalue) else: sql="{0} = '{1}'".format(newFieldName,myvalue) else: if b: if dpath.endswith(".mdb"): sql="str({0}) like '{1}{2}{1}'".format(newFieldName,match,myvalue) elif dpath.endswith(".gdb"): sql="CAST({0} AS varchar(20)) like '{1}{2}{1}'".format(newFieldName,match,myvalue) else:#shp sql="CAST({0} as character) like '{1}{2}{1}'".format(newFieldName,match,myvalue) else: sql="{0} = {1}".format(newFieldName,myvalue) if desc.dataType=="FeatureLayer": try: arcpy.SelectLayerByAttribute_management (sFeature, "NEW_SELECTION", sql) except Exception as e: arcpy.AddError(e) arcpy.AddMessage(u"语法"+sql+"错误".encode('gbk')) #汉字乱码的解决 if getCount(sFeature)<1: arcpy.AddWarning(u""+sql+"没有查询到记录") else: arcpy.AddMessage("不是图层") sFeature = arcpy.GetParameterAsText(0) FieldName = arcpy.GetParameterAsText(1) myvalue=arcpy.GetParameterAsText(2) b=arcpy.GetParameter(3) Main()
7.3.1 数据均分
#coding=utf8 import arcpy import os import sys import math def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count def getWorkspace(inFeature): desc = arcpy.Describe(inFeature) path=desc.path.lower() if path.endswith(".mdb") or path.endswith(".gdb"): return path if os.path.isdir(path):#shp return path outPath, outFC = os.path.split(path) return outPath def AddAllField(TableName,outFeature): desc = arcpy.Describe(TableName) plist = [] for field in desc.fields: if field.type!="Geometry" and field.type!="OID": plist.append(field.Name) arcpy.AddField_management(outFeature,field.Name ,field.type,field.precision,field.scale,field.length) return plist def getLayer(layername): layername=layername.upper() mxd = arcpy.mapping.MapDocument("CURRENT") try: for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name.upper()==layername: return lyr return None finally: del mxd def FieldExists(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return True break return False #获得字段类型是否为文本 def getFieldTypeIsText(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field.type=="String" return False def Main(): count=getCount(inFeature) if count < 1: arcpy.AddMessage(inFeature+u"没有数据") return outPath, outFC = os.path.split(inFeature) desc = arcpy.Describe(inFeature) isTable=desc.dataType=="TableView" if dividenum<2: if isTable==True: arcpy.TableSelect_analysis(inFeature, outworkspace+"/"+outFC) else: arcpy.Select_analysis(inFeature, outworkspace+"/"+outFC) return myFID="my_FID" if not FieldExists(inFeature,myFID): arcpy.AddField_management(inFeature,myFID , "LONG", "", "") k=1 with arcpy.da.UpdateCursor(inFeature, myFID) as cursor: for row in cursor: row[0]=k k=k+1 cursor.updateRow(row) num=getCount(inFeature) nn=num/dividenum initProgress(u"分割",dividenum) for i in range(0,dividenum): step() if i==dividenum-1: where=myFID+">"+str(i*nn)+" and "+myFID+"<="+str(num) else: where=myFID+">"+str(i*nn)+" and "+myFID+"<="+str((i+1)*nn) arcpy.AddMessage("where===="+where) if isTable==True: arcpy.TableSelect_analysis(inFeature, outworkspace+"/"+outFC+str(i),where) else: arcpy.Select_analysis(inFeature, outworkspace+"/"+outFC+str(i),where) freeProgress() inFeature = arcpy.GetParameterAsText(0) dividenum = arcpy.GetParameter(1) outworkspace = arcpy.GetParameterAsText(2) Main()
7.3.2 固定行数分割
#coding=utf8 import arcpy import os import sys import math def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count def getWorkspace(inFeature): desc = arcpy.Describe(inFeature) path=desc.path.lower() if path.endswith(".mdb") or path.endswith(".gdb"): return path if os.path.isdir(path):#shp return path outPath, outFC = os.path.split(path) return outPath def AddAllField(TableName,outFeature): desc = arcpy.Describe(TableName) plist = [] for field in desc.fields: if field.type!="Geometry" and field.type!="OID": plist.append(field.Name) arcpy.AddField_management(outFeature,field.Name ,field.type,field.precision,field.scale,field.length) return plist def getLayer(layername): layername=layername.upper() mxd = arcpy.mapping.MapDocument("CURRENT") try: for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name.upper()==layername: return lyr return None finally: del mxd def FieldExists(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return True break return False #获得字段类型是否为文本 def getFieldTypeIsText(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field.type=="String" return False def Main(): count=getCount(inFeature) if count < 1: arcpy.AddMessage(inFeature+u"没有数据") return outPath, outFC = os.path.split(inFeature) desc = arcpy.Describe(inFeature) isTable=desc.dataType=="TableView" num=getCount(inFeature) if num<rownum: if isTable==True: arcpy.TableSelect_analysis(inFeature, outworkspace+"/"+outFC) else: arcpy.Select_analysis(inFeature, outworkspace+"/"+outFC) return myFID="my_FID" if not FieldExists(inFeature,myFID): arcpy.AddField_management(inFeature,myFID , "LONG", "", "") k=1 with arcpy.da.UpdateCursor(inFeature, myFID) as cursor: for row in cursor: row[0]=k k=k+1 cursor.updateRow(row) nn=num/rownum if nn*rownum!=num: nn=nn+1 initProgress(u"分割",nn) for i in range(0,nn): step() if i==nn-1: where=myFID+">"+str(i*rownum)+" and "+myFID+"<="+str(num) else: where=myFID+">"+str(i*rownum)+" and "+myFID+"<="+str((i+1)*rownum) arcpy.AddMessage("where===="+where) if isTable==True: arcpy.TableSelect_analysis(inFeature, outworkspace+"/"+outFC+str(i),where) else: arcpy.Select_analysis(inFeature, outworkspace+"/"+outFC+str(i),where) freeProgress() inFeature = arcpy.GetParameterAsText(0) rownum = arcpy.GetParameter(1) outworkspace = arcpy.GetParameterAsText(2) Main()
7.4 属性查询-消除指定面积
#coding=utf8 import arcpy import os import sys import math def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count def FieldExists(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return True break return False def Main(): count=getCount(inFeature) if count < 1: arcpy.AddMessage(inFeature+u"没有数据") return desc=arcpy.Describe(inFeature) #清除选择对象 datatype = desc.datatype areaField="" #arcpy.AddMessage(u"areaField="+areaField) if datatype=='FeatureLayer':#清除选择 arcpy.SelectLayerByAttribute_management(inFeature, "CLEAR_SELECTION") arcpy.RefreshActiveView() areaField=desc.featureClass.areaFieldName #获得要素 else: areaField=desc.areaFieldName if areaField=="": areaField="YL_Area" if not FieldExists(inFeature,areaField): arcpy.AddField_management(inFeature,areaField , "DOUBLE") arcpy.CalculateField_management(inFeature,areaField,"!shape.area!","PYTHON") #计算面积 myselect="myselect" arcpy.MakeFeatureLayer_management(inFeature, myselect) count=getCount(myselect) if count<1: arcpy.AddWarning(u"没有小于指定面积") return arcpy.SelectLayerByAttribute_management(myselect, "NEW_SELECTION",areaField+"<"+str(minarea)) arcpy.Eliminate_management(myselect,outFeature,"LENGTH") num=getCount(outFeature) arcpy.AddMessage("消除"+str(count-num)+"个") inFeature = arcpy.GetParameterAsText(0) minarea=arcpy.GetParameter(1) outFeature = arcpy.GetParameterAsText(2) Main()
7.5 属性查询-按属性分割
#coding=utf8 import arcpy import os import sys import math def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count #获得字段类型是否为文本 def getFieldTypeIsText(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field.type=="String" return False def Main(): count=getCount(inFeature) if count < 1: arcpy.AddMessage(inFeature+u"没有数据") return jfb_Select=arcpy.env.scratchWorkspace+"/yl_999888"#不能c:要c:\或则 c:/ arcpy.Frequency_analysis(inFeature,jfb_Select,FieldName,summary_fields="#") num=getCount(jfb_Select) initProgress(u"导出数据",num) Fieldnum=len(Fields) Fieldtypes=[] for i in range(0,Fieldnum): if getFieldTypeIsText(jfb_Select,Fields[i]): Fieldtypes.append(1) #字符串 else: Fieldtypes.append(0) #不是字符串 desc = arcpy.Describe(inFeature) isTable=desc.dataType=="TableView" with arcpy.da.SearchCursor(jfb_Select, Fields) as cursor: for row in cursor: step() where="" Value="" for i in range(0,Fieldnum): if Fieldtypes[i]==1: where=where+Fields[i]+"='"+str(row[i])+"'" else: where=where+Fields[i]+"="+str(row[i]) Value=Value+str(row[i]) if i<Fieldnum-1: where=where+" and " Value=Value+"_" arcpy.AddMessage("Value==="+Value) arcpy.AddMessage("where==="+where) outFC = arcpy.ValidateTableName(Value,outworkspace) arcpy.AddMessage("outFC==="+outFC) if isTable==True: arcpy.TableSelect_analysis(inFeature, outworkspace+"/"+outFC, where) else: arcpy.Select_analysis(inFeature, outworkspace+"/"+outFC, where) freeProgress() inFeature = arcpy.GetParameterAsText(0) FieldName = arcpy.GetParameterAsText(1) outworkspace = arcpy.GetParameterAsText(2) Fields= FieldName.split(";") Main()
7.6 空间查询-获得一个省有多少的城市
#coding=utf8 import arcpy import os import sys import math def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count def getallcity(pgeo): yllayer="yllayer" arcpy.MakeFeatureLayer_management(cityFeature, yllayer) arcpy.SelectLayerByLocation_management(yllayer, 'intersect', pgeo) fields = [cityFieldName] with arcpy.da.SearchCursor(yllayer, fields) as cursor: mystr="" for row in cursor: mystr=mystr+row[0]+"," if mystr.endswith(","): return mystr[:-1] return mystr def Main(): count=getCount(sFeature) if count < 1: arcpy.AddMessage(sFeature+u"没有数据") return fields = ("SHAPE@", FieldName) initProgress(u"正在更新",count) with arcpy.da.UpdateCursor(sFeature, fields) as cursor: for row in cursor: step() pgeo=row[0] row[1]=getallcity(pgeo) cursor.updateRow(row) freeProgress() sFeature = arcpy.GetParameterAsText(0) FieldName = arcpy.GetParameterAsText(1) cityFeature=arcpy.GetParameterAsText(2) cityFieldName=arcpy.GetParameterAsText(3) Main()
7.7 空间查询-查询一个省相邻的省份
#coding=utf8 import arcpy import os import sys import math def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count #获得字段长度 def getFieldlen(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field.length return -1 #获得相邻省份 def getxlpr(pgeo,FID): yllayer="yllayer" where_clause=OIDFieldName+"<>"+str(FID) arcpy.MakeFeatureLayer_management(sFeature, yllayer,where_clause) arcpy.SelectLayerByLocation_management(yllayer, 'intersect', pgeo) fields = [FieldName] with arcpy.da.SearchCursor(yllayer, fields) as cursor: mystr="" for row in cursor: mystr=mystr+row[0]+"," if mystr.endswith(","): return mystr[:-1] return mystr def Main(): count=getCount(sFeature) if count < 1: arcpy.AddMessage(sFeature+u"没有数据") return Fieldlen=getFieldlen(sFeature,xlname) if Fieldlen<200: arcpy.AddMessage("Fieldlen="+str(Fieldlen)+",太短") return fields = ("SHAPE@", xlname,"OID@") initProgress(u"正在更新",count) with arcpy.da.UpdateCursor(sFeature, fields) as cursor: for row in cursor: step() pgeo=row[0] FID=row[2] row[1]=getxlpr(pgeo,FID) cursor.updateRow(row) freeProgress() sFeature = arcpy.GetParameterAsText(0) FieldName = arcpy.GetParameterAsText(1) xlname=arcpy.GetParameterAsText(2) #相邻 desc = arcpy.Describe(sFeature) OIDFieldName = desc.OIDFieldName Main()