今天分享两个python应用小代码,一个是根据字段来编号,字段相同的按从1到XXX进行编号(虽然这功能用FME只需要一个转换器,但还是有想在Arcgis上面进行实现)。代码如下: import arcpy def ifdir(dir,key): if dir.get(key) is None: dir[key]=1 return 1 else: i=dir.get(key)+1 dir1={key:i} dir.update(dir1) return i
def update(path,sourstr,deststr): dir={} cur=arcpy.UpdateCursor(path) for row in cur: str1=row.getValue(sourstr) number=ifdir(dir,str1) row.setValue(deststr,number) cur.updateRow(row) del row,cur path=r"e:\lianxi\bb\line.shp" //文件所在位置 file1="bb" //编号所区分的字段 fiel2="dd" //编号要放的字段 update(path,file1,fiel2) 第二个小代码为批量修改gdb文件中某个字段的值,代码如下: import os import arcpy from arcpy import env def update(path): env.workspace=path for feature in arcpy.ListDatasets('',"Feature"): for featureClass in arcpy.ListFeatureClasses('',"All",feature): path1=os.path.join(path,feature) path2=os.path.join(path1,featureClass) cur=arcpy.UpdateCursor(path2) for row in cur: if row.getValue("TAG") is None: //判断TAG字段中的值是否为空 row.setValue("TAG",2) cur.updateRow(row) del row,cur def ergodic(path): list1=os.listdir(path) for path1 in list1: if os.path.isdir(os.path.join(path,path1)): list2=path1.split(".") if len(list2)==2: update(os.path.join(path,path1)) def main1(): path=arcpy.GetParameterAsText(0) ergodic(path) print "sucess" main1() 同时第二小代码同时也适用shp格式数据,修改部分代码即可。
可以关注本人公众号: 同时也可以加我微信进行交流:
|