Select to view content in your preferred language

Network Analyst script

2635
2
12-01-2011 05:30 AM
BartoszPaczkowski
New Contributor
Hi
I have created script which first create network dataset layer, after that it creates  closest facility layer. I works fine, the only problem I have is the output format -  it saves the output to layer file (.lyr).  This layer consist of few components: facilities, incidents, point barriers, routes, line barriers and polygon, but the only thing I want to extract are routes in shape format. Does anyone know how to it? Manually I do it in ArcMap by export - data.

#importing arcpy
import arcpy

from arcpy import env

#check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")

#importing street data processing tool
arcpy.ImportToolbox("C:\StreetDataProcessing\Street Data Processing Tools.tbx")

#set environment setting
env.workspace = "c:/data"

inNW="polpl1___________nw.shp"
inMN="polpl1___________mn.shp"
inMP="polpl1___________mp.dbf"
inSI="polpl1___________si.dbf"
inSP="polpl1___________sp.dbf"
outFileGDB="pl1.gdb"

arcpy.ProcessMultiNetData_nasample(inNW,inMN,inMP,inSI,inSP,outFileGDB)


env.workspace="c:/data/pl1.gdb/MultiNet"
env.overwriteOutput = True
try:
    #Set local variables
    inNetworkDataset = "MultiNet_ND"
    outNALayer = "routes_polpl1"
    impedanceAttribute = "Minutes"
    accumulateAttributeName = ["Meters","Minutes"]
    inFacilities = "C:\data\pl1_sm.shp"
    inIncidents = "C:\data\pl1_sm.shp"
    fieldMappings = "Name NOM #"
    outLayerFile = "C:/data/output" + "/" + outNALayer + ".lyr"

    #create closet facility analysis layer
    arcpy.MakeClosestFacilityLayer_na(inNetworkDataset,
                                   outNALayer,impedanceAttribute,"TRAVEL_TO","",10,
                                   accumulateAttributeName,"NO_UTURNS",
                                   ["Oneway","RestrictedTurns"],"USE_HIERARCHY","",
                                   "TRUE_LINES_WITH_MEASURES")

  
    arcpy.AddLocations_na(outNALayer,"Facilities",inFacilities,"","")
    arcpy.AddLocations_na(outNALayer,"Incidents",inIncidents,fieldMappings,"")

    arcpy.Solve_na(outNALayer)
    arcpy.SaveToLayerFile_management(outNALayer,outLayerFile,"RELATIVE")

    print "Script completed succesfully"

except Exception as e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "An error occured on line %i" % tb.tb_lineno
    print str(e)
Tags (2)
0 Kudos
2 Replies
KyleShannon
Deactivated User
I remember doing something like this for drive time polygons.  I think I called CreateFeatureclass() and used the layer and sub layer as a template, then appended the data from the polygon layer. 

arcpy.Solve_na(s_name)
arcpy.CreateFeatureclass_management(working_dir, l_name, "POLYGON", 
                                        "{0}\Polygons".format(s_name),
                                        "", "", "", "", "", "", "")
arcpy.Append_management("{0}\Polygons".format(s_name), 
                            out_name, "NO_TEST", "", "")


Not sure if that is what you are looking for, or if I did it right.  It was in the middle of a multiprocess run where I split up a NA layer and ran it on 8 threads, so you could probably just use CopyFeatures().
0 Kudos
ChrisSnyder
Honored Contributor
bartekent - I think you are confusing a feature layer and and a layer file (.lyr file), which are two different things. The Solve_na tool returns a feature layer, which is a fancy in-memory reference to a featureclass. A feature layer's spatial/attribute data is not necessarily stored in memory, it's refernce toa feature class is just "abstracted" in memory. By saving a layer file as a .lyr file, you are in effect writing the in memory "reference" information to a file. This includes things like SQL queries, symbols, etc.

Anyway, like ksshannon says, just use the CopyFeatures tool (or another tool) to copy the feature layer to a feature class stored on disk.

For example:
arcpy.AddLocations_na("cfl", "Polygon Barriers", dnrHaulBarrierPolysFC, "", "", "", "", "", "", "", "", "", ""); showGpMessage()
arcpy.AddLocations_na("cfl", "Facilities", haulDestinationPntsFC, "", "", "", "", "", "", "", "", "", ""); showGpMessage()
arcpy.AddLocations_na("cfl", "Incidents", actDislvPntsFC, "", "", "", "", "", "", "", "", "EXCLUDE", ""); showGpMessage()
arcpy.Solve_na("cfl", "SKIP", "CONTINUE"); showGpMessage()
dnrHaulRoutesFC = fgdbPath + "\\dnr_haul_routes_least_time"
arcpy.env.outputMFlag = "DISABLED"
arcpy.CopyFeatures_management("cfl\\Routes", dnrHaulRoutesFC); showGpMessage()
0 Kudos