def placeEvents(inRoutes, idRteFld, eventTable, eventRteFld, fromVar, toVar, eventLay): #place events along routes, creates a temporary layer try: arcpy.AddMessage('Placing line events on ' + inRoutes) props = eventRteFld + ' LINE ' + fromVar + ' ' + toVar #this makes an event layer where the table includes all records even if they were not located #on any routes arcpy.MakeRouteEventLayer_lr(inRoutes, idRteFld, eventTable, props, 'lyr') arcpy.AddMessage(eventTable + ' written to temporary memory') #one way to select only events that were located on a valid route is to #save the layer to a feature class, arcpy.CopyFeatures_management('lyr', 'lyr2') #...create an in-memory layer based on that feature class and limited to where #Shape_Length <> 0, arcpy.MakeFeatureLayer_management('lyr2', 'lyr3', "Shape_Length <> 0") #make a new selection #arcpy.SelectLayerByAttribute_management('lyr2', "NEW_SELECTION", "Shape_Length <> 0") arcpy.AddMessage('Saving temp layer to permanent memory') #...and copy to the output feature class arcpy.CopyFeatures_management('lyr3', eventLay) arcpy.AddMessage(eventLay + ' written to ' + arcpy.env.scratchWorkspace) except: tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = tbinfo + '\n' + str(sys.exc_type)+ ': ' + str(sys.exc_value) arcpy.AddError(pymsg) raise SystemError
def create_merki(): arcpy.MakeRouteEventLayer_lr(OutputBraut2dnafn, "BRAUT", InputFileTPK, "NAFN POINT lengd", "merki_events", "", "NO_ERROR_FIELD", "ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.CopyFeatures_management("merki_events", OutputMerki) arcpy.Delete_management("merki_events")
def CreateRouteEventLayer(Sites_Routes, AttTable, RouteID, BMP, EMP, Fields, Output): IRIS_Diss = common.CreateOutPath(MainFile=Output, appendix='diss', Extension='') arcpy.DissolveRouteEvents_lr( in_events=AttTable, in_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), dissolve_field=';'.join(Fields), out_table=IRIS_Diss, out_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), dissolve_type="DISSOLVE", build_index="INDEX") Overlay_Event_Layer = common.CreateOutLayer('OverlayEventLayer') arcpy.MakeRouteEventLayer_lr(in_routes=Sites_Routes, route_id_field=RouteID, in_table=IRIS_Diss, in_event_properties=' '.join( [RouteID, 'LINE', BMP, EMP]), out_layer=Overlay_Event_Layer, offset_field="", add_error_field="ERROR_FIELD") Sort = common.CreateOutPath(MainFile=Output, appendix='sort', Extension='') arcpy.Sort_management(in_dataset=Overlay_Event_Layer, out_dataset=Sort, sort_field=';'.join([RouteID, BMP, EMP])) Final_Layer = common.CreateOutLayer('FinalLayer') arcpy.MakeFeatureLayer_management(in_features=Sort, out_layer=Final_Layer) arcpy.SelectLayerByAttribute_management(in_layer_or_view=Final_Layer, selection_type='NEW_SELECTION', where_clause="Shape_Length > 0") arcpy.Delete_management(Output) arcpy.CopyFeatures_management(in_features=Final_Layer, out_feature_class=Output) arcpy.Delete_management(IRIS_Diss) arcpy.Delete_management(Overlay_Event_Layer) arcpy.Delete_management(Sort) arcpy.Delete_management(Final_Layer)
MassDOT_route_query_string = "route_id = " + "'" + route_id + "'" normalized_route_id = route_id.replace(' ', '_') sl_et_name = normalized_route_id + '_sl_events' sl_layer_name = normalized_route_id + '_sl_layer' sl_fc_name = normalized_route_id + '_sl_fc' nl_et_name = normalized_route_id + '_nl_events' nl_layer_name = normalized_route_id + '_nl_layer' nl_fc_name = normalized_route_id + '_nl_fc' arcpy.AddMessage(' Generating speed limit FC.') arcpy.SelectLayerByAttribute_management(Speed_Limit_Layer, "NEW_SELECTION", MassDOT_route_query_string) arcpy.TableToTable_conversion("Speed_Limit_Layer", speed_limit_events_gdb, sl_et_name) arcpy.MakeRouteEventLayer_lr(MASSDOT_LRSN_Routes_19Dec2019, "route_id", speed_limit_events_gdb + '\\' + sl_et_name, "route_id LINE from_measure to_measure", sl_layer_name) arcpy.CopyFeatures_management(sl_layer_name, speed_limit_gdb + '\\' + sl_fc_name) arcpy.AddMessage(' Generating number of travel lanes FC.') arcpy.SelectLayerByAttribute_management(Num_Lanes_Layer, "NEW_SELECTION", MassDOT_route_query_string) arcpy.TableToTable_conversion("Num_Lanes_Layer", num_lanes_events_gdb, nl_et_name) arcpy.MakeRouteEventLayer_lr(MASSDOT_LRSN_Routes_19Dec2019, "route_id", num_lanes_events_gdb + '\\' + nl_et_name, "route_id LINE from_measure to_measure", nl_layer_name) arcpy.CopyFeatures_management(nl_layer_name, num_lanes_gdb + '\\' + nl_fc_name)
mxd = arcpy.mapping.MapDocument( r"\\GISDATA\ArcGIS\GISDATA\MXD\NewGISNetworkSeed.mxd") arcpy.MakeFeatureLayer_management(ws + "\\" + tempgdb + "\\CRND", "CRND") arcpy.MakeFeatureLayer_management(ws + "\\" + tempgdb + "\\SRND", "SRND") arcpy.MakeTableView_management(ws + "\\" + tempgdb + "\\MAP_EXTRACT", "MAP_EXTRACT", "DIRECTION <=2") arcpy.AddField_management(ws + "\\" + tempgdb + "/MAP_EXTRACT", "SRND", "TEXT", "#", "#", "24") arcpy.CalculateField_management(ws + "\\" + tempgdb + "/MAP_EXTRACT", "SRND", """Mid([NQR_DESCRIPTION],4,16)""", "VB", "#") arcpy.AddField_management(ws + "\\" + tempgdb + "/MAP_EXTRACT", "CRND", "TEXT", "#", "#", "24") arcpy.CalculateField_management(ws + "\\" + tempgdb + "/MAP_EXTRACT", "CRND", """[NQR_DESCRIPTION]""", "VB", "#") arcpy.MakeRouteEventLayer_lr( "CRND", "NE_UNIQUE", ws + "\\" + tempgdb + "/MAP_EXTRACT", "NQR_DESCRIPTION LINE BEG_CNTY_LOGMILE END_CNTY_LOGMILE", "STATE_SYSTEM", "#", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.FeatureClassToFeatureClass_conversion("STATE_SYSTEM", ws + "\\" + tempgdb, "STATE_SYSTEM", '"DIRECTION" <3') #9 sec arcpy.AddField_management(ws + "\\" + tempgdb + "\\STATE_SYSTEM", "NETWORKDATE", "DATE") arcpy.CalculateField_management(ws + "\\" + tempgdb + "\\STATE_SYSTEM", "NETWORKDATE", "datetime.datetime.now( )", "PYTHON_9.3", "#") arcpy.MakeFeatureLayer_management(ws + "\\" + tempgdb + "\\STATE_SYSTEM", "STATE_SYSTEM") import LRS_CRND_SRND import LRS_PRECISION_SDO
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
r"\\gisdata\arcgis\GISdata\Layers\County Linear Reference System.lyr") arcpy.mapping.AddLayer(df, clim) arcpy.mapping.AddLayer(df, countylrs) clrs = "County Linear Reference System" smlrs = r"Database Connections\SDEDEV_GISDEV.sde\GIS_DEV.KDOT_ROADWAY\GIS.SMLRS" arcpy.LocateFeaturesAlongRoutes_lr( "City Limits", "County Linear Reference System", "LRS_KEY", "0 Feet", r"Database Connections\SDEDEV_GISDEV.sde\GIS_DEV.CCL_Extents", "LRS_KEY LINE Beg_CMP End_CMP", "FIRST", "DISTANCE", "NO_ZERO", "FIELDS", "M_DIRECTON") arcpy.MakeRouteEventLayer_lr(clrs, "LRS_KEY", "Lane_tview", "LRSKEY LINE BEGMILEPOST ENDMILEPOST", "Lane_Events_ln", "#", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.MakeRouteEventLayer_lr(clrs, "LRS_KEY", "Maint_tview", "LRSKEY LINE BEGMILEPOST END_MP", "Maint_Events_ln", "#", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.MakeRouteEventLayer_lr(clrs, "LRS_KEY", "GIS_DEV.CCL_Extents", "LRS_KEY LINE BEG_CMP END_CMP", "GIS_CCL", "#", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.OverlayRouteEvents_lr( devpath + "GIS_DEV.CCL_Resolution",
def crashReportLRS(GDBspot, csv, fatalwt, seriouswt, nonseriouswt, possiblewt, IntersectionThreshold, SegmentThreshold): # workspace= "Z:/fullerm/Safety Locations/Safety.gdb" # Input parameters # GCAT file/location GCATfile = csv # csv created after mapping fields with schemaCleaner # Intermediate file/location? # Intersection polygon file/location # IntersectionFeatures = arcpy.GetParameterAsText(1) # IntersectionFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/Fed_Aid_2010_LucWoo_Intersection_Buffer_Dissolve" IntersectionFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/LMW_intersection_250ft_buffer_5Jul2017_3857" psFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/CS_IP_Merge_copy_clip_16june2017_LMW_3857" psThreshold = str(0) countyFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/County_FLOWHHMS_Clipped_3857" # Segment polygon file/location # SegmentFeatures = arcpy.GetParameterAsText(2) SegmentFeatures = "Z:/fullerm/Safety Locations/Crash_Report_Script_Tool.gdb/LMW_segments_70ft_buffer_5Jul2017_3857" # output file name/location for the spatial join # GDBspot = arcpy.GetParameterAsText(1) # user input location for gdb and result excel tables # psThreshold = arcpy.GetParameterAsText(8) # output file name/location for excel table # TableFolder = arcpy.GetParameterAsText(4) # rdinv = arcpy.GetParameterAsText(8) rdinv = "C:/Users/fullerm/Documents/ArcGIS/Projects/Safety Report Script/Safety Report Script.gdb/Road_Inventory_CopyFeatures" # create geodatabase TimeDate = datetime.now() TimeDateStr = "CrashLocations" + TimeDate.strftime('%Y%m%d%H%M') + "_LRS" outputGDB = arcpy.CreateFileGDB_management(GDBspot, TimeDateStr) arcpy.env.workspace = str(outputGDB).replace('/', '\\') # I kept getting errors because arcmap sets the field type based on first dozen features and some ids were numeric '''fldmppng = arcpy.FieldMappings() fldmppng.addTable(GCATfile) nameFI = fldmppng.findFieldMapIndex("LOCAL_REPORT_NUMBER_ID") fldmp = fldmppng.getFieldMap(nameFI) fld = fldmp.outputField fld.name = "LOCAL_REPORT_NUMBER_ID" fld.aliasName = "LOCAL_REPORT_NUMBER_ID" fld.type = "String" fldmp.outputField = fld fldmppng.replaceFieldMap(nameFI,fldmp)''' # convert GCAT txt file to gdb table and add to map NewTable = arcpy.TableToTable_conversion(GCATfile, outputGDB, "OHMI_data") arcpy.TableSelect_analysis(NewTable, "oh_table", '"NLF_COUNTY_CD" <> \'Monroe\' ') ohTable = arcpy.CopyRows_management("oh_table", "ohTable") arcpy.TableSelect_analysis(NewTable, "mi_table", '"NLF_COUNTY_CD" = \'Monroe\' ') miTable = arcpy.CopyRows_management('mi_table', 'miTable') # arcpy.SelectLayerByAttribute_management(NewTable,"CLEAR_SELECTION") rdlyr = arcpy.MakeFeatureLayer_management(rdinv, "rdlyr") rtloc = os.path.join( GDBspot, "Road_Inventory3456_CreateRoutes" + TimeDateStr + ".shp") lrs = arcpy.CreateRoutes_lr(rdlyr, "NLF_ID", rtloc, "TWO_FIELDS", "CTL_BEGIN", "CTL_END") event_props = "NLFID POINT COUNTY_LOG_NBR" PointFile = arcpy.MakeRouteEventLayer_lr(lrs, "NLF_ID", ohTable, event_props, "Crash_Events") # creating this extra feature class and working from it instead of the event layer # decreased script tool runtime from ~8 min to ~2 min arcpy.SelectLayerByAttribute_management(PointFile, "clear_selection") pointOH = arcpy.FeatureClassToFeatureClass_conversion( PointFile, outputGDB, "GCAT_LUCWOO_lrs_points_" + TimeDateStr) # pointOH = arcpy.CopyFeatures_management(PointFile, "LRS_Events_copy") mi_points = milocationsxy(miTable, outputGDB) pointcopy = arcpy.Merge_management([pointOH, mi_points], 'miohpointsmerge') dict = { 'fatalities_count': "ODPS_TOTAL_FATALITIES_NBR<>0", 'incapac_inj_count': "Incapac_injuries_NBR<>0 and ODPS_TOTAL_FATALITIES_NBR=0", 'non_incapac_inj_count': "non_incapac_injuries_NBR<>0 and ODPS_TOTAL_FATALITIES_NBR=0 and incapac_injuries_nbr=0", 'possible_inj_count': "possible_injuries_nbr<>0 and ODPS_TOTAL_FATALITIES_NBR=0 and non_incapac_injuries_nbr=0 and incapac_injuries_nbr=0" } fld_lst = [ 'SEVERITY_BY_TYPE_CD', 'fatalities_count', 'incapac_inj_count', 'non_incapac_inj_count', 'possible_inj_count' ] # add fields for point layer for key in dict: arcpy.AddField_management(pointcopy, key, "LONG") '''arcpy.SelectLayerByAttribute_management(PointFile, "NEW_SELECTION", dict[key]) arcpy.CalculateField_management(PointFile, key, 1) arcpy.SelectLayerByAttribute_management(PointFile, "Switch_selection") arcpy.CalculateField_management(PointFile, key, 0)''' # fillCountFields(pointcopy, fld_lst) with arcpy.da.UpdateCursor(pointcopy, fld_lst) as cursor: for row in cursor: if row[0] == 'Fatal Crashes': row[1] = 1 row[2] = 0 row[3] = 0 row[4] = 0 elif row[0] == 'Incapacitating Injury Crashes': row[1] = 0 row[2] = 1 row[3] = 0 row[4] = 0 elif row[0] == 'Non-Incapacitating Injury Crashes': row[1] = 0 row[2] = 0 row[3] = 1 row[4] = 0 elif row[0] == 'Possible Injury Crashes': row[1] = 0 row[2] = 0 row[3] = 0 row[4] = 1 else: row[1] = 0 row[2] = 0 row[3] = 0 row[4] = 0 cursor.updateRow(row) # Clear Selected Features arcpy.SelectLayerByAttribute_management(PointFile, "clear_selection") # PointFeatures2 = arcpy.CopyFeatures_management(PointFeatures,os.path.join(GDBspot, TimeDateStr + ".gdb\PointFeatures2")) PointFeatures = arcpy.FeatureClassToFeatureClass_conversion( pointcopy, outputGDB, "ohmi_points_copy" + TimeDateStr) ftype = { 'Intersection': [IntersectionThreshold, IntersectionFeatures], 'Segment': [SegmentThreshold, SegmentFeatures], 'Subdivision': [psThreshold, psFeatures] } # field map and merge rules attchmnt = [] writer = pandas.ExcelWriter(os.path.join(GDBspot, "Top_Locations.xlsx"), engine='xlsxwriter') for f in ftype: # Create a new fieldmappings and add the two input feature classes. fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(ftype[f][1]) fieldmappings.addTable(PointFeatures) # First get the fieldmaps. POP1990 is a field in the cities feature class. # The output will have the states with the attributes of the cities. Setting the # field's merge rule to mean will aggregate the values for all of the cities for # each state into an average value. The field is also renamed to be more appropriate # for the output. addSumFlds(fieldmappings) # Run the Spatial Join tool, using the defaults for the join operation and join type loc = os.path.join(GDBspot, TimeDateStr + ".gdb\\" + f + "Join_LRS") Join = arcpy.SpatialJoin_analysis(ftype[f][1], PointFeatures, loc, "Join_one_to_one", "keep_all", fieldmappings) arcpy.AddField_management(Join, "PDO_", "LONG") arcpy.AddField_management(Join, "EPDO_Index", "DOUBLE") # CRLRS_EPDO_index(Join) CursorFlds = [ 'PDO_', 'EPDO_Index', 'Join_Count', 'sum_fatalities_count', 'sum_incapac_inj_count', 'sum_non_incapac_inj_count', 'sum_possible_inj_count' ] # determine PDO and EPDO Index/Rate with arcpy.da.UpdateCursor(Join, CursorFlds) as cursor: for row in cursor: try: row[0] = row[2] - int(row[3]) - int(row[4]) - int( row[5]) - int(row[6]) except: row[0] = 0 # null or divide by zero are the major exceptions we are handling here try: row[1] = (float(row[3]) * fatalwt + float(row[4]) * seriouswt + float(row[5]) * nonseriouswt + float(row[6]) * possiblewt + float(row[0])) / float(row[2]) except: row[1] = 0 # null or divide by zero are the major exceptions we are handling here cursor.updateRow(row) # delete unnecessary fields keepFlds = [ 'OBJECTID', 'Shape', 'Shape_Area', 'Shape_Length', 'Name', 'NAMELSAD', 'COUNTY', 'COUNTY_NME', 'Join_Count', 'sum_fatalities_count', 'sum_incapac_inj_count', 'sum_non_incapac_inj_count', 'sum_possible_inj_count', 'PDO_', 'EPDO_Index', 'Fed_Aid_Buffer_Segments_2_Name', 'Length_ft', 'County' ] # lstFlds = arcpy.ListFields(Join) dropFlds = [ x.name for x in arcpy.ListFields(Join) if x.name not in keepFlds ] # delete fields arcpy.DeleteField_management(Join, dropFlds) # select high crash locations JoinLayer = arcpy.MakeFeatureLayer_management( Join, os.path.join(GDBspot, TimeDateStr + ".gdb\\" + f + "JoinLayer")) arcpy.AddMessage("{}".format(type(JoinLayer))) # arcpy.SelectLayerByAttribute_management(JoinLayer, "NEW_SELECTION", "Join_Count >=" + ftype[f][0]) fld_nmes = [fld.name for fld in arcpy.ListFields(JoinLayer)] fld_nmes.remove( 'Shape' ) # I think this field kept causing an exception: Data must be 1 dimensional arcpy.AddMessage("{}".format(fld_nmes)) arcpy.AddMessage("{}".format( type( os.path.join(GDBspot, TimeDateStr + ".gdb\\" + f + "JoinLayer")))) # do this because political sud # fields can be list or tuple, list works when 'Shape' field removed n = arcpy.da.FeatureClassToNumPyArray(JoinLayer, fld_nmes, where_clause="Join_Count >=" + ftype[f][0], skip_nulls=False, null_value=0) df = pandas.DataFrame(n) CRLRS_excel_export(df, f, writer) writer.save() return os.path.join(GDBspot, "Top_Locations.xlsx")
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
del insertCursor, currentStation # Create Route(s) lyr and define events along each route AddMsgAndPrint("\nCreating Stations") routes = arcpy.CreateScratchName("routes", data_type="FeatureClass", workspace="in_memory") arcpy.CreateRoutes_lr(lineTemp, "ID", routes, "TWO_FIELDS", "FROM_PT", "LENGTH_FT", "UPPER_LEFT", "1", "0", "IGNORE", "INDEX") stationEvents = arcpy.CreateScratchName("stationEvents", data_type="FeatureClass", workspace="in_memory") arcpy.MakeRouteEventLayer_lr(routes, "ID", stationTable, "ID POINT STATION", stationEvents, "", "NO_ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.AddField_management(stationEvents, "STATIONID", "TEXT", "", "", "25", "", "NULLABLE", "NON_REQUIRED") arcpy.CalculateField_management(stationEvents, "STATIONID", "str(!STATION!) + '_' + str(!ID!)", "PYTHON3") stationTemp = watershedFD_path + os.sep + "stations" arcpy.CopyFeatures_management(stationEvents, stationTemp) arcpy.AddXY_management(stationTemp) arcpy.AddField_management(stationTemp, "POINT_Z", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.MakeFeatureLayer_management(stationTemp, stationLyr)
def ImportRoadwayData(Input, Route, AttTable, Fields, Output, RouteID, BMP, EMP, XY_Tolerance): #Output should be on a GDB not a shapefile #Step 1: Create a route FC based on the input Sites_Event_Table = common.CreateOutPath(MainFile=Output, appendix='EventTab', Extension='') arcpy.LocateFeaturesAlongRoutes_lr(in_features=Input, in_routes=Route, route_id_field=RouteID, radius_or_tolerance=XY_Tolerance, out_table=Sites_Event_Table, out_event_properties=' '.join( [RouteID, 'LINE', BMP, EMP]), route_locations="FIRST", distance_field="DISTANCE", zero_length_events="ZERO", in_fields="FIELDS", m_direction_offsetting="M_DIRECTON") Sites_Event_Layer = common.CreateOutLayer('EventLayer') arcpy.MakeRouteEventLayer_lr(in_routes=Route, route_id_field=RouteID, in_table=Sites_Event_Table, in_event_properties=' '.join( [RouteID, 'LINE', BMP, EMP]), out_layer=Sites_Event_Layer, add_error_field="NO_ERROR_FIELD") Sites_Routes = common.CreateOutPath(MainFile=Output, appendix='route', Extension='') arcpy.CopyFeatures_management(in_features=Sites_Event_Layer, out_feature_class=Sites_Routes) IRIS_Diss = common.CreateOutPath(MainFile=Output, appendix='diss', Extension='') arcpy.DissolveRouteEvents_lr( in_events=AttTable, in_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), dissolve_field=';'.join(Fields), out_table=IRIS_Diss, out_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), dissolve_type="DISSOLVE", build_index="INDEX") Overlay_Event_Table1 = common.CreateOutPath(MainFile=Output, appendix='OverlayTab1', Extension='') arcpy.OverlayRouteEvents_lr( in_table=IRIS_Diss, in_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), overlay_table=Sites_Event_Table, overlay_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), overlay_type="INTERSECT", out_table=Overlay_Event_Table1, out_event_properties=' '.join([RouteID, 'LINE', BMP, EMP]), zero_length_events="NO_ZERO", in_fields="FIELDS", build_index="INDEX") Overlay_Event_Layer = common.CreateOutLayer('OverlayEventLayer') arcpy.MakeRouteEventLayer_lr(in_routes=Route, route_id_field=RouteID, in_table=Overlay_Event_Table1, in_event_properties=' '.join( [RouteID, 'LINE', BMP, EMP]), out_layer=Overlay_Event_Layer, offset_field="", add_error_field="ERROR_FIELD") Sites_segs1 = common.CreateOutPath(MainFile=Output, appendix='seg1', Extension='') arcpy.CopyFeatures_management(in_features=Overlay_Event_Layer, out_feature_class=Sites_segs1) #Curves_Table = common.CreateOutPath(MainFile=Output,appendix='curves',Extension='') #ExtractCurves(inp=Sites_segs1,IDField=RouteID,RMax=5280,RMin=10,DegMin=2,desd=1000,LenMin=1000,out=Curves_Table) #Overlay_Event_Table2 = common.CreateOutPath(MainFile=Output,appendix='OverlayTab2',Extension='') #arcpy.OverlayRouteEvents_lr(in_table = Overlay_Event_Table1, # in_event_properties = ' '.join([RouteID,'LINE',BMP,EMP]), # overlay_table = Curves_Table, # overlay_event_properties = ' '.join([RouteID,'LINE',BMP,EMP]), # overlay_type = "UNION", # out_table = Overlay_Event_Table2, # out_event_properties = ' '.join([RouteID,'LINE',BMP,EMP]), # zero_length_events = "NO_ZERO", # in_fields = "FIELDS", # build_index="INDEX") #Overlay_Event_Layer2 = common.CreateOutLayer('OverlayEventLayer2') #arcpy.MakeRouteEventLayer_lr(in_routes = Route, # route_id_field = RouteID, # in_table = Overlay_Event_Table2, # in_event_properties = ' '.join([RouteID,'LINE',BMP,EMP]), # out_layer = Overlay_Event_Layer2, # offset_field = "", # add_error_field = "ERROR_FIELD") Sort = common.CreateOutPath(MainFile=Output, appendix='sort', Extension='') arcpy.Sort_management(in_dataset=Sites_segs1, out_dataset=Sort, sort_field=';'.join([RouteID, BMP, EMP])) Final_Layer = common.CreateOutLayer('FinalLayer') arcpy.MakeFeatureLayer_management(in_features=Sort, out_layer=Final_Layer) arcpy.SelectLayerByAttribute_management(in_layer_or_view=Final_Layer, selection_type='NEW_SELECTION', where_clause="Shape_Length > 52") arcpy.Delete_management(Output) arcpy.MultipartToSinglepart_management(in_features=Final_Layer, out_feature_class=Output) arcpy.DeleteField_management(Output, 'ORIG_FID') FL = [ f.name for f in arcpy.ListFields(Output) if f.name != arcpy.Describe(Output).OIDFieldName ] arcpy.DeleteIdentical_management(in_dataset=Output, fields=';'.join(FL), xy_tolerance="", z_tolerance="0") arcpy.Delete_management(Sites_Event_Table) arcpy.Delete_management(Sites_Event_Layer) arcpy.Delete_management(Sites_Routes) arcpy.Delete_management(IRIS_Diss) arcpy.Delete_management(Overlay_Event_Table1) arcpy.Delete_management(Overlay_Event_Layer) arcpy.Delete_management(Sites_segs1) #arcpy.Delete_management(Curves_Table) #arcpy.Delete_management(Overlay_Event_Table2) #arcpy.Delete_management(Overlay_Event_Layer2) arcpy.Delete_management(Sort) arcpy.Delete_management(Final_Layer)
addMsgAndPrint(' ' + inFC) arcpy.env.workspace = wsName(lineFC) if inFC == 'ContactsAndFaults': lineCrossingLength = -lineCrossingLength # intersect inFC with ZMline to get points where arcs cross section line linePts = scratch + '/xxxLinePts' + outFdsTag arcpy.Intersect_analysis([inFC, ZMline], linePts, 'ALL', '#', 'POINT') if numberOfRows(linePts) == 0: addMsgAndPrint(' ' + inFC + ' does not intersect section line') else: # numberOfRows > 0 eventProperties = 'rtID POINT M fmp' eventTable = locateEventTable(gdb, inFC, linePts, dem, 10, eventProperties, 'Z_MEAN', True) addMsgAndPrint(' placing events on section line') eventLyr = 'xxxLineEvents' arcpy.MakeRouteEventLayer_lr(ZMline, idField, eventTable, eventProperties, eventLyr) outFC = 'ed_CS' + outFdsTag + shortName(inFC) addMsgAndPrint(' creating feature class ' + outFC + ' in ' + shortName(outFds)) # make new feature class using old as template testAndDelete(outFds + '/' + outFC) arcpy.CreateFeatureclass_management(outFds, outFC, 'POLYLINE', inFC, 'DISABLED', 'SAME_AS_TEMPLATE') outFC = outFds + '/' + outFC addMsgAndPrint(' moving and calculating attributes') ## open search cursor on inFC, open insert cursor on outFC inRows = arcpy.SearchCursor(eventLyr) outRows = arcpy.InsertCursor(outFC) # get field names inFieldNames = fieldNameList(eventLyr) outFieldNames = fieldNameList(outFC)
def create_cond_events(workspace, condition, condlec_nodes_fc, condition_field, routes_fc, routes_id_field, point_search_meters, clean_up_temp_files): """ :param workspace: :param condition: :param condlec_nodes_fc: :param condition_field: :param routes_fc: :param routes_id_field: :param point_search_meters: :param clean_up_temp_files: :return: """ # STEP 0: Set-up # Take the user parameter for search radius and append linear unit measurement for string search_distance = "{0} Meters".format(str(point_search_meters)) # Create workspace file geodatabase timestamp = '{:%Y%m%d_%H%M}'.format(datetime.datetime.now()) workspace_gdb_name = "ConditionPostProcessing_{0}".format(timestamp) arcpy.AddMessage("Creating workspace file geodatabase '{0}'...".format( workspace_gdb_name)) workspace_gdb = arcpy.CreateFileGDB_management( workspace, workspace_gdb_name).getOutput(0) os.chdir(workspace) if os.path.isfile(os.path.join(workspace, "work_routes_lyr")): arcpy.AddMessage("Removing previously-existing layers...") os.remove(os.path.join(workspace, "work_routes_lyr")) work_event_table = os.path.join(workspace_gdb, "work_cond_events_table") out_condition_event_fc = os.path.join(workspace_gdb, "out_{0}_events".format(condition)) # STEP 1: Create copy of our routes and condition points so we can operate on them for the splitting by point step. arcpy.AddMessage("Copying data to workspace...") work_nodes_fc = arcpy.FeatureClassToFeatureClass_conversion( condlec_nodes_fc, workspace_gdb, "work_condition_nodes").getOutput(0) work_routes_fc = arcpy.FeatureClassToFeatureClass_conversion( routes_fc, workspace_gdb, "work_routes").getOutput(0) work_routes_lyr = arcpy.MakeFeatureLayer_management( routes_fc, "work_routes_lyr").getOutput(0) # STEP 2: Perform Select-by-Location using the input points on the routes copy - the idea is to select only the # lines that were evaluated. Export this selection to disk as "Evaluated_Routes". arcpy.AddMessage("Finding evaluated roads...") arcpy.SelectLayerByLocation_management(work_routes_lyr, "INTERSECT", work_nodes_fc, search_distance) work_routes_evaluated = arcpy.CopyFeatures_management( work_routes_lyr, os.path.join(workspace_gdb, "work_evaluated_routes")).getOutput(0) # STEP 3: Use Split Line by Point to fracture the Routes into lines that are determined by the points entered. # Make sure to enter a search area. arcpy.AddMessage("Creating condition segments...") #TODO - Use the near tool to find the nearest point on the line instead of a search radius for "SplitLineAtPoint" work_route_evaluated_segments = arcpy.SplitLineAtPoint_management( in_features=work_routes_evaluated, point_features=work_nodes_fc, out_feature_class=os.path.join(workspace_gdb, "work_evaluated_route_segments"), search_radius=search_distance).getOutput(0) # STEP 4: Transfer the condition from the points to the line segments by using spatial join arcpy.AddMessage( "Transferring condition attributes to condition route segments...") arcpy.SpatialJoin_analysis(target_features=work_route_evaluated_segments, join_features=work_nodes_fc, out_feature_class=os.path.join( workspace_gdb, "work_evaluated_route_segment_conditions"), join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_ALL", match_option="INTERSECT", search_radius=search_distance, distance_field_name="closest_node_distance") work_evaluated_route_segment_conditions = os.path.join( workspace_gdb, "work_evaluated_route_segment_conditions") # STEP 5: Correct for the first line segment by selecting it and changing the condition rating to "Excellent" arcpy.AddMessage("Adding default start segment condition...") with arcpy.da.UpdateCursor(work_evaluated_route_segment_conditions, condition_field) as cursor: for row in cursor: row[0] = "Excellent" cursor.updateRow(row) break # STEP 6: Create event table using condition segments arcpy.LocateFeaturesAlongRoutes_lr( in_features=work_evaluated_route_segment_conditions, in_routes=work_routes_evaluated, route_id_field=routes_id_field, out_table=work_event_table, radius_or_tolerance=search_distance, out_event_properties="RID LINE FMEAS TMEAS", route_locations="FIRST", distance_field="DISTANCE", zero_length_events="ZERO", in_fields="FIELDS", m_direction_offsetting="M_DIRECTON") # GP Tool: Table conversion of edited events to a feature layer. work_condition_events_lyr = arcpy.MakeRouteEventLayer_lr( in_routes=routes_fc, route_id_field=routes_id_field, in_table=work_event_table, in_event_properties="rid LINE fmeas tmeas", out_layer="work_condition_events_lyr").getOutput(0) # GP Tool: Copy feature layer to disk as feature class. arcpy.CopyFeatures_management(in_features=work_condition_events_lyr, out_feature_class=out_condition_event_fc) # Optional clean-up of temp data. if clean_up_temp_files: arcpy.AddMessage("Cleaning up temporary content...") # print("Cleaning up temporary content...") content_to_delete = [ work_evaluated_route_segment_conditions, work_nodes_fc, work_routes_lyr, work_condition_events_lyr, work_event_table, work_route_evaluated_segments, work_routes_evaluated, work_routes_fc ] for item in content_to_delete: arcpy.Delete_management(item) arcpy.AddMessage("Temp files cleaned up.")
def convert_lec_nodes_to_line_events( workspace_gdb, lec_nodes_fc, lec_fault_id_field, routes_fc, routes_id_field, snapping_tolerance_meters, clean_up_temp_files=False, ): # Naming file variables temp_lines_fc = os.path.join(workspace_gdb, "temp_ConvertedLines") temp_events_table = os.path.join(workspace_gdb, "temp_LocatedAlongRoute") temp_events_lyr_name = "LEC_events_lyr" output_fc = os.path.join(workspace_gdb, "LEC_LinearEvents") # Snapping tolerance variable since GP tool accepts string with distance format snapping_tolerance = "{0} Meters".format(str(snapping_tolerance_meters)) # GP Tool: LEC points to line, using a fault ID field to allow multiple edits to be processed arcpy.PointsToLine_management(Input_Features=lec_nodes_fc, Output_Feature_Class=temp_lines_fc, Line_Field=lec_fault_id_field, Sort_Field="OBJECTID", Close_Line="NO_CLOSE") # GP Tool: Locate features along route using lines and routes and write to a table. arcpy.LocateFeaturesAlongRoutes_lr( in_features=temp_lines_fc, in_routes=routes_fc, route_id_field=routes_id_field, radius_or_tolerance=snapping_tolerance, out_table=temp_events_table, out_event_properties="RID LINE FMEAS TMEAS", route_locations="FIRST", distance_field="DISTANCE", zero_length_events="ZERO", in_fields="FIELDS", m_direction_offsetting="M_DIRECTON") # GP Tool: Table conversion of edited events to a feature layer. event_layer = arcpy.MakeRouteEventLayer_lr( in_routes=routes_fc, route_id_field=routes_id_field, in_table=temp_events_table, in_event_properties="rid LINE fmeas tmeas", out_layer=temp_events_lyr_name, offset_field="", add_error_field="NO_ERROR_FIELD", add_angle_field="NO_ANGLE_FIELD", angle_type="NORMAL", complement_angle="ANGLE", offset_direction="LEFT", point_event_type="POINT").getOutput(0) # GP Tool: Copy feature layer to disk as feature class. arcpy.CopyFeatures_management(in_features=event_layer, out_feature_class=output_fc) # Optional clean-up of temp data. if clean_up_temp_files: arcpy.AddMessage("Cleaning up temporary content...") # print("Cleaning up temporary content...") content_to_delete = [temp_lines_fc, temp_events_table] for item in content_to_delete: arcpy.Delete_management(item) arcpy.AddMessage("Temp files cleaned up.")
if n >= nrows - 1: rows1.deleteRow(line1) rows1.updateRow(line1) break line2 = next(rows2) if line1.Rank_UGO != line2.Rank_UGO: rows1.deleteRow(line1) rows1.updateRow(line1) if line1.Rank_UGO == line2.Rank_UGO: line1.Distance = line2.MEAS rows1.updateRow(line1) n += 1 MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr( Routes, "Rank_UGO", LocateTABLE, "Rank_UGO LINE MEAS Distance", "%ScratchWorkspace%\\MakeRouteEventTEMP") SplitinFC = arcpy.Sort_management( MakeRouteEventTEMP, "%ScratchWorkspace%\\SplitinFC", [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) CopyInflLine = arcpy.CopyFeatures_management( InflectionLine, "%ScratchWorkspace%\\CopyInflLine") #/creation of the new fields fieldnames = [f.name for f in arcpy.ListFields(SplitinFC)] arcpy.AddField_management(SplitinFC, "Rank_DGO", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(SplitinFC, "Sinuosity", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(SplitinFC, "Half_Amplitude", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
# -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # test.py # Created on: 2016-08-23 20:12:59.00000 # (generated by ArcGIS/ModelBuilder) # Usage: test <Input_Excel_file_of_the_points_on_cable> <Cables_shapefile> <Excel_file_sheet_name_that_containts_data> <Plots_shapefile> <Output_location_for_Excel_file> <Cable_unique_ID_column_name> <Output_directory_for_resulting_shapefile> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy import os, shutil # Script arguments # Param 1 Input_Excel_file_of_the_points_on_cable = arcpy.GetParameterAsText(0) if Input_Excel_file_of_the_points_on_cable == '#' or not Input_Excel_file_of_the_points_on_cable: Input_Excel_file_of_the_points_on_cable = "D:\\ArcmapWorkspace\\cables\\events.xlsx" # provide a default value if unspecified # Param 2 Cables_shapefile = arcpy.GetParameterAsText(1) if Cables_shapefile == '#' or not Cables_shapefile: Cables_shapefile = "D:\\ArcmapWorkspace\\cables\\XrefKLMSusteren_Kabelsleidingen_v001\\Xref-KLM-Susteren_Kabelsleidingen_v001_Elektriciteit_Laagspanning.shp" # provide a default value if unspecified # Param 3 Excel_file_sheet_name_that_containts_data = arcpy.GetParameterAsText(2) if Excel_file_sheet_name_that_containts_data == '#' or not Excel_file_sheet_name_that_containts_data: Excel_file_sheet_name_that_containts_data = "Sheet1" # provide a default value if unspecified # Param 4 Plots_shapefile = arcpy.GetParameterAsText(3) if Plots_shapefile == '#' or not Plots_shapefile: Plots_shapefile = "D:\\ArcmapWorkspace\\cables\\FrankvanNeer01_87AB4CFDC480682F40693F31A5773FFD\\kkadmin_2shp.shp\\Percelen_laag_poly.shp" # provide a default value if unspecified # Param 5 Output_location_for_Excel_file = arcpy.GetParameterAsText(4)
streetDir = 'NW' else: streetDir = 'NW' impactSide = getSide(streetDir, impact.SIDE_OF_STREET) lrInfo = getLRInfoFromBLOCKFACE(segkey, loctype, impactSide, dictBlockface, ntLinearRef) #CREATE A NEW ROW newRow = fieldMapNewRow(ntupPermit, impact, lrInfo, dictImpactEventSchema) insertCurTblImpactLocations.insertRow(newRow) #==================== # ADD ALL THE NEW RECORDS TO A TABLE. #LINEAR REFERENCE THE TABLE flyrImpacts = arcpy.MakeRouteEventLayer_lr( fcStreets, 'COMPKEY', tblImpactEvents, "SEGKEY LINE DISTANCE END_DISTANCE", 'impactsLR', 'WIDTH')[0] arcpy.TruncateTable_management(fcTargetImpacts) arcpy.Append_management(flyrImpacts, fcTargetImpacts) #FOR NOW (7/5/2017, we will just Truncate-Append in all our data. #TODO - make this update only the records with mod dates greater than the last update date """ for row in update cursor (target feature class): update """ print 'HOORAY'
arcpy.Delete_management(outputLinesEventsTable) #eventsTable = arcpy.da.NumPyArrayToTable(myResutlNumpyArray2, r"memory\events") eventsTable = arcpy.da.NumPyArrayToTable(myResutlNumpyArray2, outputLinesEventsTable) #create a table view from eventsTable #fields= arcpy.ListFields(eventsTable) #arcpy.MakeTableView_management(eventsTable, "eventsTableView", "", "", "") #arcpy.MakeTableView_management(eventsTable, "eventsTableView") # To persist the layer on disk make a copy of the view #arcpy.CopyRows_management("crime_view", "C:/temp/newfreq.dbf") #arcpy.CopyRows_management(eventsTable, outputPointEventsTable) #myType = np.dtype([('tag_ident','U10'),('eventtype','U10'),('initialdistancemts','d'), ('finaldistancemts','d'), \ # ('initialptime','U30'), ('finalptime','U30'), ('initialpointid','i'), ('finalpointid','i')]) #Create Route Event Layer rid = fieldDelimiter props = fieldDelimiter + " LINE initialdistancemts finaldistancemts" lyr = theRunName + "_distance_lineevents_lyr_filter1" # Execute MakeRouteEventLayer arcpy.MakeRouteEventLayer_lr(inFcRoutes, rid, outputLinesEventsTable, props, lyr, "#", "ERROR_FIELD", "ANGLE_FIELD") #arcpy.MakeRouteEventLayer_lr(inFcRoutes, rid, "memory\events", props, lyr, "#", "ERROR_FIELD", "ANGLE_FIELD") outputLineFC = os.path.join(arcpy.env.workspace, theRunName, theRunName + "_distance_lineevents_fc_filter1") if arcpy.Exists(outputLineFC): arcpy.Delete_management(outputLineFC) arcpy.CopyFeatures_management(lyr, outputLineFC)
arcpy.AddMessage(' ' + outFC + ' saved') else: #or continue and place the intervals as events along the borehole routes #convert to routes bhRoutes = outName + '_bhRoutes' arcpy.AddMessage("Measuring the length of borehole lines in " + bhLines) arcpy.CreateRoutes_lr(bhLines, bhIdField, bhRoutes, 'ONE_FIELD', bhDepthField, '#', 'UPPER_LEFT') arcpy.AddMessage(' ' + bhRoutes + ' written to ' + arcpy.env.scratchWorkspace) #place borehole intervals (line events) on borehole routes props = intBhIdFld + ' LINE ' + intTopDepthFld + ' ' + intBotDepthFld arcpy.AddMessage("Placing borehole intervals on routes in " + bhRoutes) arcpy.MakeRouteEventLayer_lr(bhRoutes, bhIdField, intervalsTable, props, 'lyr', '#', 'ERROR_FIELD') #extract only valid route events from this in-memory layer arcpy.AddMessage('Filtering interval records with location errors.') bhIntervals = outName + '_intervals' arcpy.Select_analysis('lyr', bhIntervals, "\"LOC_ERROR\" <> 'ROUTE NOT FOUND'") arcpy.AddMessage(' ' + bhIntervals + ' written to ' + arcpy.env.scratchWorkspace) #now, join to eventTable in order to pass over the 'DISTANCE' which is the distance #the sticklog is away from the cross-section line arcpy.AddField_management(bhIntervals, 'Dis2XSec', 'DOUBLE') layer2 = arcpy.MakeFeatureLayer_management(bhIntervals, 'lyr2') arcpy.env.qualifiedFieldNames = False arcpy.AddJoin_management('lyr2', bhIdField, eventTable, bhIdField)
def get_dem_intersect(): with arcpy.da.SearchCursor(route_copy, ['route_id', 'End_Measure']) as cursor: for row in cursor: interval_num = dem_intersect_interval route_id = int(row[0]) end_measure = row[1] row_number = int(row[1] / interval_num) csv_path = os.path.join(table_folder, str(route_id) + '.csv') df = pd.DataFrame({'measure': [0], 'route_id': [route_id]}) if row_number == 1: df_50 = pd.DataFrame(({'measure': [interval_num], 'route_id': [route_id]})) df = df.append(df_50, ignore_index=True) if row_number > 1: i = 1 while i < row_number + 1: df_row = pd.DataFrame(({'measure': [interval_num * i], 'route_id': [route_id]})) df = df.append(df_row, ignore_index=True) i = i + 1 df_end_measure = pd.DataFrame(({'measure': [end_measure], 'route_id': [route_id]})) df = df.append(df_end_measure, ignore_index=True) df.to_csv(csv_path) arcpy.AddMessage("Table Creation Complete: " + csv_path) arcpy.AddMessage("Row number: " + str(row_number + 2)) arcpy.MakeRouteEventLayer_lr(in_routes=route_copy, route_id_field='route_id', in_table=csv_path, in_event_properties="route_id POINT measure", out_layer=ROUTE_EVENT_LAYER) arcpy.CopyFeatures_management(ROUTE_EVENT_LAYER, os.path.join(gdb, 'profile_REL_' + str(route_id))) arcpy.AddMessage('Route Event Layer Created: ' + str(route_id)) arcpy.sa.ExtractValuesToPoints(os.path.join(gdb, 'profile_REL_' + str(route_id)), third_arc_second_raster, os.path.join(gdb, 'profile_' + str(route_id))) arcpy.AddField_management(os.path.join(gdb, 'profile_' + str(route_id)), 'elevation', "DOUBLE") arcpy.AddMessage('Calculating the Elevation field: ' + str(route_id)) with arcpy.da.UpdateCursor(os.path.join(gdb, 'profile_' + str(route_id)), ['RASTERVALU', 'elevation']) \ as updater: for route in updater: elevation = route[0] * 3.28084 route[1] = elevation updater.updateRow(route) arcpy.AddMessage('Merging outputs....') profile_output_list = [] output_fcs = arcpy.ListFeatureClasses() for output in output_fcs: if 'REL' in output or 'route_copy' in output: arcpy.Delete_management(output) else: profile_output_list.append(output) arcpy.Merge_management(profile_output_list, 'merged_profiles') arcpy.CopyRows_management('merged_profiles', 'merge_table') arcpy.AddMessage('Outputs merged') field_list = [f.name for f in arcpy.ListFields('merge_table')] arcpy.AddMessage("Field List Compiled") # load output table into a numpy array, to prepare the data for pandas arcpy.AddMessage("Building Array....") table_array = arcpy.da.FeatureClassToNumPyArray('merged_profiles', field_list) arcpy.AddMessage("Array Complete") # load output table into pandas data frame arcpy.AddMessage("Building Data Frame.....") dem_df = pd.DataFrame(table_array) dem_df.drop(columns=['OBJECTID', 'Field1', 'RASTERVALU'], inplace=True) dem_df.sort_values(['route_id', 'measure'], ascending=[True, True], inplace=True) dem_df = dem_df.round({'elevation': 2}) # remove duplicate values if one happens to be generated dem_df.drop_duplicates(subset=['measure', 'route_id'], keep="first", inplace=True) # output data frame to csv arcpy.AddMessage("Outputting to csv.....") dem_df.to_csv(output_file_path) arcpy.AddMessage("csv complete")
if __name__ == '__main__': pass import arcpy LRS = "SDE.CMLRS" ws = r"//gisdata/arcgis/GISdata/KDOT/BTP/Projects/MAPINDEX/IndexProcess.mdb/NAD83" iCCL = "CCL_ENV" PLSS_IN = "DW_PLSS_MV" PLSS_1 = "PLSS" PLSS_2 = "PLSSTR1" cclU = "CCL_U" cclUD = "CCL_UD" #CCL Bounding Polygons arcpy.MakeRouteEventLayer_lr("SDE.CMLRS", "LRS_KEY", "SDE.CCL_Lane", "LRSKEY LINE BEGMILEPOST ENDMILEPOST", "CCL_LINES", "#", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.MinimumBoundingGeometry_management( "SDE.CCL_Lane Events", "//gisdata/arcgis/GISdata/KDOT/BTP/Projects/MAPINDEX/IndexProcess.mdb/NAD83/CCL", "ENVELOPE", "LIST", "CITYNO; ", "MBG_FIELDS") #City Limit Bounding Polygons #Build PLSS SEctions for T&R arcpy.env.workspace = ws arcpy.FeatureClassToFeatureClass_conversion(PLSS_IN, ws, PLSS_1, "#", "#") arcpy.Dissolve_management(PLSS_1, ws + "//" + PLSS_2, "TOWNSHIP;RANGE", "#", "MULTI_PART", "DISSOLVE_LINES") arcpy.AddField_management(PLSS_2, "TWP_NO", "LONG", "#", "#", "#", "#", "NULLABLE", "NON_REQUIRED", "#")
row.setValue("reference", seg['reference']) row.setValue("lastEditBy", seg['lasteditby']) row.setValue("conflict", seg['conflict']) row.setValue("parent", seg['parent']) row.setValue("loStation", seg['lostation']) row.setValue("hiStation", seg['histation']) rows.insertRow(row) cur.close() del row del rows arcpy.env.workspace = workspace in_routes_field = "PARENT" in_table_props = "parent LINE loStation hiStation" out_routes = "art80_routes_" + str(int(time.time())) arcpy.MakeRouteEventLayer_lr(street, in_routes_field, route_table, in_table_props, out_routes) arcpy.CopyFeatures_management(out_routes, out_routes) input_features = out_routes + ";" + parcel_buffered route_intersect = "art80_routes_intersected_" + str(int(time.time())) arcpy.Intersect_analysis(input_features, route_intersect, "ALL", "", "LINE") where_clause = """ reptype in ('ARA','ARC','ARP','ARR','TRF','BDR','BPR','BKC','MDT','PIA','PIR','RCA','RCC','RCR', 'OVA','OVC','OVR','SWK') AND WebCategory in ('Board Approved','LOI Submitted','Under Construction','Under Review')""" # Execute Select arcpy.Select_analysis(route_intersect, output, where_clause) print output # arcpy.TableToExcel_conversion(output,"test_excel") # print "test_excel"
arcpy.MakeTableView_management(C_PointData_LocateFeatures, C_PointData_LocateFeatures_View, "", "", "") # Process: Table to Table (Pts) arcpy.TableToTable_conversion(C_PointData_LocateFeatures_View, Workspace, "D_PointData_LocateFeatures_FeaturesToUse", "", "") # Process: Add Join with Pts arcpy.AddJoin_management(E_CopyOfOriginalInputWithNewFields_View, "Event_ID", D_PointData_LocateFeatures_FeaturesToUse, "Event_ID", "KEEP_ALL") # Process: Calculate Field (Route_ID) arcpy.CalculateField_management(E_CopyOfOriginalInputWithNewFields_View, "E_CopyOfOriginalInputWithNewFields.Route_ID", "!D_PointData_LocateFeatures_FeaturesToUse.RID!", "PYTHON", "") # Process: Calculate Field (Measure) arcpy.CalculateField_management(E_CopyOfOriginalInputWithNewFields_View, "E_CopyOfOriginalInputWithNewFields.Measure", "!D_PointData_LocateFeatures_FeaturesToUse.MEAS!", "PYTHON", "") # Process: Remove Join with Pts arcpy.RemoveJoin_management(E_CopyOfOriginalInputWithNewFields_View, "D_PointData_LocateFeatures_FeaturesToUse") # Process: Export to Final Table arcpy.TableToTable_conversion(E_CopyOfOriginalInputWithNewFields_View, Workspace, "F_FinalEventTable", "", "", "") # Process: Make Route Event Layer arcpy.MakeRouteEventLayer_lr(LRS_Route_Network, "Route_ID", F_FinalEventTable, "RouteID POINT Measure", G_FinalEventTable_Events, "", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") # Process: Copy Features arcpy.CopyFeatures_management(G_FinalEventTable_Events, G_FinalEventsToQC, "", "0", "0", "0") # Process: Make Feature Layer arcpy.MakeFeatureLayer_management(G_FinalEventsToQC, G_FinalEventsToQC_Layer, "", "", "")
"LRS_KEY POINT CRMP", "FIRST", "DISTANCE", "ZERO", "FIELDS", "M_DIRECTON") #Calculate the begin and end miles post integer around the CR_MP arcpy.AddField_management("LFAR_CRASHES", "BeginCMP", "LONG") arcpy.AddField_management("LFAR_CRASHES", "EndCMP", "LONG") #take the crash location and calculate the integer part of the Logmile arcpy.CalculateField_management("LFAR_CRASHES", "BeginCMP", "long( !CRMP! )", "PYTHON_9.3", "#") #and add one to it - now we have the crashes overlapping every mile covering the extent of the LRS Geometry arcpy.CalculateField_management("LFAR_CRASHES", "EndCMP", "long( !CRMP! )+1", "PYTHON_9.3", "#") #locate the events now as lines #dissolve the lines as follows arcpy.MakeRouteEventLayer_lr("LRS_Route_CRM", "LRS_KEY", "LFAR_CRASHES", "LRS_KEY LINE BeginCMP EndCMP", "LFAR_CRASHES lines", "#", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT") arcpy.Dissolve_management("LFAR_CRASHES lines", "LFAR_Dissolve", "LRS_KEY;BeginCMP;EndCMP", "ACCIDENT_KEY COUNT", "SINGLE_PART", "DISSOLVE_LINES") arcpy.FeatureClassToFeatureClass_conversion("LFAR_Dissolve", ws, sumlyr) arcpy.AddField_management(sumlyr, "ID1", "LONG") #arcpy.CalculateField_management(sumlyr,"ID1","[OBJECTID]","VB","#") #I used a cursor for fun! cursor = arcpy.UpdateCursor(sumlyr) for row in cursor: row.ID1 = row.OBJECTID cursor.updateRow(row) # Delete cursor and row objects to remove locks on the data del row
rows1.deleteRow(line1) rows1.updateRow(line1) if line1.Rank_UGO == line2.Rank_UGO : line1.Distance = line2.MEAS rows1.updateRow(line1) n += 1 arcpy.AddMessage(" Creation of the final inflection points output - 8/9") PtsTEMP = arcpy.MakeXYEventLayer_management(InflectionPtTABLE, "NEAR_X", "NEAR_Y", "PtsTEMP", SpatialRef, "") InflectionPt = arcpy.CopyFeatures_management(PtsTEMP, outInflectionPt) arcpy.DeleteField_management(InflectionPt, ["RID", "MEAS", "Distance", "IN_FID", "NEAR_FID", "NEAR_DIST", "NEAR_X", "NEAR_Y", "End_X", "End_Y"]) arcpy.AddMessage(" Creation of the final inflection line output - 9/9") MakeRouteEventTEMP = arcpy.MakeRouteEventLayer_lr(BezierRoutes, "Rank_UGO", PtsForInflLineDisaggregationTABLE, "Rank_UGO LINE MEAS Distance", "%ScratchWorkspace%\\MakeRouteEventTEMP") InflectionLine = arcpy.Sort_management(MakeRouteEventTEMP, "%ScratchWorkspace%\\InflectionLine", [["Rank_UGO", "ASCENDING"], ["Distance", "ASCENDING"]]) fieldnames = [f.name for f in arcpy.ListFields(InflectionLine)] arcpy.AddField_management(InflectionLine, "Rank_DGO", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(InflectionLine, "Rank_DGO", "!"+fieldnames[0]+"!", "PYTHON_9.3", "") arcpy.CalculateField_management(InflectionLine, "Distance", "!MEAS!", "PYTHON_9.3", "") arcpy.DeleteField_management(InflectionLine, ["RID", "MEAS", "IN_FID", "NEAR_FID", "NEAR_DIST", "NEAR_X", "NEAR_Y", "END_X", "END_Y"]) arcpy.AddField_management(InflectionPt, "Distance", "FLOAT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") UPD_SL.UpToDateShapeLengthField(InflectionLine) rows0 = arcpy.UpdateCursor(InflectionPt) rows1 = arcpy.UpdateCursor(InflectionPt) line1 = next(rows1) rows2 = arcpy.SearchCursor(InflectionLine) rows3 = arcpy.SearchCursor(BezierRoutes)
arcpy.env.scratchWorkspace) #remove duplicate records that result from what appears to be #an unresolved bug in the Locate Features Along Routes tool #some points will get more than one record in the event table #and slightly different, sub-mapunit, mValues try: arcpy.DeleteIdentical_management(eventTable, 'ORIG_PTID') except: pass #place points as events on the cross section line eventLyr = '_lyr' rProps = 'rkey POINT RouteM' arcpy.MakeRouteEventLayer_lr(zmLine, 'ORIG_FID', eventTable, rProps, eventLyr, '#', 'ERROR_FIELD', 'ANGLE_FIELD', 'TANGENT') eventPts = outName + '_events' arcpy.CopyFeatures_management(eventLyr, eventPts) arcpy.AddMessage(' ' + eventPts + ' feature layer written to ' + arcpy.env.scratchWorkspace) # add DistanceFromSection and LocalXsAzimuth fields arcpy.AddField_management(eventPts, 'DistanceFromSection', 'FLOAT') arcpy.AddField_management(eventPts, 'LocalCSAzimuth', 'FLOAT') #check for whether these are structural data if not strikeField == '': isOrientationData = True arcpy.AddField_management(eventPts, 'ApparentInclination', 'FLOAT') arcpy.AddField_management(eventPts, 'Obliquity', 'FLOAT')
# populate event table fromLoc = start inc = util.excelTime.xl_time[increment] with arcpy.da.InsertCursor(eventTable, f_names) as iCur: while fromLoc < endPath: iCur.insertRow([ptt, fromLoc]) fromLoc += inc iCur.insertRow([ptt, endPath]) # arcpy.AddMessage('Event table created') # Add events to route eventProperties = 'ptt POINT FromLoc' # eventProperties = 'name POINT from_loc' eventLayer = route_name + '_events' # arcpy.MakeRouteEventLayer_lr(route_name, 'name', eventTable, arcpy.MakeRouteEventLayer_lr(route_name, 'ptt', eventTable, eventProperties, eventLayer) # persist to GDB area = path.basename(sel_layer) eventName = ''.join(['pt_', str(ptt).zfill(5), '_', area]) eventFC = arcpy.CopyFeatures_management(eventLayer, eventName) # arcpy.AddMessage('Residence point layer into GDB') # select events inside AOI arcpy.SelectLayerByLocation_management(eventName, "INTERSECT", sel_layer) # count selected events & convert into days result = arcpy.GetCount_management(eventName) count_interp = int(result.getOutput(0)) hours = count_interp / inc_dict[increment] days = hours / 24.0 # If tag was 100% inside, adding up event hours can slightly exceeed all_time
def AddSegFromAddresses(AddressList, SegInput, RouteID, Output): import requests APIKey = 'AIzaSyCs80htAI4UAHHuF5m9IclsbMqg1FKxoEQ' PntLayer = CreateOutPath(Output, 'pnts', '') arcpy.CreateFeatureclass_management(out_path=os.path.dirname(Output), out_name=os.path.basename(PntLayer), geometry_type='POINT', spatial_reference=NAD1983IL) arcpy.AddField_management(PntLayer, 'SegID', 'SHORT') arcpy.AddField_management(PntLayer, 'Address', 'TEXT') IC = arcpy.InsertCursor(PntLayer) i = 0 for add in AddressList: r = IC.newRow() r.setValue('SegID', i) r.setValue('Address', add[0]) IC.insertRow(r) r = IC.newRow() r.setValue('SegID', i) r.setValue('Address', add[1]) IC.insertRow(r) i += 1 del IC AddPointFromAddress(PntLayer, 'Address') Buffer = "200 Feet" SPJ = CreateOutPath(MainFile=Output, appendix='SPJ', Extension='') arcpy.SpatialJoin_analysis( target_features=SegInput, join_features=PntLayer, out_feature_class=SPJ, join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_COMMON", match_option="INTERSECT", search_radius=Buffer, ) UnSplt = CreateOutPath(MainFile=Output, appendix='Unsplt', Extension='') arcpy.UnsplitLine_management(in_features=SPJ, out_feature_class=UnSplt, dissolve_field="", statistics_fields="") SPJ2 = CreateOutPath(MainFile=Output, appendix='SPJ2', Extension='') arcpy.SpatialJoin_analysis( target_features=UnSplt, join_features=PntLayer, out_feature_class=SPJ2, join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_COMMON", match_option="INTERSECT", search_radius=Buffer, ) Final_Layer = CreateOutLayer('FinalLayer') arcpy.MakeFeatureLayer_management(in_features=SPJ2, out_layer=Final_Layer) arcpy.SelectLayerByAttribute_management(in_layer_or_view=Final_Layer, selection_type='NEW_SELECTION', where_clause="Join_Count = 2") EventTable = CreateOutPath(MainFile=Output, appendix='EventTable', Extension='') arcpy.LocateFeaturesAlongRoutes_lr(in_features=PntLayer, in_routes=SPJ, route_id_field=RouteID, radius_or_tolerance=Buffer, out_table=EventTable, out_event_properties=" ".join( [RouteID, "POINT", "MP"]), route_locations="FIRST", distance_field="DISTANCE", in_fields="FIELDS", m_direction_offsetting="M_DIRECTON") SegTable = CreateOutPath(MainFile=Output, appendix='SegTable', Extension='') arcpy.CreateTable_management(out_path=os.path.dirname(SegTable), out_name=os.path.basename(SegTable)) arcpy.AddField_management(SegTable, RouteID, 'TEXT') arcpy.AddField_management(SegTable, 'BEG_STA', 'DOUBLE') arcpy.AddField_management(SegTable, 'END_STA', 'DOUBLE') arcpy.AddField_management(SegTable, 'Address1', 'TEXT') arcpy.AddField_management(SegTable, 'Address2', 'TEXT') #SegIDDict = {r.getValue('SegID'):{'INV':'','BMP':0,'EMP':0,'Add1':'','Add2':''}} SegIDDict = {} for r in arcpy.SearchCursor(EventTable): k = r.getValue('SegID') if k in SegIDDict.keys(): mp = r.getValue('MP') add = r.getValue('Address') if SegIDDict[k]['BMP'] <= mp: SegIDDict[k]['EMP'] = mp SegIDDict[k]['Add2'] = add else: SegIDDict[k]['EMP'] = SegIDDict[k]['BMP'] SegIDDict[k]['BMP'] = mp SegIDDict[k]['Add2'] = SegIDDict[k]['Add1'] SegIDDict[k]['Add1'] = add else: SegIDDict.update({ r.getValue('SegID'): { 'INV': r.getValue(RouteID), 'BMP': r.getValue('MP'), 'EMP': -1, 'Add1': r.getValue('Address'), 'Add2': '' } }) print('End point was not found') IC = arcpy.InsertCursor(SegTable) for k in SegIDDict.keys(): r = IC.newRow() r.setValue(RouteID, SegIDDict[k]['INV']) r.setValue('BEG_STA', SegIDDict[k]['BMP']) r.setValue('END_STA', SegIDDict[k]['EMP']) r.setValue('Address1', SegIDDict[k]['Add1']) r.setValue('Address2', SegIDDict[k]['Add2']) IC.insertRow(r) del IC Overlay_Event_Layer = CreateOutLayer('OverlayEventLayer') arcpy.MakeRouteEventLayer_lr(in_routes=SegInput, route_id_field=RouteID, in_table=SegTable, in_event_properties=' '.join( [RouteID, 'LINE', 'BEG_STA', 'END_STA']), out_layer=Overlay_Event_Layer, offset_field="", add_error_field="ERROR_FIELD") Sort = CreateOutPath(MainFile=Output, appendix='sort', Extension='') arcpy.Sort_management(in_dataset=Overlay_Event_Layer, out_dataset=Sort, sort_field=';'.join([RouteID, 'BEG_STA', 'END_STA'])) Final_Layer = CreateOutLayer('FinalLayer') arcpy.MakeFeatureLayer_management(in_features=Sort, out_layer=Final_Layer) arcpy.SelectLayerByAttribute_management(in_layer_or_view=Final_Layer, selection_type='NEW_SELECTION', where_clause="Shape_Length > 0") arcpy.Delete_management(Output) arcpy.CopyFeatures_management(in_features=Final_Layer, out_feature_class=Output) arcpy.Delete_management(PntLayer) arcpy.Delete_management(SPJ) arcpy.Delete_management(SPJ2) arcpy.Delete_management(EventTable) arcpy.Delete_management(SegTable) arcpy.Delete_management(Overlay_Event_Layer) arcpy.Delete_management(Sort) arcpy.Delete_management(Final_Layer) arcpy.Delete_management(UnSplt)
arcpy.SelectLayerByAttribute_management( "locations_tbl", "NEW_SELECTION", "FMEAS + " + str(zoneDistanceM) + " <= routeLengthM") # zoneM is simply calculated as the start point plus the defined zone distance arcpy.CalculateField_management("locations_tbl", "zoneM", "!FMEAS! + " + str(zoneDistanceM), "PYTHON_9.3") arcpy.TableToTable_conversion("locations_tbl", workingDirectory, "nonConfluence") # Create impounded zones nonConfluenceZones = arcpy.MakeRouteEventLayer_lr( routes, "routeID", "nonConfluence", "routeID LINE FMEAS zoneM", "nonConfluenceZones", "#", "NO_ERROR_FIELD", "NO_ANGLE_FIELD") # Convert to feature in geodatabase arcpy.FeatureClassToFeatureClass_conversion(nonConfluenceZones, workingDirectory, "nonConfluenceZone") toMerge = [workingDirectory + "/nonConfluenceZone"] # ================= # Confluence Points # ================= arcpy.SelectLayerByAttribute_management("locations_tbl", "SWITCH_SELECTION")