arcpy.AddMessage("Thresholding : keep cells with a drainage area >= " + str(ThresholdDrainage) + "km� - Step " + str(ncurrentstep) + "/" + str(nstep)) ThresholdStep = arcpy.gp.Con_sa(FlowAcc, FlowAcc, "%ScratchWorkspace%\\ThresholdStep", "", Expression) #/linking of the streamnetwork ncurrentstep+=1 arcpy.AddMessage("Linking Stream Network - Step " + str(ncurrentstep) + "/" + str(nstep)) StreamLink = arcpy.gp.StreamLink_sa(ThresholdStep, FlowDir, "%ScratchWorkspace%\\StreamLink") #/conversion of the raster streamnetwork into a polyline layer ncurrentstep+=1 arcpy.AddMessage("Converting into a polyline feature - Step " + str(ncurrentstep) + "/" + str(nstep)) Network = arcpy.gp.StreamToFeature_sa(StreamLink, FlowDir, Output, "NO_SIMPLIFY") #/updating fields try : UPD_SL.UpToDateShapeLengthField(Network) except : try : arcpy.AddField_management(Network, "Length", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(Network, "Length", "!shape.length!", "PYTHON_9.3", "") except : pass try : arcpy.DeleteField_management(Network, ["GRID_CODE"]) except : pass
# CODING #=============================================================================== ########################### ### Valley bottom width ### ########################### if str(TypeOfWidth) == "Valley Bottom": #/conversion of the polygon into a polyline arcpy.AddMessage("Converting to line - Step " + str(ncurrentstep) + "/" + str(nstep)) PolygonToLine = arcpy.FeatureToLine_management( Polygon, "%ScratchWorkspace%\\PolygonToLine", "", "ATTRIBUTES") ncurrentstep += 1 arcpy.AddMessage("Up to date the \"Shape_Length\" field - Step " + str(ncurrentstep) + "/" + str(nstep)) UPD_SL.UpToDateShapeLengthField(PolygonToLine) #/disaggregation of the polygon boundaries ncurrentstep += 1 arcpy.AddMessage("Splitting the Polygon contour - Step " + str(ncurrentstep) + "/" + str(nstep)) SplitLine = dS.SLEM(PolygonToLine, DisaggregationStepForWidth, "%ScratchWorkspace%\\SplitLine", ScratchW, "true") #/calculation of the valley bottom width ncurrentstep += 1 arcpy.AddMessage("Converting to points - Step " + str(ncurrentstep) + "/" + str(nstep)) SplitLineToPoints = arcpy.FeatureVerticesToPoints_management( SplitLine, "%ScratchWorkspace%\\SplitLineToPoints", "MID")
str(nstep)) arcpy.DeleteIdentical_management(ACtoLine, ["Shape_Length"]) #/processing the contact length ncurrentstep += 1 arcpy.AddMessage( "Intersecting active channel lines with the disaggregated valley bottom - Step " + str(ncurrentstep) + "/" + str(nstep)) Intersect = arcpy.Intersect_analysis([DisaggregatedValleyBottom, ACtoLine], "%ScratchWorkspace%\\Intersect", "ALL", "", "LINE") ncurrentstep += 1 arcpy.AddMessage("Up to date the \"Shape_Length\" field - Step " + str(ncurrentstep) + "/" + str(nstep)) UPD_SL.UpToDateShapeLengthField(Intersect) arcpy.AddField_management(Intersect, "contactL", "DOUBLE", "", "9", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(Intersect, "contactL", "!Shape_Length!", "PYTHON_9.3", "") #/transfer of the contact length values into the disaggregated valley bottom ncurrentstep += 1 arcpy.AddMessage( "Transferring the information thanks to a Spatial join - Step " + str(ncurrentstep) + "/" + str(nstep)) fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(Intersect) fieldmappings.addTable(DisaggregatedValleyBottom)
else : nstep = 4 ncurrentstep = 1 #=============================================================================== # CODING #=============================================================================== #/creation of the output with the new fields arcpy.AddMessage("Creating output with new fields - Step " + str(ncurrentstep) + "/" + str(nstep)) Out = arcpy.CopyFeatures_management(inFC, "%ScratchWorkspace%\\OutTemp") arcpy.AddField_management(Out, "Z_Up", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(Out, "Z_Down", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(Out) ncurrentstep+=1 arcpy.AddMessage("Converting input line into points and adding surface information - Step " + str(ncurrentstep) + "/" + str(nstep)) Pts = arcpy.FeatureVerticesToPoints_management(Out, "%ScratchWorkspace%\\Pts", "BOTH_ENDS") arcpy.AddXY_management(Pts) #/extraction and calculation of the topologic metrics # Ajout Aurelie arcpy.CheckOutExtension("3D") # Fin Ajout Aurelie arcpy.AddSurfaceInformation_3d(Out, DEM, "Z_MEAN", "BILINEAR") arcpy.AddSurfaceInformation_3d(Pts, DEM, "Z", "BILINEAR") arcpy.AddField_management(Out, "Slope", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(Out, "Slope3D", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
[["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) ncurrentstep += 1 arcpy.AddMessage("Merging Thiessen polygons - Step " + str(ncurrentstep) + "/" + str(nstep)) Dissolve1 = arcpy.Dissolve_management(Join, "%ScratchWorkspace%\\Dissolve1", "Rank_UGO", "", "MULTI_PART", "DISSOLVE_LINES") ncurrentstep += 1 arcpy.AddMessage("Finalizing the centerline - Step " + str(ncurrentstep) + "/" + str(nstep)) Dissolve1ToLine = arcpy.Intersect_analysis( [Dissolve1, Dissolve1], "%ScratchWorkspace%\\Dissolve1ToLine", "", "", "LINE") UPD_SL.UpToDateShapeLengthField(Dissolve1ToLine) arcpy.DeleteIdentical_management(Dissolve1ToLine, ["Shape_Length"]) RawCenterline = arcpy.Intersect_analysis([Dissolve1ToLine, Polygon], "%ScratchWorkspace%\\RawCenterline", "ALL", "", "INPUT") ncurrentstep += 1 arcpy.AddMessage("Smoothing centerline - Step " + str(ncurrentstep) + "/" + str(nstep)) Centerline = arcpy.SmoothLine_cartography(RawCenterline, Output, "PAEK", Smoothing, "FIXED_CLOSED_ENDPOINT", "NO_CHECK") #/deleting residual fields try:
def SLEM(Line, Distance, Output, TempFolder, TF): CopyLine = arcpy.CopyFeatures_management(Line, "%ScratchWorkspace%\CopyLine") fieldnames = [f.name for f in arcpy.ListFields(CopyLine)] #/identification of the polyline type : raw, UGOs, sequenced UGOs, or AGOs k = 0 if "Rank_AGO" in fieldnames: k = 3 elif "Order_ID" in fieldnames: k = 2 elif "Rank_UGO" in fieldnames: k = 1 arcpy.AddMessage(k) ################################ ########## Raw polyline ######## ################################ if k == 0: #/shaping of the segmented result arcpy.AddField_management(CopyLine, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "Rank_UGO", "[" + fieldnames[0] + "]", "VB", "") arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB", "") arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "To_Measure", "!shape.length!", "PYTHON_9.3", "") #/conversion in routes LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO", "%ScratchWorkspace%\\LineRoutes", "TWO_FIELDS", "From_Measure", "To_Measure") #/creation of the event table PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%", "PointEventTEMP", "", "") arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(LineRoutes) rowslines = arcpy.SearchCursor(LineRoutes) rowsevents = arcpy.InsertCursor(PointEventTEMP) for line in rowslines: tempdistance = float(0) while (tempdistance < float(line.Shape_Length)): row = rowsevents.newRow() row.Rank_UGO = line.Rank_UGO row.To_M = tempdistance + float(Distance) row.Distance = tempdistance rowsevents.insertRow(row) tempdistance = tempdistance + float(Distance) del rowslines del rowsevents #/creation of the route event layer MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr( LineRoutes, "Rank_UGO", PointEventTEMP, "Rank_UGO LINE Distance To_M", "%ScratchWorkspace%\\MakeRouteEventTEMP") Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP, "%ScratchWorkspace%\\Split", "", "0", "0", "0") Sort = arcpy.Sort_management( Split, Output, [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) arcpy.DeleteField_management(Sort, "To_M") #/calculation of the "Distance" field UPD_SL.UpToDateShapeLengthField(Sort) rows1 = arcpy.UpdateCursor(Sort) rows2 = arcpy.UpdateCursor(Sort) line2 = rows2.next() line2.Distance = 0 rows2.updateRow(line2) nrows = int(str(arcpy.GetCount_management(Sort))) n = 0 for line1 in rows1: line2 = rows2.next() if n == nrows - 1: break if n == 0: line1.Distance = 0 if line2.Rank_UGO == line1.Rank_UGO: line2.Distance = line1.Distance + line1.Shape_Length rows2.updateRow(line2) if line2.Rank_UGO != line1.Rank_UGO: line2.Distance = 0 rows2.updateRow(line2) n += 1 #/deleting of the temporary files if str(TF) == "true": arcpy.Delete_management(Split) arcpy.Delete_management(CopyLine) arcpy.Delete_management(LineRoutes) arcpy.Delete_management(PointEventTEMP) ################## ###### UGO ####### ################## if k == 1: #/shaping of the segmented result arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB", "") arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "To_Measure", "!shape.length!", "PYTHON_9.3", "") #/conversion in routes LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO", "%ScratchWorkspace%\\LineRoutes", "TWO_FIELDS", "From_Measure", "To_Measure") #/creation of the event table PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%", "PointEventTEMP", "", "") arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(LineRoutes) rowslines = arcpy.SearchCursor(LineRoutes) rowsevents = arcpy.InsertCursor(PointEventTEMP) for line in rowslines: tempdistance = float(0) while (tempdistance < float(line.Shape_Length)): row = rowsevents.newRow() row.Rank_UGO = line.Rank_UGO row.To_M = tempdistance + float(Distance) row.Distance = tempdistance rowsevents.insertRow(row) tempdistance = tempdistance + float(Distance) del rowslines del rowsevents #/creation of the route event layer MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr( LineRoutes, "Rank_UGO", PointEventTEMP, "Rank_UGO LINE Distance To_M", "%ScratchWorkspace%\\MakeRouteEventTEMP") Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP, "%ScratchWorkspace%\\Split", "", "0", "0", "0") Sort = arcpy.Sort_management( Split, Output, [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) arcpy.DeleteField_management(Sort, "To_M") #/calculation of the "Distance" field UPD_SL.UpToDateShapeLengthField(Sort) rows1 = arcpy.UpdateCursor(Sort) rows2 = arcpy.UpdateCursor(Sort) line2 = rows2.next() line2.Distance = 0 rows2.updateRow(line2) nrows = int(str(arcpy.GetCount_management(Sort))) n = 0 for line1 in rows1: line2 = rows2.next() if n == nrows - 1: break if n == 0: line1.Distance = 0 if line2.Rank_UGO == line1.Rank_UGO: line2.Distance = line1.Distance + line1.Shape_Length rows2.updateRow(line2) if line2.Rank_UGO != line1.Rank_UGO: line2.Distance = 0 rows2.updateRow(line2) n += 1 #/deleting of the temporary files if str(TF) == "true": arcpy.Delete_management(Split) arcpy.Delete_management(CopyLine) arcpy.Delete_management(LineRoutes) arcpy.Delete_management(PointEventTEMP) ################################ ######### Sequenced UGO ######## ################################ if k == 2: #/shaping of the segmented result arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB", "") arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "To_Measure", "!Shape_Length!", "PYTHON_9.3", "") #/conversion in routes LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO", "%ScratchWorkspace%\\LineRoutes", "TWO_FIELDS", "From_Measure", "To_Measure") arcpy.AddField_management(LineRoutes, "Order_ID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") Sort = arcpy.Sort_management(Line, "%ScratchWorkspace%\\Sort", [["Rank_UGO", "ASCENDING"]]) rows1 = arcpy.UpdateCursor(LineRoutes) rows2 = arcpy.SearchCursor(Sort) for line1 in rows1: line2 = rows2.next() line1.Order_ID = line2.Order_ID rows1.updateRow(line1) #/creation of the event table PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%", "PointEventTEMP", "", "") arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Order_ID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(LineRoutes) rowslines = arcpy.SearchCursor(LineRoutes) rowsevents = arcpy.InsertCursor(PointEventTEMP) for line in rowslines: tempdistance = float(0) while (tempdistance < float(line.Shape_Length)): row = rowsevents.newRow() row.To_M = tempdistance + float(Distance) row.Order_ID = line.Order_ID row.Rank_UGO = line.Rank_UGO row.Distance = tempdistance rowsevents.insertRow(row) tempdistance = tempdistance + float(Distance) del rowslines del rowsevents MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr( LineRoutes, "Rank_UGO", PointEventTEMP, "Rank_UGO LINE Distance To_M", "%ScratchWorkspace%\\MakeRouteEventTEMP") Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP, "%ScratchWorkspace%\\Split", "", "0", "0", "0") Sort = arcpy.Sort_management( Split, Output, [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) arcpy.DeleteField_management(Sort, "To_M") #/calculation of the "Distance" field UPD_SL.UpToDateShapeLengthField(Sort) rows1 = arcpy.UpdateCursor(Sort) rows2 = arcpy.UpdateCursor(Sort) line2 = rows2.next() line2.Distance = 0 rows2.updateRow(line2) nrows = int(str(arcpy.GetCount_management(Split))) n = 0 for line1 in rows1: line2 = rows2.next() if n >= nrows - 1: break if n == 0: line1.Distance = 0 if line2.Rank_UGO == line1.Rank_UGO: line2.Distance = line1.Distance + line1.Shape_Length rows2.updateRow(line2) if line2.Rank_UGO != line1.Rank_UGO: line2.Distance = 0 rows2.updateRow(line2) n += 1 #/deleting of the temporary files if str(TF) == "true": arcpy.Delete_management(Split) arcpy.Delete_management(CopyLine) arcpy.Delete_management(LineRoutes) arcpy.Delete_management(PointEventTEMP) ############# #### AGO #### ############# if k == 3: #/shaping of the segmented result arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "VB", "") arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") try: arcpy.CalculateField_management(CopyLine, "To_Measure", "!shape.length!", "PYTHON_9.3", "") except: arcpy.CalculateField_management(CopyLine, "To_Measure", "!forme.length!", "PYTHON_9.3", "") #/conversion in routes LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_AGO", "%ScratchWorkspace%\\LineRoutes", "TWO_FIELDS", "From_Measure", "To_Measure") arcpy.AddField_management(LineRoutes, "Order_ID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(LineRoutes, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(LineRoutes, "AGO_Val", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(LineRoutes) Ext.Export(CopyLine, TempFolder, "ExportTable") fichier = open(TempFolder + "\\ExportTable.txt", 'r') Order_ID = [] Rank_UGO = [] Dist = [] Rank_AGO = [] AGO_Val = [] head = fichier.readline().split('\n')[0].split(';') iOrder_ID = head.index("Order_ID") iRank_UGO = head.index("Rank_UGO") iRank_AGO = head.index("Rank_AGO") iAGO_Val = head.index("AGO_Val") for l in fichier: Order_ID.append(int(l.split('\n')[0].split(';')[iOrder_ID])) Rank_UGO.append(int(l.split('\n')[0].split(';')[iRank_UGO])) Rank_AGO.append(float(l.split('\n')[0].split(';')[iRank_AGO])) AGO_Val.append( float(l.split('\n')[0].split(';')[iAGO_Val].replace(',', '.'))) p = 0 rows1 = arcpy.UpdateCursor(LineRoutes) for line1 in rows1: line1.Order_ID = Order_ID[p] line1.Rank_UGO = Rank_UGO[p] line1.Rank_AGO = Rank_AGO[p] line1.AGO_Val = AGO_Val[p] rows1.updateRow(line1) p += 1 #/creation of the event table PointEventTEMP = arcpy.CreateTable_management("%ScratchWorkspace%", "PointEventTEMP", "", "") arcpy.AddField_management(PointEventTEMP, "Distance_From_Start", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Order_ID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Rank_AGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "AGO_Val", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") rowslines = arcpy.SearchCursor(LineRoutes) rowsevents = arcpy.InsertCursor(PointEventTEMP) for line in rowslines: tempdistance = float(0) while (tempdistance < float(line.Shape_Length)): row = rowsevents.newRow() row.Distance_From_Start = tempdistance row.To_M = tempdistance + float(Distance) row.Order_ID = line.Order_ID row.Rank_UGO = line.Rank_UGO row.Rank_AGO = line.Rank_AGO row.AGO_Val = line.AGO_Val rowsevents.insertRow(row) tempdistance = tempdistance + float(Distance) del rowslines del rowsevents MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr( LineRoutes, "Rank_AGO", PointEventTEMP, "Rank_AGO LINE Distance_From_Start To_M", "%ScratchWorkspace%\\MakeRouteEventTEMP") Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP, "%ScratchWorkspace%\\Split", "", "0", "0", "0") arcpy.AddField_management(Split, "Distance", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(Split, "Distance", "!Distance_From_Start!", "PYTHON_9.3", "") arcpy.DeleteField_management(Split, ["To_M", "Distance_From_Start"]) Sort = arcpy.Sort_management( Split, Output, [["Order_ID", "ASCENDING"], ["Rank_UGO", "ASCENDING"], ["Rank_AGO", "ASCENDING"], ["Distance", "ASCENDING"]]) UPD_SL.UpToDateShapeLengthField(Sort) #/deleting of the temporary files if str(TF) == "true": arcpy.Delete_management(Split) arcpy.Delete_management(CopyLine) arcpy.Delete_management(LineRoutes) arcpy.Delete_management(PointEventTEMP) return Sort
arcpy.AddMessage(" Dissolving all lines with the same \"Rank_UGO\" field - 2/9") Dissolve = arcpy.Dissolve_management(InflToLine, "%ScratchWorkspace%\\Dissolve", "Rank_UGO", "", "MULTI_PART", "DISSOLVE_LINES") arcpy.AddMessage(" Smoothing by Bezier interpolation - 3/9") Bezier = arcpy.SmoothLine_cartography(Dissolve, "%ScratchWorkspace%\\Bezier", "BEZIER_INTERPOLATION") arcpy.AddField_management(Bezier, "Start", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(Bezier, "End", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(Bezier, "Start", "0", "PYTHON_9.3", "") arcpy.CalculateField_management(Bezier, "End", "!Shape_Length!", "PYTHON_9.3", "") arcpy.AddMessage(" Converting in routes - 4/9") BezierRoutes = arcpy.CreateRoutes_lr(Bezier, "Rank_UGO", "%ScratchWorkspace%\\BezierRoutes", "TWO_FIELDS", "Start", "End") arcpy.AddMessage(" Up to date the \"Shape_Length\" field - 5/9") UPD_SL.UpToDateShapeLengthField(BezierRoutes) arcpy.AddMessage(" Locate inflection points on Bezier routes - 6/9") InflectionPtTABLE = arcpy.LocateFeaturesAlongRoutes_lr(PtsForSplitting, BezierRoutes, "Rank_UGO", "1", "%ScratchWorkspace%\\InflectionPtTABLE", "RID POINT MEAS") #/correction of the table : some errors have occured at confluences rows1 = arcpy.UpdateCursor(InflectionPtTABLE) rows2 = arcpy.UpdateCursor(InflectionPtTABLE) rows3 = arcpy.SearchCursor(BezierRoutes) line2 = next(rows2) nrows = int(str(arcpy.GetCount_management(InflectionPtTABLE))) # Each inflection point has a MEAS field (distance from upstream) n = 0 for line1 in rows1 : if n >= nrows-1:
if str(DeleteTF) == "true": nstep = 6 else: nstep = 5 ncurrentstep = 1 #=============================================================================== # CODING #=============================================================================== #/segmentation of the inFC with the inflection points arcpy.AddMessage( "Segmentation of the inFC according to the inflection points - Step " + str(ncurrentstep) + "/" + str(nstep)) CopyinFC = arcpy.CopyFeatures_management(inFC, "%ScratchWorkspace%\\CopyinFC") UPD_SL.UpToDateShapeLengthField(CopyinFC) arcpy.AddField_management(CopyinFC, "Start", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(CopyinFC, "End", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyinFC, "Start", "0", "PYTHON_9.3", "") arcpy.CalculateField_management(CopyinFC, "End", "!Shape_Length!", "PYTHON_9.3", "") Routes = arcpy.CreateRoutes_lr(CopyinFC, "Rank_UGO", "%ScratchWorkspace%\\Routes", "TWO_FIELDS", "Start", "End") UPD_SL.UpToDateShapeLengthField(Routes) LocateTABLE = arcpy.LocateFeaturesAlongRoutes_lr(
def main(Polygon, Polyline, DisaggregationStep, Smoothing, Output): # Allow the temporary outputs overwrite arcpy.env.overwriteOutput = True # Derived variable from inputs name = os.path.split(os.path.splitext(Polygon)[0])[1] # Number of steps nstep = 12 ncurrentstep = 1 #=============================================================================== # CODING #=============================================================================== #/creation of the extreme points arcpy.AddMessage( "Looking for the extreme points of the input polyline - Step " + str(ncurrentstep) + "/" + str(nstep)) ExtremePoints = arcpy.FeatureVerticesToPoints_management( Polyline, "in_memory\\ExtremePoints", "DANGLE" ) ### Simplified the method for finding Extreme Points to use line "dangles". This appears to have removed a bunch of extra extreme points found in the temp data. ### KMW: Removed this section, I do not fully understand how this functions, but it does not appear to break the process. # arcpy.AddXY_management(ExtremePoints) # arcpy.AddField_management(ExtremePoints, "Del", "SHORT") # ExtPts.ExtremePoints(ExtremePoints) # # Make = arcpy.MakeFeatureLayer_management(ExtremePoints, "in_memory\\Make") # Selection = arcpy.SelectLayerByAttribute_management(Make, "NEW_SELECTION", "\"Del\" = 1") # # arcpy.DeleteFeatures_management(Selection) ### #/splitting of the polygon with extreme points ncurrentstep += 1 arcpy.AddMessage("Converting the input polygon to line - Step " + str(ncurrentstep) + "/" + str(nstep)) PolyToLine = arcpy.FeatureToLine_management(Polygon, "in_memory\\PolyToLine", "", "ATTRIBUTES") ncurrentstep += 1 arcpy.AddMessage( "Looking for the longer distance between extreme points and the polygon - Step " + str(ncurrentstep) + "/" + str(nstep)) NearTable = arcpy.GenerateNearTable_analysis(ExtremePoints, PolyToLine, "in_memory\\NearTable", "", "LOCATION", "NO_ANGLE") NearPoints = arcpy.MakeXYEventLayer_management(NearTable, "NEAR_X", "NEAR_Y", "NearPoints", ExtremePoints) arcpy.CopyFeatures_management("NearPoints", "in_memory\\NearPoints") ### Removed this Section. It appears to find the max distance in the table, for use in splitting the lines? #rows = arcpy.SearchCursor(NearTable) # Counter = 0 # for row in rows : # if row.NEAR_DIST > Counter : # Counter = row.NEAR_DIST # Counter+=1 ### ncurrentstep += 1 arcpy.AddMessage("Splitting polygon with the extreme points - Step " + str(ncurrentstep) + "/" + str(nstep)) FracTEMP = arcpy.SplitLineAtPoint_management( PolyToLine, "NearPoints", "in_memory\\FracTEMP", "0.1 METERS" ) ### Changed to use near points for splitting, also added "0.1 METERS" search distance to solve an esri bug in this function. ncurrentstep += 1 arcpy.AddMessage("Deleting residual segments - Step " + str(ncurrentstep) + "/" + str(nstep)) FracTEMPToPoints = arcpy.FeatureVerticesToPoints_management( FracTEMP, "in_memory\\FracTEMPToPoints", "BOTH_ENDS") arcpy.AddField_management(FracTEMP, "Fusion", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") fieldnames = [f.name for f in arcpy.ListFields(FracTEMP)] arcpy.CalculateField_management(FracTEMP, "Fusion", "[" + fieldnames[0] + "]", "VB", "") SpatialRef = arcpy.Describe(Polygon).spatialReference XY = arcpy.MakeXYEventLayer_management(NearTable, "NEAR_X", "NEAR_Y", "in_memory\\XY", SpatialRef, "") NearTable2 = arcpy.GenerateNearTable_analysis(XY, FracTEMPToPoints, "in_memory\\NearTable2", "", "LOCATION", "NO_ANGLE", "ALL", "2") fieldnames = [f.name for f in arcpy.ListFields(FracTEMP)] arcpy.JoinField_management(FracTEMPToPoints, fieldnames[0], NearTable2, "NEAR_FID", ["NEAR_FID"]) MakeFracTEMPToPoints = arcpy.MakeFeatureLayer_management( FracTEMPToPoints, "in_memory\\MakeFracTEMPToPoints", "", "", "ORIG_FID ORIG_FID VISIBLE NONE") MakeFracTEMP = arcpy.MakeFeatureLayer_management( FracTEMP, "in_memory\\MakeFracTEMP", "", "", "ORIG_FID ORIG_FID VISIBLE NONE") SelectionPoints = arcpy.SelectLayerByAttribute_management( MakeFracTEMPToPoints, "NEW_SELECTION", "\"NEAR_FID\" IS NULL") SelectLine = arcpy.SelectLayerByLocation_management( MakeFracTEMP, "BOUNDARY_TOUCHES", SelectionPoints, "", "NEW_SELECTION") arcpy.CalculateField_management(SelectLine, "Fusion", "10000", "VB", "") FracPoly_TEMP = arcpy.Dissolve_management(FracTEMP, "in_memory\\FracPoly_TEMP", "Fusion", "", "MULTI_PART", "DISSOLVE_LINES") FracPoly = arcpy.MultipartToSinglepart_management(FracPoly_TEMP, "in_memory\\FracPoly") arcpy.DeleteField_management(FracPoly, "Fusion") ncurrentstep += 1 arcpy.AddMessage("Split the input polygon - Step " + str(ncurrentstep) + "/" + str(nstep)) PolySplitTEMP = dS.SLEM(FracPoly, DisaggregationStep, "in_memory\\PolySplitTEMP", "true") PolySplit = arcpy.Sort_management( PolySplitTEMP, "in_memory\\PolySplit", [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) ncurrentstep += 1 arcpy.AddMessage("Converting Split polygon to points - Step " + str(ncurrentstep) + "/" + str(nstep)) PolySplitToPoint = arcpy.FeatureToPoint_management( PolySplit, "in_memory\\PolySplitToPoint", "INSIDE") #/creating the Thiessen polygons and the centerline ncurrentstep += 1 arcpy.AddMessage("Creating Thiessen polygons - Step " + str(ncurrentstep) + "/" + str(nstep)) ThiessenPoly = arcpy.CreateThiessenPolygons_analysis( PolySplitToPoint, "in_memory\\ThiessenPoly", "ALL") JoinTEMP = arcpy.SpatialJoin_analysis( ThiessenPoly, PolySplitToPoint, "in_memory\\JoinTEMP", "JOIN_ONE_TO_ONE", "KEEP_ALL", "Rank_UGO \"Rank_UGO\" true true false 4 Long 0 0 ,First,#, in_memory\\PolySplitToPoint,Rank_UGO,-1,-1; Distance \"Distance\" true true false 4 Long 0 0 ,First,#,in_memory\\PolySplitToPoint,Distance,-1,-1", "INTERSECT", "", "") Join = arcpy.Sort_management( JoinTEMP, "in_memory\\Join", [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) ncurrentstep += 1 arcpy.AddMessage("Merging Thiessen polygons - Step " + str(ncurrentstep) + "/" + str(nstep)) Dissolve1 = arcpy.Dissolve_management(Join, "in_memory\\Dissolve1", "Rank_UGO", "", "MULTI_PART", "DISSOLVE_LINES") ncurrentstep += 1 arcpy.AddMessage("Finalizing the centerline - Step " + str(ncurrentstep) + "/" + str(nstep)) Dissolve1ToLine = arcpy.Intersect_analysis([Dissolve1, Dissolve1], "in_memory\\Dissolve1ToLine", "", "", "LINE") UPD_SL.UpToDateShapeLengthField(Dissolve1ToLine) arcpy.DeleteIdentical_management(Dissolve1ToLine, ["Shape_Length"]) RawCenterline = arcpy.Intersect_analysis([Dissolve1ToLine, Polygon], "in_memory\\RawCenterline", "ALL", "", "INPUT") ncurrentstep += 1 arcpy.AddMessage("Smoothing centerline - Step " + str(ncurrentstep) + "/" + str(nstep)) Centerline = arcpy.SmoothLine_cartography(RawCenterline, Output, "PAEK", Smoothing, "FIXED_CLOSED_ENDPOINT", "NO_CHECK") #/deleting residual fields try: arcpy.DeleteField_management(Centerline, [ "FID_Dissolve1ToLine", "FID_Dissolve1", "FID_Dissolve1_1", "ORIG_FID", "Rank_UGO", "Rank_UGO_1", "FID_" + str(name) ]) except: pass try: arcpy.DeleteField_management(Centerline, [ "FID_Dissol", "FID_Diss_1", "FID_Diss_2", "FID_" + str(name)[0:6], "ORIG_FID", "Rank_UGO", "Rank_UGO_1", "Shape_Leng", "Shape_Le_1" ]) except: pass #=============================================================================== # DELETING TEMPORARY FILES #=============================================================================== ncurrentstep += 1 arcpy.AddMessage("Deleting temporary files - Step " + str(ncurrentstep) + "/" + str(nstep)) ClearInMemory.main() return # main(Polygon,Polyline,DisaggregationStep,Smoothing,Output)
def SLEM(Line, Distance, Output, TF): CopyLine = arcpy.CopyFeatures_management(Line, r"in_memory\CopyLine") fieldnames = [f.name for f in arcpy.ListFields(CopyLine)] #/identification of the polyline type : raw, UGOs, sequenced UGOs, or AGOs k = 0 if "Rank_AGO" in fieldnames : k = 3 elif "Order_ID" in fieldnames : k = 2 elif "Rank_UGO" in fieldnames : k = 1 ################################ ########## Raw polyline ######## ################################ if k == 0 : #/shaping of the segmented result arcpy.AddField_management(CopyLine, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "Rank_UGO", "!"+fieldnames[0]+"!", "PYTHON_9.3", "") arcpy.AddField_management(CopyLine, "From_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "From_Measure", "0", "PYTHON_9.3", "") arcpy.AddField_management(CopyLine, "To_Measure", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(CopyLine, "To_Measure", "!shape.length!", "PYTHON_9.3", "") #/conversion in routes LineRoutes = arcpy.CreateRoutes_lr(CopyLine, "Rank_UGO", r"in_memory\LineRoutes", "TWO_FIELDS", "From_Measure", "To_Measure") #/creation of the event table PointEventTEMP = arcpy.CreateTable_management("in_memory", "PointEventTEMP", "", "") arcpy.AddField_management(PointEventTEMP, "Rank_UGO", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "Distance", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(PointEventTEMP, "To_M", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(LineRoutes) rowslines = arcpy.SearchCursor(LineRoutes) rowsevents = arcpy.InsertCursor(PointEventTEMP) for line in rowslines: tempdistance = float(line.Shape_Length) while (tempdistance > float(0)): row = rowsevents.newRow() row.Rank_UGO = line.Rank_UGO row.To_M = max(0, tempdistance - float(Distance)) row.Distance = tempdistance rowsevents.insertRow(row) tempdistance = tempdistance - float(Distance) del rowslines del rowsevents #/creation of the route event layer MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(LineRoutes, "Rank_UGO", PointEventTEMP, "Rank_UGO LINE Distance To_M", r"in_memory\MakeRouteEventTEMP") Split = arcpy.CopyFeatures_management(MakeRouteEventTEMP, r"in_memory\Split", "", "0", "0", "0") Sort = arcpy.Sort_management(Split, Output, [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) arcpy.DeleteField_management(Sort, "To_M") #/calculation of the "Distance" field UPD_SL.UpToDateShapeLengthField(Sort) rows1 = arcpy.UpdateCursor(Sort) rows2 = arcpy.UpdateCursor(Sort) line2 = rows2.next() line2.Distance = 0 rows2.updateRow(line2) nrows = int(str(arcpy.GetCount_management(Sort))) n = 0 for line1 in rows1 : line2 = rows2.next() if n == nrows-1 : break if n == 0 : line1.Distance = 0 if line2.Rank_UGO == line1.Rank_UGO : line2.Distance = line1.Distance + line1.Shape_Length rows2.updateRow(line2) if line2.Rank_UGO != line1.Rank_UGO : line2.Distance = 0 rows2.updateRow(line2) n+=1 #/deleting of the temporary files if str(TF) == "true" : arcpy.Delete_management(Split) arcpy.Delete_management(CopyLine) arcpy.Delete_management(LineRoutes) arcpy.Delete_management(PointEventTEMP) return Sort