I have a network layer of roads and a point layer containing points (towns) on that network. I need to create layers containing each pair of towns and its true shape route line. So for example from A-B, A-C, A-D, ... etc. So that every pair has its own shapefile with the line.
I tried the OD Matrix solution in Network Analyst in ArcMap 10.3 which created what I wanted as far as attributes go, but it draws only straight lines between each pair, it doesn't draw the line along the network. I need that. Closest Facility solution provides true shapes along the network but it doesn't create what OD matrix does. I need one shapefile for every pair.
Any advice or direction?
I tried to implement what Paul in Answer 1 suggested but I couldn't complete the script. I still need a tool to address correct points in pairs so they could be solved. It could be done sql query maybe? Any further advice?
from collections import defaultdict
import itertools
import pprint
import arcpy
from arcpy import env
import os
# generate pairs
ddict = defaultdict(list)
for start,end in itertools.combinations(range(10), 2):
ddict[start].append(end)
# viz example 3
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = r"E:/Dokumenty/GISdata/PID"
env.overwriteOutput = True
#Set local variables
inNetworkDataset = r"E:/Dokumenty/GISdata/PID/silnice_krnap_ND.nd"
impedanceAttribute = "Length"
accumulateAttributeName = ["Length"]
for key,value in ddict.items():
for v in value:
inFacilities = r"PID/Obce_body_luzka" # melo by byt key
inIncidents = r"PID/Obce_body_luzka" # melo by byt v
outNALayerName = "Dvojice_bodu_{}_{}".format(key, v)
outLayerFile = os.path.join(r"E:/dokumenty/gisdata/output2", outNALayerName + ".lyr")
NAResultObject = arcpy.na.MakeClosestFacilityLayer(inNetworkDataset,outNALayerName,impedanceAttribute,"TRAVEL_TO","",1, accumulateAttributeName,"NO_UTURNS")
#Get the layer object from the result object. The closest facility layer can
#now be referenced using the layer object.
outNALayer = NAResultObject.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"]
# Import system modules
import arcpy
# Set data path
intable = "E:\Dokumenty\GISdata\PID\Obce_body_luzka.dbf"
# Get the fields from the input
fields= arcpy.ListFields(intable)
# Create a fieldinfo object
fieldinfo = arcpy.FieldInfo()
# Iterate through the fields and set them to fieldinfo
for field in fields:
fieldinfo.addField(field.name, field.name, "VISIBLE", "")
arcpy.MakeTableView_management(intable, "crime_view", "", "", fieldinfo)
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, "Obce_body_luzka", "", "")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, "Obce_body_luzka", "", "")
#Solve the closest facility layer
arcpy.na.Solve(outNALayer)
#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 occured on line %i" % tb.tb_lineno
print str(e)
I worked with the idea of using the closest facility method, while thinking that "the closest" route is also "the only route" from A-B, etc.