官方示例,记录而已
#coding=utf-8
import arcpy
from arcpy import env
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = r'E:xxx est2Data80.gdb'
env.overwriteOutput = True
#Set local variables
inNetworkDataset = r'RD/RD_ND'
outNALayerName = "ClosestHospital"
impedanceAttribute = "Length"
accumulateAttributeName = ["Meters"]
inFacilities = "DX/三甲医院2"
inIncidents = "TMP/小区点"
outLayerFile = "E:\xxx\test2\" + outNALayerName + "333.lyr"
out_featureclass =r'E:xxx est2Data80.gdbTMPHOS'
#Create a new closest facility analysis layer. Apart from finding the drive
#time to the closest warehouse, we also want to find the total distance. So
#we will accumulate the "Meters" impedance attribute.
outNALayer = arcpy.na.MakeClosestFacilityLayer(inNetworkDataset,outNALayerName,
impedanceAttribute,"TRAVEL_TO",
"",1, "",
"ALLOW_UTURNS")
#Get the layer object from the result object. The closest facility layer can
#now be referenced using the layer object.
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the closest facility layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later
facilitiesLayerName = subLayerNames["Facilities"]
incidentsLayerName = subLayerNames["Incidents"]
#Load the warehouses as Facilities using the default field mappings and
#search tolerance
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities, "", "")
#Load the Stores as Incidents. Map the Name property from the NOM field
#using field mappings
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, incidentsLayerName)
fieldMappings["Name"].mappedFieldName = "NOM"
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings,"")
#Solve the closest facility layer
arcpy.na.Solve(outNALayer)
#复制最短距离 routes 到其他地方
routes = arcpy.mapping.ListLayers(outNALayer, "Routes")[0]
arcpy.CopyFeatures_management(routes, out_featureclass)
#Save the solved closest facility layer as a layer file on disk with relative paths
arcpy.management.SaveToLayerFile(outNALayer, outLayerFile,"RELATIVE")
print "Script completed successfully"
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 occurred on line %i" % tb.tb_lineno
print str(e)
print 'over'