import arcpy import math def fillzh(str1): if len(str1) == 1: str2 = '00' + str1 elif len(str1) == 2: str2 = '0' + str1 elif len(str1) == 4: str2 = '000' else: str2 = str1 return str2 def dmname(): dmxpath = r'D:\4work_now\20241017dm\20241129\dzgq1\dmx.shp' dmdpath = r'D:\4work_now\20241017dm\20241129\dzgq1\dmd.shp' gqpath = r'D:\4work_now\20241017dm\20241129\dzgq1\zxx.shp' arcpy.MakeFeatureLayer_management(dmxpath, 'DMshp') arcpy.MakeFeatureLayer_management(dmdpath, 'DMDshp') arcpy.MakeFeatureLayer_management(gqpath, 'GQshp') arcpy.AddField_management('DMshp', 'ZH', 'TEXT') arcpy.AddField_management('DMshp','KZ','LONG') arcpy.AddField_management('DMshp','JZ','LONG') with arcpy.da.SearchCursor('GQshp',['SHAPE@']) as cursorZ: for curZ in cursorZ: GQline = curZ[0] length = GQline.length # mm = 0 # nn = 0 with arcpy.da.UpdateCursor('DMshp',['SHAPE@','ZH','KZ','JZ']) as cursorA: for curA in cursorA: line1 = curA[0] buf1 = line1.buffer(1) arcpy.SelectLayerByLocation_management('DMDshp', 'WITHIN', buf1) pc = int(arcpy.GetCount_management('DMDshp')[0]) if pc == 0: pass else: minnum = 9999 with arcpy.da.SearchCursor('DMDshp', ['SHAPE@']) as cursorAA: for curAA in cursorAA: tuple1 = GQline.queryPointAndDistance(curAA[0]) lj = tuple1[2] if lj < minnum: minnum = lj pt = curAA[0] len1 =GQline.measureOnLine(pt) kz = int(len1 // 1000) jz = int(len1 % 1000) if jz % 10 <5: num1 = jz % 10 jz = jz - num1 else: num1 = jz % 10 num2 = 10 - num1 jz = jz + num2 str2 = fillzh(str(jz)) if jz == 1000: kz = kz+1 str1 = 'K' + str(kz) + '+' + str2 + ' ' jz = 0 else: str1 = 'K' + str(kz) + '+' + str2 + ' ' curA[1] = str1 curA[2] = kz curA[3] = jz cursorA.updateRow(curA) print(str1) # if curA[1] == '': # line1 = curA[0] # if nn < 1000: # str1 = fillzh(str(nn)) # zh1 = 'K' + str(mm) + '+' + str1 # curA[2] = mm # curA[3] = nn # curA[1] = zh1 # cursorA.updateRow(curA) # nn = nn + 50 # else: # nn = 0 # mm = mm + 1 # str1 = fillzh(str(nn)) # zh1 = 'K' + str(mm) + '+' + str1 # curA[2] = mm # curA[3] = nn # curA[1] = zh1 # cursorA.updateRow(curA) # nn = nn + 50 # arcpy.SelectLayerByLocation_management('DMDshp','WITHIN',buf1) # pc = int(arcpy.GetCount_management('DMDshp')[0]) # if pc == 0: # pass # else: # with arcpy.da.SearchCursor('DMDshp',['SHAPE@','height']) as cursorAA: # for curAA in cursorAA: # len1 = GQline.measureOnLine(curAA[0]) # len2 = round(length - len1) # kz = int(len2 // 1000) # jz = int(len2 % 1000) # if jz % 10 != 0: # jz = jz + 1 # gc = round(curAA[1],1) # str2 = fillzh(str(jz)) # str1 = 'K' + str(kz) + '+' + str2 + ' ' # curA[1] = str1 # cursorA.updateRow(curA) # print('1') # arcpy.SelectLayerByLocation_management('DMDshp', 'WITHIN', buf1) # pc2 = int(arcpy.GetCount_management('DMDshp')[0]) # if pc2 > 0: # with arcpy.da.UpdateCursor('DMDshp', ['SHAPE@', 'ZH', 'LJ', 'SFR', 'SFL']) as cursorB: # for curB in cursorB: # tuple1 = GQline.queryPointAndDistance(curB[0]) # lj = round(tuple1[2], 4) # if tuple1[3] == False: # curB[3] = -1 # else: # curB[3] = 1 # pt = curB[0] # buf2 = pt.buffer(1) # arcpy.SelectLayerByLocation_management('roadshp', 'INTERSECT', buf2) # pc3 = int(arcpy.GetCount_management('roadshp')[0]) # # arcpy.SelectLayerByLocation_management('roadshp', 'CONTAINS', buf2) # # pc4 = int(arcpy.GetCount_management('roadshp')[0]) # if pc3 > 0: # curB[4] = 1 # curB[1] = zh # curB[2] = lj # cursorB.updateRow(curB) if __name__ == '__main__': try: dmname() except arcpy.ExecuteError: arcpy.AddMessage (arcpy.GetMessages())