概述
#coding: UTF-8
importarcpyimportosimporttypesimportstringimportshutilimportsysimportreimportopenpyxl#ws是一个xls的工作表#mode是替换模型,1完全替换,2模糊替换,3 追加替换
defreplacexls(ws,mode, text, replaceText):
rows=ws.max_row
cols=ws.max_column
changeCells=0if (mode <0):return -1
elif (mode>3):return -1
for row in range(1, rows + 1):for col in range(1, cols + 1):try:
content= ws.cell(row=row, column=col).valueif (content !=None):#mode1: fullmatch replacement
if (mode == 1):if (content ==text):
ws.cell(row=row, column=col).value =replaceText
changeCells+= 1
#mode2: partial replacement
elif (mode == 2):if (type(content) ==str):
ws.cell(row=row, column=col).value = content.replace(text, replaceText, 1)
changeCells+= 1
#mode3: partialmatch and filling
elif (mode == 3):if (type(content) ==str):
ws.cell(row=row, column=col).value = content.replace(text, text + replaceText, 1)
changeCells+= 1
exceptException as e:print(traceback.format_exc())returnchangeCellsdefchangeData(file, mode, text, replaceText):#load the file(*.xlsx)
wb =openpyxl.load_workbook(file)#! deal with one sheet
ws=wb.worksheets[0]
replacexls(ws, mode, text, replaceText)definitProgress(hint,num):
arcpy.SetProgressor("step", hint,0,num,1)defstep():
arcpy.SetProgressorLabel(u"waiting....")
arcpy.SetProgressorPosition()deffreeProgress():
arcpy.ResetProgressor()defAddLayer(mxd,inFeature):
df=arcpy.mapping.ListDataFrames(mxd)[0]
addLayer=arcpy.mapping.Layer(inFeature)
arcpy.mapping.AddLayer(df, addLayer,"TOP") #AUTO_ARRANGE�?BOTTOM",TOP#######
defgetLayer(layername):
layername=layername.upper()
mxd= arcpy.mapping.MapDocument("CURRENT")try:for lyr inarcpy.mapping.ListLayers(mxd):if lyr.name.upper()==layername:returnlyrreturnNonefinally:delmxd###
defmidFill(sumn,mystr,Fill):
n=getlength(mystr)if n>=sumn:returnmystr
leftn=int((sumn-n)/2)
s=""lefts=s.ljust(leftn,Fill)
s=""rightn=sumn-n-leftn
rights=s.ljust(rightn,Fill)return lefts+mystr+rights#获得一个表的记录数
defgetCount(inFeature):
result=arcpy.GetCount_management(inFeature)
count=int(result.getOutput(0))returncountdefclearSelect(inFeature):
mylyr="mylyr"arcpy.MakeFeatureLayer_management (inFeature, mylyr)
arcpy.SelectLayerByAttribute_management (mylyr,"CLEAR_SELECTION")defvalidate_sheet_name(sheet_name):"""Validate sheet name to excel limitations
- 31 character length
- there characters not allowed : / ? * [ ]"""
importreif len(sheet_name) > 31:
sheet_name= sheet_name[:31]#Replace invalid sheet character names with an underscore
r = re.compile(r'[:\/?*[]]')
sheet_name= r.sub("_", sheet_name)returnsheet_name#把内多边形,分解出来
defsplitNgeometry(mgeometry):
num=mgeometry.count
Sumarray=arcpy.Array()
parray=arcpy.Array()for i inrange(num):
pt=mgeometry[i]ifpt:
parray.add(pt)else:#内边形
Sumarray.add(parray)
parray.removeAll()
Sumarray.add(parray)returnSumarraydefgetJZDH(pgeometry):
mylayer="mylayer"arcpy.MakeFeatureLayer_management(JZDFeature, mylayer)
arcpy.SelectLayerByLocation_management (mylayer,'intersect',pgeometry )
num=getCount(mylayer)
cur=arcpy.da.SearchCursor(mylayer,[JZDHFieldName])
myJZDH=""i=1
for row incur:
myJZDH=row[0]ifcur:delcurreturnmyJZDH
startrow=4
#获得点的距离
defpointDistance(pt1,pt2):return math.sqrt((pt1.X-pt2.X)*(pt1.X-pt2.X)+(pt1.Y-pt2.Y)*(pt1.Y-pt2.Y))defwriteonexls(partgeometry,ws):globalstartrow
num=partgeometry.countfor i inrange(num):
pt=partgeometry[i]
x=pt.X
y=pt.Y
arcpy.AddMessage(str(x)+":"+str(y))
xstr="%.3f" %x
ystr="%.3f" %y
ws.cell(row=startrow, column=3).value=ystr
ws.cell(row=startrow, column=4).value=xstr
pointGeometry=arcpy.PointGeometry(pt)
JZDH=getJZDH(pointGeometry)
ws.cell(row=startrow, column=1).value =JZDHif (i < num - 1):
pt2= partgeometry[i+1]
linelen=pointDistance(pt,pt2)
ws.cell(row=startrow+1, column=2).value = "%.2f" %linelen
startrow=startrow+2
defwriteXLS(ZDH,geometry):
scriptPath=sys.path[0]
toolSharePath=scriptPath #os.path.dirname(scriptPath)
myxls="界址点2000国家大地坐标系.xlsx" ##encode("GBK")
sFile=toolSharePath+u"/xls/"+myxls
tFile=outpath+"/"+ZDH+".xlsx"arcpy.AddMessage(u"sFile"+sFile+","+tFile)
shutil.copyfile(sFile,tFile)
wb=openpyxl.load_workbook(tFile)
ws=wb.worksheets[0]
replacexls(ws,2, "[宗地号]", ZDH)
area=geometry.area
replacexls(ws,2, "[平方米]", '%.2f'%area)
area=area/10000replacexls(ws,2, "[公顷]", '%.4f' %area)
linelen=geometry.length
replacexls(ws,2, "[总长]", '%.2f' %linelen)globalstartrow
startrow= 4part_count= geometry.partCount #有几部分
pointcount=geometry.pointCount
replacexls(ws,2, "[总节点]", '%d' % (pointcount+part_count-1) )
Sumarray=arcpy.Array()for i inrange(part_count):
partgeometry=geometry.getPart(i)
SpliArray=splitNgeometry(partgeometry)
N=SpliArray.count#arcpy.AddMessage("NNNNN=====:"+str(N))
for j inrange(N):
Splitgeometry=SpliArray[j]
writeonexls(Splitgeometry,ws)
wb.save(tFile)defmain():
num=getCount(ZDFeature)if num<1:
arcpy.AddMessage(u"宗地没有数据")returnclearSelect(JZDFeature)
num=getCount(JZDFeature)if num<1:
arcpy.AddMessage(u"宗地没有数据")return
for row in arcpy.da.SearchCursor(ZDFeature, ["OID@", "SHAPE@",ZDHFieldName]):
FID=row[0]
pgeometry=row[1]
ZDH=row[2]
writeXLS(ZDH,pgeometry)defprintauthor(toolname):
titlestr=""sumn=60Fill='*'titlestr=titlestr.ljust(sumn,Fill)
arcpy.AddMessage(titlestr)
arcpy.AddMessage(midFill(sumn,u"欢迎使用:"+toolname,Fill))
mystr=u"本工具闫磊编写QQ:276529800,电话:18987281928"arcpy.AddMessage(midFill(sumn,mystr,Fill))
mystr=u"使用前请做好数据备份,工具产生的不良后果请自行承担!"arcpy.AddMessage(midFill(sumn,mystr,Fill))
arcpy.AddMessage(titlestr)
ZDFeature= arcpy.GetParameterAsText(0) #宗地
ZDHFieldName=arcpy.GetParameterAsText(1) #宗地号字段
JZDFeature=arcpy.GetParameterAsText(2) #输出数据
JZDHFieldName=arcpy.GetParameterAsText(3) #界址点号字段
outpath=arcpy.GetParameterAsText(4) #路径
arcpy.env.overwriteOutput=True#printauthor(u"部标准坐标导入")
if notos.path.exists(outpath):
os.makedirs(outpath)
main()
最后
以上就是飘逸太阳为你收集整理的arcgis出界址点成果表_界址点成果表打印的全部内容,希望文章能够帮你解决arcgis出界址点成果表_界址点成果表打印所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复