# -*- coding: utf-8 -*- from cmath import e import os from re import L import arcpy import pythonaddins import math from multiprocessing import Process import random import fnmatch import time def TFtoTB(shape, elem): list1 = [] # 相交,包含,被包含 arcpy.SelectLayerByLocation_management('TF', "INTERSECT", shape) ma1 = int(arcpy.GetCount_management('TF').getOutput(0)) if ma1 != 0: with arcpy.da.SearchCursor('TF', ['TFH1']) as cursorB: for curB in cursorB: tfh1 = str(curB[0]) if list1.__contains__(tfh1): pass else: list1.append(tfh1) arcpy.SelectLayerByLocation_management('TF', "WITHIN", shape) ma2 = int(arcpy.GetCount_management('TF').getOutput(0)) if ma2 != 0: with arcpy.da.SearchCursor('TF', ['TFH1']) as cursorC: for curC in cursorC: tfh2 = str(curC[0]) if list1.__contains__(tfh2): pass else: list1.append(tfh2) arcpy.SelectLayerByLocation_management('TF', "COMPLETELY_CONTAINS", shape) ma3 = int(arcpy.GetCount_management('TF').getOutput(0)) if ma3 != 0: with arcpy.da.SearchCursor('TF', ['TFH1']) as cursorD: for curD in cursorD: tfh3 = str(curD[0]) if list1.__contains__(tfh3): pass else: list1.append(tfh3) if list1 == []: pass else: ii = 0 while ii < len(list1): # tfh.append(list1[ii]) elem.text = elem.text + list1[ii] if ii == (len(list1) - 1): pass elif ( ii + 1 ) % 2 == 0: elem.text = elem.text + '\n'+ ' ' else: elem.text = elem.text + ',' ii = ii + 1 def JZGenerate(outfileshp, zdbh): # def JZGenerate(outfileshp): shpstr = 'JZD.shp' shpstr1 = 'JZX.shp' outpath = outfileshp + '\\' + shpstr outpath1 = outfileshp + '\\' + shpstr1 try: arcpy.CreateFeatureclass_management(outfileshp, shpstr, 'POINT', spatial_reference=4523) #"4507" except: arcpy.Delete_management(outpath) arcpy.CreateFeatureclass_management(outfileshp, shpstr, 'POINT', spatial_reference=4523) try: arcpy.CreateFeatureclass_management(outfileshp, shpstr1, 'POLYLINE', spatial_reference=4523) except: arcpy.Delete_management(outpath1) arcpy.CreateFeatureclass_management(outfileshp, shpstr1, 'POLYLINE', spatial_reference=4523) sr = arcpy.Describe(LayerObject.currentLayer).spatialReference # 加载 lyrjzd = arcpy.mapping.Layer(outpath) arcpy.mapping.AddLayer(df, lyrjzd) lyrjzx = arcpy.mapping.Layer(outpath1) arcpy.mapping.AddLayer(df, lyrjzx) # 添加一群字段 arcpy.AddField_management('JZD', '标识码', 'LONG') arcpy.AddField_management('JZD', 'YSDM', 'TEXT') arcpy.AddField_management('JZD', 'JZDH', 'TEXT') arcpy.AddField_management('JZD', 'JBLX', 'TEXT') arcpy.AddField_management('JZD', 'JZDLX', 'TEXT') arcpy.AddField_management('JZD', 'ZDBH', 'TEXT') arcpy.AddField_management('JZD', '顺序号', 'LONG') arcpy.AddField_management('JZD', 'flag', 'LONG') arcpy.AddField_management('JZD', 'ANGLE', 'LONG') #float按理说应该符合的 bsm = 0 field1 = ['SHAPE@', '标识码', 'YSDM', 'JZDH', 'JBLX', 'JZDLX', 'ZDBH', '顺序号','flag','ANGLE'] # endregion with arcpy.da.InsertCursor('JZD', field1) as cursorZ: with arcpy.da.SearchCursor(LayerObject.currentLayer, ['SHAPE@', '宗地编号']) as cursorA: for curA in cursorA: # region [读折点] poly1 = curA[0] zdbh = curA[1] if zdbh == None or zdbh == '' or zdbh == ' ': pass else: # 获取折点 points = poly1.getPart() pt = points[0] mm = 0 linelist = [] indexp = 0 #存的都是点 while mm < pt.count: jzd = pt[mm] try: xx = round(jzd.X,4) yy = round(jzd.Y,4) if mm == 0 or mm == indexp: xx0 = xx yy0 = yy slist = [] slist.append(pt[mm]) else: numdis0 = xx - xx0 numdis1 = yy - yy0 numdis = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1)) if xx == xx0 and yy == yy0: #一个的结束 indexp = mm + 1 linelist.append(slist) if numdis < 0.1: pass else: slist.append(pt[mm]) mm = mm + 1 except: if mm == indexp: indexp = mm + 1 mm = mm + 1 llen = len(linelist) #判断下如果多环,排个序 linelist1 = pdlen(linelist) linelist = linelist1 # endregion #甭管有没有环,都是第一个来处理 ln1 = linelist[0] list1 = [] nn1 = 0 while nn1 < len(ln1): if nn1 == len(ln1) - 1: fpx = round(ln1[nn1].X,4) fpy = round(ln1[nn1].Y,4) lpx = round(ln1[0].X,4) lpy = round(ln1[0].Y,4) fpoint = arcpy.Point(fpx, fpy) lpoint = arcpy.Point(lpx, lpy) else: fpx = round(ln1[nn1].X,4) fpy = round(ln1[nn1].Y,4) lpx = round(ln1[nn1 + 1].X,4) lpy = round(ln1[nn1 + 1].Y,4) fpoint = arcpy.Point(fpx, fpy) lpoint = arcpy.Point(lpx, lpy) new_array = arcpy.Array() new_array.append(fpoint) new_array.append(lpoint) ln2 = arcpy.Polyline(new_array,sr) #先把起始点加进去 bl1 = addpoint(list1,fpoint) if bl1 == 1: list1.append(fpoint) # list1.append(lpoint) #插入点 arcpy.SelectLayerByLocation_management(LayerObject.currentLayer,'INTERSECT',ln2) count0 = int(arcpy.GetCount_management(LayerObject.currentLayer).getOutput(0)) if count0 < 2: bl4 = addpoint(list1,lpoint) if bl4 == 1: list1.append(lpoint) nn1 = nn1 + 1 else: #对于同一个线段来说,把所有点的位置准确点 listp1 = [] with arcpy.da.SearchCursor(LayerObject.currentLayer, ['SHAPE@', '宗地编号']) as cursorB: for curB in cursorB: zdbh1 = curB[1] #排除自己 if zdbh1 == zdbh: pass else: try: shpB = curB[0] lineB = shpB.boundary () rln = lineB.intersect (ln2, 2) fpoint1 = rln.firstPoint lpoint1 = rln.lastPoint fx = round(fpoint1.X,4) fy = round(fpoint1.Y,4) lx = round(lpoint1.X,4) ly = round(lpoint1.Y,4) #判断这两个点是否都在里面 fp = arcpy.Point(fx, fy) lp = arcpy.Point(lx, ly) list2 = [] bl1 = addpoint(list1,fp) if bl1 == 1: #判断与起点的距离 dis1 = math.sqrt(((fpx - fx)*(fpx - fx)) + ((fpy-fy)*(fpy-fy))) slist2 = [] slist2.append(dis1) slist2.append(fp) list2.append(slist2) bl1 = addpoint(list1,lp) if bl1 == 1: dis1 = math.sqrt(((fpx - lx)*(fpx - lx)) + ((fpy-ly)*(fpy-ly))) slist2 = [] slist2.append(dis1) slist2.append(lp) list2.append(slist2) if len(list2) == 1: listp1.append(list2[0][1]) elif len(list2) == 2: #对2排序 if list2[0][0] < list2[1][0]: listp1.append(list2[0][1]) listp1.append(list2[1][1]) else: listp1.append(list2[1][1]) listp1.append(list2[0][1]) except: pass #得到所有的点,进行一个与起点的排序 nn2 = 0 listp2 =[] while nn2 < len(listp1): num0 = fpx - listp1[nn2].X num1 = fpy - listp1[nn2].Y dis2 = math.sqrt((num0*num0)+(num1*num1)) slistp2 = [] slistp2.append(dis2) slistp2.append(listp1[nn2]) listp2.append(slistp2) nn2 = nn2 + 1 listp2.sort() nn3 = 0 bl3 = 0 while nn3 < len(listp2): #这里要看下要不要除重 bl3 = addpoint(list1,listp2[nn3][1]) if bl3 == 1: list1.append(listp2[nn3][1]) nn3 = nn3 + 1 bl4 = addpoint(list1,lpoint) if bl4 == 1: list1.append(lpoint) nn1 = nn1 + 1 with arcpy.da.InsertCursor('JZX', ['SHAPE@']) as cursorY: if len(linelist) == 1: listF = linezd(list1,poly1) #直接写 ii = 0 zdh = 0 while ii < len(listF): if ii == 0: zdh = zdh + 1 bsm = bsm + 1 # shape point1 = arcpy.Point(listF[ii][0], listF[ii][1]) # 要素代码 ysdm = '6002010300' # 界址点号 jzdh = 'J' + str(zdh) # 宗地代码 zddm = curA[1] ag = calcangle(listF[ii-1],listF[ii]) rlist = [] rlist.append(point1) rlist.append(bsm) rlist.append(ysdm) rlist.append(jzdh) rlist.append('') rlist.append('') rlist.append(zddm) rlist.append(zdh) rlist.append(1) rlist.append(ag) cursorZ.insertRow(rlist) point = arcpy.Point(listF[ii][0], listF[ii][1]) if ii == len(listF) - 1: point0 = arcpy.Point(listF[0][0], listF[0][1]) else: point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1]) array=arcpy.Array() array.add(point) array.add(point0) pl=arcpy.Polyline(array) cursorY.insertRow([pl]) else: numdis0 = listF[ii][0] - listF[ii - 1][0] numdis1 = listF[ii][1] - listF[ii - 1][1] numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1)) if numdis2 < 0.1: pass else: zdh = zdh + 1 bsm = bsm + 1 # shape point1 = arcpy.Point(listF[ii][0], listF[ii][1]) # 要素代码 ysdm = '6002010300' # 界址点号 jzdh = 'J' + str(zdh) # 宗地代码 zddm = curA[1] ag = calcangle(listF[ii-1],listF[ii]) rlist = [] rlist.append(point1) rlist.append(bsm) rlist.append(ysdm) rlist.append(jzdh) rlist.append('') rlist.append('') rlist.append(zddm) rlist.append(zdh) rlist.append(1) rlist.append(ag) cursorZ.insertRow(rlist) point = arcpy.Point(listF[ii][0], listF[ii][1]) if ii == len(listF) - 1: point0 = arcpy.Point(listF[0][0], listF[0][1]) else: point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1]) array=arcpy.Array() array.add(point) array.add(point0) pl=arcpy.Polyline(array) cursorY.insertRow([pl]) ii = ii + 1 else: mm0 = 0 zdh = 0 while mm0 < len(linelist): if mm0 == 0: listF = linezd(list1,poly1) ii = 0 while ii < len(listF): if ii == 0: zdh = zdh + 1 bsm = bsm + 1 # shape point1 = arcpy.Point(listF[ii][0], listF[ii][1]) # 要素代码 ysdm = '6002010300' # 界址点号 jzdh = 'J' + str(zdh) # 宗地代码 zddm = curA[1] ag = calcangle(listF[ii-1],listF[ii]) rlist = [] rlist.append(point1) rlist.append(bsm) rlist.append(ysdm) rlist.append(jzdh) rlist.append('') rlist.append('') rlist.append(zddm) rlist.append(zdh) rlist.append(mm0+1) rlist.append(ag) cursorZ.insertRow(rlist) point = arcpy.Point(listF[ii][0], listF[ii][1]) if ii == len(listF) - 1: point0 = arcpy.Point(listF[0][0], listF[0][1]) else: point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1]) array=arcpy.Array() array.add(point) array.add(point0) pl=arcpy.Polyline(array) cursorY.insertRow([pl]) else: numdis0 = listF[ii][0] - listF[ii - 1][0] numdis1 = listF[ii][1] - listF[ii - 1][1] numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1)) if numdis2 < 0.1: pass else: zdh = zdh + 1 bsm = bsm + 1 # shape point1 = arcpy.Point(listF[ii][0], listF[ii][1]) # 要素代码 ysdm = '6002010300' # 界址点号 jzdh = 'J' + str(zdh) # 宗地代码 zddm = curA[1] ag = calcangle(listF[ii-1],listF[ii]) rlist = [] rlist.append(point1) rlist.append(bsm) rlist.append(ysdm) rlist.append(jzdh) rlist.append('') rlist.append('') rlist.append(zddm) rlist.append(zdh) rlist.append(mm0+1) rlist.append(ag) cursorZ.insertRow(rlist) point = arcpy.Point(listF[ii][0], listF[ii][1]) if ii == len(listF) - 1: point0 = arcpy.Point(listF[0][0], listF[0][1]) else: point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1]) array=arcpy.Array() array.add(point) array.add(point0) pl=arcpy.Polyline(array) cursorY.insertRow([pl]) ii = ii + 1 else: listF = linezd(linelist[mm0],poly1) ii = len(listF) - 1 while ii >= 0: if ii == len(listF) - 1: zdh = zdh + 1 bsm = bsm + 1 # shape point1 = arcpy.Point(listF[ii][0], listF[ii][1]) # 要素代码 ysdm = '6002010300' # 界址点号 jzdh = 'J' + str(zdh) # 宗地代码 zddm = curA[1] ag = calcangle(listF[ii-1],listF[ii]) rlist = [] rlist.append(point1) rlist.append(bsm) rlist.append(ysdm) rlist.append(jzdh) rlist.append('') rlist.append('') rlist.append(zddm) rlist.append(zdh) rlist.append(mm0+1) rlist.append(ag) cursorZ.insertRow(rlist) point = arcpy.Point(listF[ii][0], listF[ii][1]) if ii == len(listF) - 1: point0 = arcpy.Point(listF[0][0], listF[0][1]) else: point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1]) array=arcpy.Array() array.add(point) array.add(point0) pl=arcpy.Polyline(array) cursorY.insertRow([pl]) else: numdis0 = listF[ii][0] - listF[ii - 1][0] numdis1 = listF[ii][1] - listF[ii - 1][1] numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1)) if numdis2 < 0.1: pass else: zdh = zdh + 1 bsm = bsm + 1 # shape point1 = arcpy.Point(listF[ii][0], listF[ii][1]) # 要素代码 ysdm = '6002010300' # 界址点号 jzdh = 'J' + str(zdh) # 宗地代码 zddm = curA[1] ag = calcangle(listF[ii-1],listF[ii]) rlist = [] rlist.append(point1) rlist.append(bsm) rlist.append(ysdm) rlist.append(jzdh) rlist.append('') rlist.append('') rlist.append(zddm) rlist.append(zdh) rlist.append(mm0+1) rlist.append(ag) cursorZ.insertRow(rlist) point = arcpy.Point(listF[ii][0], listF[ii][1]) if ii == len(listF) - 1: point0 = arcpy.Point(listF[0][0], listF[0][1]) else: point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1]) array=arcpy.Array() array.add(point) array.add(point0) pl=arcpy.Polyline(array) cursorY.insertRow([pl]) ii = ii - 1 mm0 = mm0 + 1 def pdlen(list1): ii = 0 list2 = [] while ii < len(list1): pts = list1[ii] length1 = 0 mm = 0 while mm < len(pts): if mm == len(pts) - 1: pt1 = pts[mm] pt2 = pts[0] dis1 = math.sqrt(((pt1.X-pt2.X)*(pt1.X-pt2.X))+((pt1.Y-pt2.Y)*(pt1.Y-pt2.Y))) length1 = length1 + dis1 mm = mm + 1 else: pt1 = pts[mm] pt2 = pts[mm + 1] dis1 = math.sqrt(((pt1.X-pt2.X)*(pt1.X-pt2.X))+((pt1.Y-pt2.Y)*(pt1.Y-pt2.Y))) length1 = length1 + dis1 mm = mm + 1 slist2 = [] slist2.append(length1) slist2.append(pts) list2.append(slist2) ii = ii + 1 list2.sort(reverse=True) nn = 0 list3 = [] while nn < len(list2): list3.append(list2[nn][1]) nn = nn + 1 return list3 def calc_angle(lastp,p): angle = 0 if p[0]-lastp[0]>0 and p[1]-lastp[1]>0:#↗ angle = 0 if p[0]-lastp[0]>0 and p[1]-lastp[1]<0:#↘ angle = 90 if p[0]-lastp[0]<0 and p[1]-lastp[1]<0:#↙ angle = 180 if p[0]-lastp[0]<0 and p[1]-lastp[1]>0:#↖ angle = 270 return angle def cal_stdangle(lastp,p): stdangle=0 dy= p[1]-lastp[1] dx= p[0]-lastp[0] if dx==0 and dy>0: stdangle = 0 if dx==0 and dy<0: stdangle = 180 if dy==0 and dx>0: stdangle = 90 if dy==0 and dx<0: stdangle = 270 if dx>0 and dy>0: stdangle = math.atan(dx/dy)*180/math.pi elif dx<0 and dy>0: stdangle = 360 + math.atan(dx/dy)*180/math.pi elif dx<0 and dy<0: stdangle = 180 + math.atan(dx/dy)*180/math.pi elif dx>0 and dy<0: stdangle = 180 + math.atan(dx/dy)*180/math.pi return stdangle def calcangle(lastp,p): angle=0 stdangle = cal_stdangle(lastp,p) if (stdangle < 45 and stdangle >= 0) or (stdangle <= 360 and stdangle >= 315): angle = 270 if (stdangle < 135 and stdangle >= 45): angle = 0 if (stdangle < 225 and stdangle >= 135): angle = 90 if (stdangle < 315 and stdangle >= 225): angle = 180 return angle def addpoint(list1,pt): ptx = round(pt.X,4) pty = round(pt.Y,4) ii = 0 bl = 1 while ii < len(list1): if ptx == round(list1[ii].X,4) and pty == round(list1[ii].Y,4): bl = -1 ii = ii + 1 return bl def linezd(line1,poly): # 获取折点 mm = 0 pp = 0 list1 = [] while mm < len(line1): plist = [] jzd = line1[mm] try: xx = jzd.X yy = jzd.Y plist.append(xx) plist.append(yy) # 初始顺序 plist.append(pp) list1.append(plist) pp = pp + 1 mm = mm + 1 except: mm = mm + 1 # 对折点排序(算法) if len(list1) < 3: listF = [] else: # listF = zdsort(list1, zx) listF = jzdsort(list1,poly) return listF def jzdsort(list0,poly): m = 0 n = 0 northwest = [] northwest.append(poly.extent.XMin) northwest.append(poly.extent.YMax) minDis = (northwest[1] - list0[0][1])*(northwest[1] - list0[0][1]) + (northwest[0] - list0[0][0])*(northwest[0] - list0[0][0]) maxStdangle = cal_stdangle(northwest,list0[0]) while m < len(list0): Dis = (northwest[1] - list0[m][1])*(northwest[1] - list0[m][1]) + (northwest[0] - list0[m][0])*(northwest[0] - list0[m][0]) if Dis < minDis: minDis = Dis maxStdangle = cal_stdangle(northwest,list0[m]) n = m if abs(Dis-minDis) < 0.000001: stdangle = cal_stdangle(northwest,list0[m]) if stdangle > maxStdangle: maxStdangle = stdangle n = m m = m + 1 i = 0 while i < len(list0): if n == 0: vectangleN = cal_stdangle(list0[0], list0[1]) vectangleNl = cal_stdangle(list0[len(list0)-1], list0[0]) if abs(vectangleN-vectangleNl) < 3: n = len(list0)-1 else: if n == len(list0)-1: vectangleN = cal_stdangle(list0[n], list0[0]) else: vectangleN = cal_stdangle(list0[n], list0[n+1]) vectangleNl = cal_stdangle(list0[n-1], list0[n]) if abs(vectangleN-vectangleNl) < 3: n = n-1 i = i + 1 list4 = [] a = n num = 0 while a < len(list0): slist4 = [] num = num + 1 slist4.append(list0[a][0]) slist4.append(list0[a][1]) slist4.append(num) list4.append(slist4) a = a + 1 a = 0 while a < n: slist4 = [] num = num + 1 slist4.append(list0[a][0]) slist4.append(list0[a][1]) slist4.append(num) list4.append(slist4) a = a + 1 return list4 def mkdir(path): folder = os.path.exists(path) if not folder: #判断是否存在文件夹如果不存在则创建为文件夹 os.makedirs(path) #makedirs 创建文件时如果路径不存在会创建这个路径 def getScale(scale): yushu = float(str(scale).split(".", 1)[0]) % 50 lastTWO = 0 if yushu < 25: lastTWO = float(str(scale).split(".", 1)[0]) // 50 * 50 if yushu < 75 and yushu >= 25: lastTWO = float(str(scale).split(".", 1)[0]) // 50 * 50 + 50 if yushu < 100 and yushu >= 75: lastTWO = float(str(scale).split(".", 1)[0]) // 50 * 50 + 100 scale = "1:" + str(lastTWO).split(".",1)[0] return scale class LayerObject: currentLayer = "" mxd = arcpy.mapping.MapDocument('current') df = arcpy.mapping.ListDataFrames(mxd)[0] LayerObject.currentLayer = arcpy.GetParameterAsText(0) arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "CLEAR_SELECTION") arcpy.SelectLayerByAttribute_management("Copy", "CLEAR_SELECTION") #设置保存路径 savePath1 = arcpy.GetParameterAsText(1) singlelist = arcpy.GetParameterAsText(2).split(",") arcpy.env.workspace = arcpy.GetParameterAsText(3) times = 1 if len(arcpy.GetParameterAsText(4)) > 0: times = float(arcpy.GetParameterAsText(4)) #宗地添加面积字段并计算 arcpy.AddField_management(LayerObject.currentLayer, "Area", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(LayerObject.currentLayer, "Area", "round(!shape.area!,2)", "PYTHON_9.3") #面转线 line = arcpy.PolygonToLine_management("Copy", savePath1 +"\line.shp") #遍历宗地 rows = arcpy.SearchCursor(LayerObject.currentLayer) for row in rows: if row.getValue(u"宗地编号")[-3:] in singlelist: for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "dlclip": arcpy.mapping.RemoveLayer(df, lyr) if lyr.name == "JZD": arcpy.mapping.RemoveLayer(df, lyr) if lyr.name == "JZX": arcpy.mapping.RemoveLayer(df, lyr) if lyr.name == "lineclip": arcpy.mapping.RemoveLayer(df, lyr) filename = row.getValue(u"宗地编号") savePath = os.path.join(savePath1, filename) mkdir(savePath) arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "CLEAR_SELECTION") arcpy.SelectLayerByAttribute_management("Copy", "CLEAR_SELECTION") OBJECTID_1 = row.getValue(u"OBJECTID_1") query = "OBJECTID_1={0}".format(OBJECTID_1) # arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "NEW_SELECTION", query) # df.zoomToSelectedFeatures() df.scale *= 1.1 shape = row.shape #编辑文本元素 qlr = row.getValue(u"权利人") title = filename + qlr + u"宗地图" tfh = " " zdmj = "{}".format(row.getValue(u"Area")) # east = row.getValue(u"四至东") # east =east.replace(")","/0301)") # south = row.getValue(u"四至南") # south =south.replace(")","/0301)") # west = row.getValue(u"四至西") # west =west.replace(")","/0301)") # north = row.getValue(u"四至北") # north =north.replace(")","/0301)") time_tuple = time.localtime(time.time()) rq = "{}年{}月{}日".format(time_tuple[0],time_tuple[1],time_tuple[2]) ny = "{}年{}月".format(time_tuple[0],time_tuple[1]) elemlist = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT") for elem in elemlist: if elem.name == "zddm": elem.text = filename if elem.name == "qlr": elem.text = qlr if elem.name == "tfh": elem.text = tfh #预先添加标准TF图层,计算得到图幅号 TFtoTB(row.shape, elem) if elem.name == "zdmj": elem.text = zdmj # if elem.name == "east": # elem.text = east # if elem.name == "south": # elem.text = south # if elem.name == "west": # elem.text = west # if elem.name == "north": # elem.text = north if elem.name == "scale": elem.text = getScale(df.scale) if elem.name == "ztz": elem.text = random.choice(['王荣华','余思翰','阮梦妍','杨玉婧','赵芩','屈亚星']) if elem.name == "shz": elem.text = random.choice(['何杰','张学仪']) if elem.name == "ztrq": elem.text = rq if elem.name == "shrq": elem.text = rq if elem.name == "jzdtjf": elem.text = ny + "图解法测绘界址点" if elem.name == "zlz": title = filename + qlr + u"宗地草图" elem.text = random.choice(['王荣华','余思翰','阮梦妍','杨玉婧','赵芩','屈亚星']) if elem.name == "jcz": elem.text = random.choice(['何杰','张学仪']) if elem.name == "zlrq": elem.text = rq if elem.name == "jcrq": elem.text = rq arcpy.SelectLayerByAttribute_management("TF", "CLEAR_SELECTION") #生成当前宗地界址点线 JZGenerate(savePath, filename) arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "NEW_SELECTION", query) #界址线添加长度字段 arcpy.AddField_management("JZX", "LENGTH", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management("JZX", "LENGTH", "round(!shape.length!,2)", "PYTHON_9.3") #建立缓冲区 if df.scale > 1000: shapebuffer = arcpy.Polygon.buffer(shape, 10 * times) elif df.scale > 900: shapebuffer = arcpy.Polygon.buffer(shape, 9 * times) elif df.scale > 800: shapebuffer = arcpy.Polygon.buffer(shape, 8 * times) elif df.scale > 700: shapebuffer = arcpy.Polygon.buffer(shape, 7 * times) elif df.scale > 600: shapebuffer = arcpy.Polygon.buffer(shape, 6 * times) elif df.scale > 500: shapebuffer = arcpy.Polygon.buffer(shape, 5 * times) elif df.scale > 400: shapebuffer = arcpy.Polygon.buffer(shape, 4 * times) elif df.scale > 300: shapebuffer = arcpy.Polygon.buffer(shape, 3 * times) elif df.scale > 200: shapebuffer = arcpy.Polygon.buffer(shape, 2 * times) elif df.scale > 100: shapebuffer = arcpy.Polygon.buffer(shape, 1 * times) else: shapebuffer = arcpy.Polygon.buffer(shape, 0.5 * times) #裁剪线图层 arcpy.Clip_analysis(line, shapebuffer, savePath + "\lineclip.shp") lyrline = arcpy.mapping.Layer(savePath + "\lineclip.shp") arcpy.mapping.AddLayer(df, lyrline) arcpy.SelectLayerByLocation_management(lyrline, "BOUNDARY_TOUCHES", shape) arcpy.SelectLayerByAttribute_management(lyrline, "SWITCH_SELECTION") arcpy.DeleteFeatures_management(lyrline) arcpy.SelectLayerByLocation_management("BCDW", "BOUNDARY_TOUCHES", shape) arcpy.CopyFeatures_management("BCDW", savePath + "\BCDW.shp") arcpy.SelectLayerByLocation_management("ZJD", "BOUNDARY_TOUCHES", shape) arcpy.CopyFeatures_management("ZJD", savePath + "\ZJD.shp") arcpy.SelectLayerByLocation_management("Copy", "SHARE_A_LINE_SEGMENT_WITH", shape) neighlist = [] m = int(arcpy.GetCount_management("Copy").getOutput(0)) if m != 0: with arcpy.da.SearchCursor("Copy", ['宗地编号']) as cursor: for cur in cursor: f = cur[0] if f == filename: pass else: if neighlist.__contains__(f): pass else: neighlist.append(f) txtfile = open(os.path.join(savePath1, "neighbourList.txt"),'a') txtfile.write(filename + ':' + str(len(neighlist)) + str(tuple(neighlist)).replace("u","").replace("510811100205JE00","") + '\n') arcpy.Clip_analysis("DLTB", shapebuffer, savePath + "\dlbfclip.shp") shape1 = arcpy.Polygon.buffer(shape, 0.1) # arcpy.Erase_analysis(savePath + "\dlbfclip.shp", shape1, savePath + "\dlclip.shp") # fcs = arcpy.ListFeatureClasses() for fc in fcs: if fc.__contains__("dlclip"): arcpy.Delete_management(os.path.join(arcpy.env.workspace,fc)) #arcpy.env.workspace = savePath arcpy.FeatureClassToGeodatabase_conversion([savePath + '\dlclip.shp'], arcpy.env.workspace) target_lyr = arcpy.env.workspace + "\dlclip" # arcpy.AddRepresentation_cartography(target_lyr, "DL_Rep", import_rule_layer = "DLTB") arcpy.CalculateField_management(target_lyr, "RuleID_1", '!RuleID!', "PYTHON_9.3") lyrdl = arcpy.mapping.Layer(target_lyr) refLayer = arcpy.mapping.ListLayers(mxd, "", df) arcpy.mapping.InsertLayer(df, refLayer[len(refLayer) - 1], lyrdl, "AFTER") arcpy.SetLayerRepresentation_cartography("dlclip", "DL_Rep") for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name == "Point": lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "JZD") lyr.showLabels = True lyr.labelClasses[0].SQLQuery = "ANGLE = 0" lyr.labelClasses[0].expression = "[JZDH]" lyr.labelClasses[1].SQLQuery = "ANGLE = 90" lyr.labelClasses[1].expression = "[JZDH]" lyr.labelClasses[2].SQLQuery = "ANGLE = 180" lyr.labelClasses[2].expression = "[JZDH]" lyr.labelClasses[3].SQLQuery = "ANGLE = 270" lyr.labelClasses[3].expression = "[JZDH]" lyr.visible = True elif lyr.name == "Polyline": lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "JZX") lyr.showLabels = True lyr.labelClasses[0].expression = "[LENGTH]" lyr.visible = True elif lyr.name == LayerObject.currentLayer: lyr.showLabels = True lyr.labelClasses[0].SQLQuery = '"OBJECTID_1" = ' + str(OBJECTID_1) lyr.visible = True elif lyr.name == "Copy": lyr.showLabels = True sqlquery = str(tuple(neighlist)) if sqlquery.endswith(",)"): query = sqlquery.replace(",)", ")") else: query = sqlquery lyr.labelClasses[0].SQLQuery = '"宗地编号" IN ' + query.replace("u","") lyr.visible = True elif lyr.name == "line": lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "lineclip") lyr.visible = True elif lyr.name == "dlclip": lyr.visible = True elif lyr.name == "BCDW": lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "BCDW") lyr.visible = True elif lyr.name == "ZJD": lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "ZJD") lyr.visible = True else: lyr.visible = False #saveMxdPath = savePath + "\\" + title + ".mxd" #mxd.saveACopy(saveMxdPath) arcpy.mapping.ExportToJPEG( mxd, savePath + "\\" + title + ".jpg", resolution = 300) arcpy.Delete_management(savePath +"\dlbfclip.shp") arcpy.Delete_management(savePath +"\dlclip.shp") #arcpy.Delete_management(savePath +"\lineclip.shp") #arcpy.Delete_management(savePath +"\JZD.shp") #arcpy.Delete_management(savePath +"\JZX.shp") arcpy.AddMessage (filename + ' Finish!') txtfile.close() arcpy.Delete_management(savePath1 +"\line.shp")