I am having trouble adding a network analyst layer to an mxd using arcpy. I am not getting any errors, yet the layers don't show up after the script completes. I have tried several ways. The first is doing it all in memory. I am creating a layer, say using MakeClosestFacilityLayer, getting the layer from the created object, then adding it via arcpy.mapping.addLayer. I have also exported the Layer to disk and then created a Layer using arcpy.mapping.Layer and then adding that to the mxd. No luck. Help would be appreciated.
Solved! Go to Solution.
Before saving the mxd, arcpy.RefreshTOC() and arcpy.RefreshActiveView() must be called. The documentation samples do not show this.
not sure where you are doing this or what you are using, but in arcmap and ArcGIS pro, you make your layers by accessing tools within arctoolbox within your scripts. maybe you could elaborate
I found that when a network dataset it updated, the NA layer in the mxd doesn’t automatically incorporate those changes. The layer must be removed and re-added to the mxd. I host an endpoint on arcserver that has some NA layers. I am automating the republishing of this service. To do this successfully though, I need to remove the layers and add them again. I can remove the layers without a problem. But having difficulty adding them back in. I have also tried exporting the layer to disc and then using the arcpy.mapping.Layer to import it and add to the mxd, but no luck. Here is my code:
mxd = arcpy.mapping.MapDocument(pathtoMXD)
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
ndLayer = '';
layers = arcpy.mapping.ListLayers(mxd, "", df)
for lyr in layers:
if lyr.name == "Closest Facility" or lyr.name == "Route":
arcpy.mapping.RemoveLayer(df, lyr)
print "layer removed"
elif lyr.name =="Standard_CL_ND":
ndLayer = lyr
print "found nd layer"
else:
pass
mxd.save()
cfObject=arcpy.MakeClosestFacilityLayer_na(in_network_dataset=ndLayer, out_network_analysis_layer="Closest Facility",
impedance_attribute="DriveTime", travel_from_to="TRAVEL_TO", default_cutoff="", default_number_facilities_to_find="1",
accumulate_attribute_name="",UTurn_policy="ALLOW_UTURNS", restriction_attribute_name="Oneway", hierarchy="NO_HIERARCHY",
hierarchy_settings="", output_path_shape="TRUE_LINES_WITH_MEASURES", time_of_day="", time_of_day_usage="NOT_USED")
cfLayer = cfObject.getOutput(0)
#arcpy.SaveToLayerFile_management (cfLayer,"c:
publish
catawba
CFLayer")
#addLayer = arcpy.mapping.Layer("c:
publish
catawba
CFLayer.lyr")
arcpy.mapping.AddLayer(df, cfLayer, "TOP")
mxd.save()
Also, Im on arc 10.5. I guess I could install pro on my computer. I did see that the process is easier. If you have any thoughts on the solution without pro, that would be great. Thanks in advance.
arjun
And I'm actually having trouble adding ANY layers. I've seen other threads about this issue, but it seems that everyone else is running their code within arcmap, not stand-alone, so my case may be different.
Before saving the mxd, arcpy.RefreshTOC() and arcpy.RefreshActiveView() must be called. The documentation samples do not show this.