GavinGuan 2012-07-06
在用ArcMap制图时,某些标注过长,地图显示上不太好看,而Maplex扩展可以将标注分行显示,但是首先要将标注字符串用符号分隔。下面的代码是用Python根据一定的规则分隔字符串。这个规则最好的情况下是要有个语义字典库,这样才能保证字符串分割比较符合语言习惯。#coding=gbkimportsys,string,os,arcgisscriptinggp=arcgisscripting.create()defsubString(str,fromindex,toindex):returnstr.decode('gbk')[fromindex:toindex].encode('gbk')#pfeatureclass="D:\\Data\\ChinaAdmin\\entern.shp"#srcfield="shanghai_4"#destfield="Form_Name"pfeatureclass=gp.getparameterastext(0)srcfield=gp.getparameterastext(1)destfield=gp.getparameterastext(2)featurecur=gp.updatecursor(pfeatureclass,"")featurerow=featurecur.next()whilefeaturerow:name_cn=featurerow.getvalue(srcfield)#printname_cnunicodenamelen=len(name_cn.decode('gbk'))asciinamelen=len(name_cn)#如果字符串中含有非中文,不处理ifunicodenamelen*2!=asciinamelen:result=name_cnelse:ifunicodenamelen==6:result=subString(name_cn,0,3)+"|"+subString(name_cn,3,6)elifunicodenamelen==7:result=subString(name_cn,0,4)+"|"+subString(name_cn,4,7)elifunicodenamelen==8:result=subString(name_cn,0,4)+"|"+subString(name_cn,4,elifunicodenamelen==9:result=subString(name_cn,0,5)+"|"+subString(name_cn,5,9)elifunicodenamelen==10:result=subString(name_cn,0,5)+"|"+subString(name_cn,5,10)elifunicodenamelen==11:result=subString(name_cn,0,6)+"|"+subString(name_cn,6,11)elifunicodenamelen==12:result=subString(name_cn,0,6)+"|"+subString(name_cn,6,12)elifunicodenamelen==13:result=subString(name_cn,0,5)+"|"+subString(name_cn,5,10)+"|"+subString(name_cn,10,13)elifunicodenamelen==14:result=subString(name_cn,0,5)+"|"+subString(name_cn,5,10)+"|"+subString(name_cn,10,14)elifunicodenamelen==15:result=subString(name_cn,0,5)+"|"+subString(name_cn,5,10)+"|"+subString(name_cn,10,15)elifunicodenamelen==16:result=subString(name_cn,0,6)+"|"+subString(name_cn,6,11)+"|"+subString(name_cn,11,16)elifunicodenamelen==17:result=subString(name_cn,0,6)+"|"+subString(name_cn,6,12)+"|"+subString(name_cn,12,17)elifunicodenamelen==18:result=subString(name_cn,0,6)+"|"+subString(name_cn,6,12)+"|"+subString(name_cn,12,18)else:result=name_cnfeaturerow.setvalue(destfield,result)featurecur.Updaterow(featurerow)gp.addwarning("Updatefeaturerowvalue"+result+"succeeded")featurerow=featurecur.next();delfeaturecurdelfeaturerow