それには辞書を使います。彼らがどれほど速いか私は驚いています。
import arcpy
def GetDict(fc,precision):
fields = ['SHAPE@','OID@']
# use a dictionary with x1-y1-xn-yn key
dict = {}
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
fpx = round(row[0].firstPoint.X,precision)
fpy = round(row[0].firstPoint.Y,precision)
lpx = round(row[0].lastPoint.X,precision)
lpy = round(row[0].lastPoint.Y,precision)
key= u'{0}-{1}-{2}-{3}'.format(fpx,fpy,lpx,lpy)
if not dict.has_key(key):
dict[key] = row[0]
return dict
def GetOIDsOfLinesInNeedofFlipping(fc,dict,precision):
fields = ['SHAPE@','OID@']
flipoids = []
changedoids = [] # polyline has been more than just flipped
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
fpx = round(row[0].firstPoint.X,precision)
fpy = round(row[0].firstPoint.Y,precision)
lpx = round(row[0].lastPoint.X,precision)
lpy = round(row[0].lastPoint.Y,precision)
ftkey= u'{0}-{1}-{2}-{3}'.format(fpx,fpy,lpx,lpy)
tfkey= u'{0}-{1}-{2}-{3}'.format(lpx,lpy,fpx,fpy)
if not dict.has_key(ftkey):
if dict.has_key(tfkey):
flipoids.append(row[1])
else:
changedoids.append(row[1])
if len(changedoids) > 0:
print(u'these are not the {0} oids you are looking for'.format(len(changedoids)))
return flipoids
def FlipPolylines(fc,oids):
fields = ['SHAPE@','OID@']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[1] in oids:
# https://gis.stackexchange.com/a/67422/59
if row[0].partCount > 1:
print "Warning: multiple parts! extra parts are automatically trimmed!"
lp= row[0].getPart(0)
rPnts=arcpy.Array()
for i in range(len(lp)): rPnts.append(lp[len(lp)-i-1])
rPoly=arcpy.Polyline(rPnts)
row[0] = rPoly
cursor.updateRow(row)
return
def main():
precision = 1
dict = GetDict(r'H:\filegdbs\sewer.gdb\sewermains',precision) #the "master"
print(u'keys = {0}'.format(len(dict)))
oids = GetOIDsOfLinesInNeedofFlipping(r'H:\filegdbs\sewer.gdb\sewermainsflipped',dict,precision)
print(u'{0} polylines need flipping'.format(len(oids)))
if len(oids) > 0:
FlipPolylines(r'H:\filegdbs\sewer.gdb\sewermainsflipped',oids)
else:
print("none need flipping")
return
if __name__ == '__main__':
main()