Thursday, 5 April 2018

python - Creating an Origin/Destination (OD) cost matrix using ArcPy


I have created a python script that runs an OD cost matrix over five years from 1999 to 2004. I have set up an OD cost matrix for each year, which I think is very cumbersome.



Is there someone who can show me how I can do this with a loop in python (arcpy) instead of setting up the OD cost matrix for each year?


In my origins and Destinations I also have a field called HOSPITALS. In the OD cost matrix, this field should be connected to each other so there will only be an analysis of each origins to Destination.


How is this done?


import arcpy

from arcpy import env

arcpy.env.overwriteOutput = True
#Set environments
arcpy.env.workspace = "C:\Tom\OPPDRAG_2013\Sykehus"



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

#Set local variables
inNetworkDataset = "C:\Tom\Elveg\ELVEG_Nettverk_2013\ELVEG_Nettverk_2013\ELVEG_Nettverk_2013.gdb\ELVEG_Nettverk\ELVEG_Nettverk_ND_2013"
outNALayer_1999 = "Pasienter_til_Sykehus_Matrix_1999"
outNALayer_2000 = "Pasienter_til_Sykehus_Matrix_2000"
outNALayer_2001 = "Pasienter_til_Sykehus_Matrix_2001"

outNALayer_2002 = "Pasienter_til_Sykehus_Matrix_2002"
outNALayer_2003 = "Pasienter_til_Sykehus_Matrix_2003"
impedanceAttribute = "Drivetime"
accumulateAttributeName = ["Minutes"]
inOrgins = "Pasienter_10.shp"
inDestinations = "Tre_Sykehus.shp"
outLayerFile_1999 = "C:\Tom\OPPDRAG_2013\Sykehus" + "/" + "outNALayer_1999" + ".lyr"
outLayerFile_2000 = "C:\Tom\OPPDRAG_2013\Sykehus" + "/" + "outNALayer_2000" + ".lyr"
outLayerFile_2001 = "C:\Tom\OPPDRAG_2013\Sykehus" + "/" + "outNALayer_2001" + ".lyr"
outLayerFile_2002 = "C:\Tom\OPPDRAG_2013\Sykehus" + "/" + "outNALayer_2002" + ".lyr"

outLayerFile_2003 = "C:\Tom\OPPDRAG_2013\Sykehus" + "/" + "outNALayer_2003" + ".lyr"

#arcpy.ImportToolbox("C:\Tom\Toolboxes\ArcPyToolBox.tbx")


#########################################


# Process: Make Feature Layer (2)
#arcpy.MakeFeatureLayer_management(inOrgins, "PasienterLyr", "\"SYKEHUS\" = '%SYKEHUS%'")



########################################

arcpy.MakeFeatureLayer_management (inOrgins, "Pas_Lyr1999")
arcpy.SelectLayerByAttribute_management ("Pas_Lyr1999", "NEW_SELECTION", " f_aar = '1999' ")

#Create a new OD Cost matrix layer.
arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayer_1999, "Minutes", "", "", "Minutes")


#Load the locations as origins.
arcpy.AddLocations_na(outNALayer_1999, "Origins", "Pas_Lyr1999", "Name f_aar #","1000 Meters", "SYKEHUS")

#Load the locations as destinations
arcpy.AddLocations_na (outNALayer_1999, "Destinations", inDestinations, "Name SYKEHUS #","1000 Meters", "SYKEHUS")


#Solve the OD cost matrix layer
arcpy.Solve_na(outNALayer_1999)


#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths
arcpy.SaveToLayerFile_management(outNALayer_1999, outLayerFile_1999,"RELATIVE")

#####################################

arcpy.MakeFeatureLayer_management (inOrgins, "Pas_Lyr2000")
arcpy.SelectLayerByAttribute_management ("Pas_Lyr2000", "NEW_SELECTION", " f_aar = '2000' ")

#Create a new OD Cost matrix layer.

arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayer_2000, "Minutes", "", "", "Minutes")

#Load the locations as origins.
arcpy.AddLocations_na(outNALayer, "Origins", "Pas_Lyr2000", "Name f_aar #","1000 Meters", "SYKEHUS")

#Load the locations as destinations
arcpy.AddLocations_na (outNALayer, "Destinations", inDestinations, "Name SYKEHUS #","1000 Meters", "SYKEHUS")


#Solve the OD cost matrix layer

arcpy.Solve_na(outNALayer_2000)

#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths
arcpy.SaveToLayerFile_management(outNALayer_2000, outLayerFile_2000,"RELATIVE")

#####################################

arcpy.MakeFeatureLayer_management (inOrgins, "Pas_Lyr2001")
arcpy.SelectLayerByAttribute_management ("Pas_Lyr2001", "NEW_SELECTION", " f_aar = '2001' ")


#Create a new OD Cost matrix layer.
arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayer_2001, "Minutes", "", "", "Minutes")

#Load the locations as origins.
arcpy.AddLocations_na(outNALayer, "Origins", "Pas_Lyr2001", "Name f_aar #","1000 Meters", "SYKEHUS")

#Load the locations as destinations
arcpy.AddLocations_na (outNALayer, "Destinations", inDestinations, "Name SYKEHUS #","1000 Meters", "SYKEHUS")



#Solve the OD cost matrix layer
arcpy.Solve_na(outNALayer_2001)

#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths
arcpy.SaveToLayerFile_management(outNALayer_2001, outLayerFile_2001,"RELATIVE")

#####################################


arcpy.MakeFeatureLayer_management (inOrgins, "Pas_Lyr2002")
arcpy.SelectLayerByAttribute_management ("Pas_Lyr2002", "NEW_SELECTION", " f_aar = '2000' ")

#Create a new OD Cost matrix layer.
arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayer_2002, "Minutes", "", "", "Minutes")

#Load the locations as origins.
arcpy.AddLocations_na(outNALayer, "Origins", "Pas_Lyr2002", "Name f_aar #","1000 Meters", "SYKEHUS")

#Load the locations as destinations

arcpy.AddLocations_na (outNALayer, "Destinations", inDestinations, "Name SYKEHUS #","1000 Meters", "SYKEHUS")


#Solve the OD cost matrix layer
arcpy.Solve_na(outNALayer_2002)

#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths
arcpy.SaveToLayerFile_management(outNALayer_2002, outLayerFile_2002,"RELATIVE")


#####################################

arcpy.MakeFeatureLayer_management (inOrgins, "Pas_Lyr2003")
arcpy.SelectLayerByAttribute_management ("Pas_Lyr2003", "NEW_SELECTION", " f_aar = '2000' ")

#Create a new OD Cost matrix layer.
arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayer_2003, "Minutes", "", "", "Minutes")

#Load the locations as origins.
arcpy.AddLocations_na(outNALayer, "Origins", "Pas_Lyr2003", "Name f_aar #","1000 Meters", "SYKEHUS")


#Load the locations as destinations
arcpy.AddLocations_na (outNALayer, "Destinations", inDestinations, "Name SYKEHUS #","1000 Meters", "SYKEHUS")


#Solve the OD cost matrix layer
arcpy.Solve_na(outNALayer_2003)

#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths

arcpy.SaveToLayerFile_management(outNALayer_2003, outLayerFile_2003,"RELATIVE")


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...