Trouble adding Network Analyst Layer to MXD with ArcPy

622
5
Jump to solution
12-11-2017 11:56 AM
ArjunDongre
Occasional Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
ArjunDongre
Occasional Contributor

Before saving the mxd, arcpy.RefreshTOC() and arcpy.RefreshActiveView() must be called. The documentation samples do not show this.

View solution in original post

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
ArjunDongre
Occasional Contributor

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()

0 Kudos
ArjunDongre
Occasional Contributor

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

0 Kudos
ArjunDongre
Occasional Contributor

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.

0 Kudos
ArjunDongre
Occasional Contributor

Before saving the mxd, arcpy.RefreshTOC() and arcpy.RefreshActiveView() must be called. The documentation samples do not show this.

0 Kudos