Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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")
share|improve this question

1 Answer 1

I would put the core of your code within a "for" loop. Something like this:

for year in range(1999,2005): #python doesn't include 2005 here and will only go up to 2004
  outNALayer_Year = 'outNALayer'+str(year)
  Pas_Lyr_Year = 'Pas_Lyr'+str(year)

  arcpy.MakeFeatureLayer_management (inOrgins, Pas_Lyr_Year)
  arcpy.SelectLayerByAttribute_management (Pas_Lyr_Year, "NEW_SELECTION", " f_aar = str(year) ")

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

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

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

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

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

Anytime you reference outNALayer_1999, outNALayer_2000 and so on, replace it with outNALayer_Year. Same goes for Pas_Lyr1999, etc.

Note, I haven't tested this, but that's the logic I would use.

share|improve this answer
    
But how can I link the Orgins and Destinations based on the HOSPITALS field? –  user15888 May 30 '13 at 6:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.