网络分析员(ArcGIS 9.3)出现循环错误



我正在使用Network Analyst的Make Service Area Layer工具进行循环,但它不起作用。我想制作3个不同的服务区,每个服务区有3个不同点,每个点都与一个设施相匹配。设施是作为新网络位置来源的要素类或图层。这些要点称之为"盔甲"、"布格奈斯"one_answers"Chantrerie"。我的阻抗属性是"Minutes_S1"

这是我的代码:

import arcgisscripting
gp = arcgisscripting.create()
gp.CheckOutExtension("Network")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Network Analyst Tools.tbx")
network = "D:/Travaux/NantesMetropole/Traitements/SIG_Final/Modelisation/voiture/Reseau_Voiture_TOP_ND.nd" 
Armor = "D:/Travaux/NantesMetropole/Traitements/SIG_Final/pts_kml/Pt_Ar.shp"
Bouguenais = "D:/Travaux/NantesMetropole/Traitements/SIG_Final/pts_kml/Pt_Boug.shp"
Chantrerie = "D:/Travaux/NantesMetropole/Traitements/SIG_Final/pts_kml/Pt_Chanr.shp"
facilities_points = ["Armor", "Bouguenais", "Chantrerie"]
while 1:
    for i in facilities_points:
        try:
            gp.MakeServiceAreaLayer_na (network, "Service Area_" + i, "Minutes_S1", "TRAVEL_FROM", "10 20 30 40 50", "SIMPLE_POLYS", "MERGE", "DISKS", "NO_LINES", "OVERLAP", "NO_SPLIT", "", "", "ALLOW_UTURNS", "", "TRIM_POLYS", "200 Meters", "NO_LINES_SOURCE_FIELDS")
            gp.AddLocations_na("Service Area_" + i, "Facilities_" + i, i, "", "100 Meters")
            gp.Solve_na ("Service Area_" + i, "HALT")
            gp.CopyFeatures_management("Service Area_" + i + "/Polygons", "D:/Travaux/NantesMetropole/Traitements/SIG_Final/Modelisation/voiture/test_" + i +".shp")
            print "Minutes_S1 done on", i
        except:
             print "Error on", i
    break

事实上,在"gp.AddLocations_na"行上已经发生了一个错误,上面写着:

Error on Armor
Error on Bouguenais
Error on Chantrerie

我查看了ESRI的帮助,但我仍然不知道如何解决我的问题。你能帮我吗?

您说gp.AddLocations_na导致了问题,但从您的代码中看并不明显。若要调试,请在except块中添加一个raise语句。这将告诉您是哪一行导致错误。当你解决了问题时,你总是可以把它取下来。

看起来孤独的我才是罪魁祸首。您将它作为字符串传递,而它可能应该是要素类变量。您可以使用字典来存储名称和变量。

#store table/feature class names with the variables
facilities_points = {"Armor":Armor, "Bouguenais":Bouguenais, "Chantrerie":Chantrerie}
#note the while loop is superfluous here
while 1:
    for i in facilities_points:
        try:
            gp.MakeServiceAreaLayer_na (network, "Service Area_" + i, "Minutes_S1", "TRAVEL_FROM", "10 20 30 40 50", "SIMPLE_POLYS", "MERGE", "DISKS", "NO_LINES", "OVERLAP", "NO_SPLIT", "", "", "ALLOW_UTURNS", "", "TRIM_POLYS", "200 Meters", "NO_LINES_SOURCE_FIELDS")
            gp.AddLocations_na("Service Area_" + i, "Facilities_" + i, facilities_points[i], "", "100 Meters")
            gp.Solve_na ("Service Area_" + i, "HALT")
            gp.CopyFeatures_management("Service Area_" + i + "/Polygons", "D:/Travaux/NantesMetropole/Traitements/SIG_Final/Modelisation/voiture/test_" + i +".shp")
            print "Minutes_S1 done on", i
        except:
            #this error message tells you nothing about what really happened
            print "Error on", i
            #Raise the full exception to assist with debugging
            #comment it out once the script works
            raise
    break 

顺便说一句,尽管错误处理是一个好习惯,但在我看来,它对这种类型的脚本编写几乎没有帮助。除非您愿意接受失败的地理处理函数调用并继续,否则最好让脚本停止在其轨道上并返回错误发生的位置。但是,如果您实现了日志记录来捕获回溯,并且将其作为计划任务运行,那么except块将是放置日志消息的地方。

最新更新