bartekent

Network Analyst script

Discussion created by bartekent on Dec 1, 2011
Latest reply on Dec 1, 2011 by csny490
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)

Outcomes