Processing math: 100%

3 votos

Creación de una matriz de costes Origen/Destino (OD) con ArcPy

He creado un script en python que ejecuta una matriz de costes de DO a lo largo de cinco años, de 1999 a 2004. He creado una matriz de costes de DO para cada año, lo que me parece muy engorroso.

¿Hay alguien que pueda mostrarme cómo puedo hacer esto con un bucle en python (arcpy) en lugar de configurar la matriz de costes de DO para cada año?

En mis orígenes y destinos también tengo un campo llamado HOSPITALES. En la matriz de costes de los OD, estos campos deben estar conectados entre sí, de modo que sólo se analice cada uno de los orígenes y destinos.

¿Cómo se hace?

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")

3voto

Amy Puntos 46

Yo pondría el núcleo de tu código dentro de un " para ". Algo como esto:

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")

Cada vez que haga referencia a outNALayer_1999 , outNALayer_2000 y así sucesivamente, sustitúyalo por outNALayer_Year . Lo mismo ocurre con Pas_Lyr1999 etc.

Nota, no he probado esto, pero esa es la lógica que yo usaría.

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X