[GIS] Executing Python code takes a lot of time (more than 2 hours)? (Arcgis10.1)

arcgis-10.1arcpyperformancepython

I am executing this Python code, in which I am trying to determinate the position of neighbor parcel (north, south, east, west).

It works fine but it took 2 hours for just 50 parcels!

I think that there is something going wrong. Is this execution time normal? Do you have any suggestions to make it faster?

I'am usingr arcgis10.1, arcpy, Python2.7
import arcpy
import math
import arcview

arcpy.env.workspace = "D:/Users/saadiya/Documents/ArcGIS/Default1.gdb"
arcpy.env.overwriteOutput = True
workspace = "C:/Saadia/SAFTOP/Parcelle et rivrains/simulation arcgis"

for i in range (1,50) :   
    expression = '"src_OBJECTID"=' + str(i)
    output_table = "table" + str(i)
    arcpy.PolygonNeighbors_analysis("P_dataset","Voisins") 
    arcpy.TableSelect_analysis("Voisins",output_table, expression)
    arcpy.MakeFeatureLayer_management("P_dataset", "Parcelles_class1_copy")
    arcpy.AddJoin_management("Parcelles_class1_copy","OBJECTID", output_table,"nbr_OBJECTID","KEEP_ALL")
    target_parcel = "target_parcel_2" + str(i)
    whereclause1 = '"P_dataset.OBJECTID"=' + str(i)
    #print whereclause1 
    arcpy.Select_analysis("Parcelles_class1_copy",target_parcel,whereclause1)
    Neighbors = "Neighbors_2" + str(i)
    whereclause2 = output_table_2+'.src_OBJECTID='+str(i)    
    #print whereclause2
    arcpy.Select_analysis("Parcelles_class1_copy",Neighbors,whereclause2)
    arcpy.RemoveJoin_management("Parcelles_class1_copy",output_table)
    target_neighbor_parcels = "target_neighbor_parcels_2" + str(i)
    arcpy.Union_analysis([target_parcel,Neighbors],target_neighbor_parcels,"ALL")
    centroide = "centroide_2" + str(i)
    arcpy.FeatureToPoint_management(target_neighbor_parcels,centroide)
    arcpy.AddXY_management(centroide)
    cur = arcpy.UpdateCursor(centroide)
    arcpy.AddField_management(centroide,"Gisement", "DOUBLE")
    arcpy.AddField_management(centroide,"Orientation", "TEXT")
    arcpy.Delete_management(output_table)
    arcpy.Delete_management(target_parcel)
    arcpy.Delete_management(Neighbors)
    arcpy.Delete_management(target_neighbor_parcels)
    cur = arcpy.UpdateCursor(centroide)
    for row in cur :
        a = output_table_2+"_src_FID_1"
        #print a
        condition = row.getValue(a)
        if (condition == 0) :
            PointX = row.getValue("POINT_X")
            PointY = row.getValue("POINT_Y")     
            end_x = PointX 
            end_y = PointY 
        elif (condition != 0) :
            PointX = row.getValue("POINT_X")
            PointY = row.getValue("POINT_Y")
         #print end_x
         #print end_y
         #print PointX
         #print PointY
        dx = (end_x - PointX)
        dy = (end_y - PointY)
        #print dx
        #print dy
        azimuth = math.atan2(dx, dy)
        #print azimuth
        azimuth_gr = azimuth*200/3.14519
        print azimuth_gr
        if (dy>=0 and dx>= 0 ):
           print 1
           azimuth_correct = azimuth_gr
        elif (dy>=0 and dx<= 0 ):
              print 2
              azimuth_correct = 200- azimuth_gr
        elif(dy<=0 and dx<= 0 ):
                 print 3
                 azimuth_correct = 200 + azimuth_gr
        elif(dy<=0 and dx>= 0 ):
                        print 4
                        azimuth_correct = 400 - azimuth_gr
        print azimuth_correct
        row.setValue("Gisement", azimuth_correct)
        cur.updateRow(row)
        Position = row.getValue("Gisement")
        #print Position
        if (Position > 45 and   Position <= 135):
                         Orient = "Est"
        elif (Position > 135 and Position <= 225):
                         Orient = "Sud"
        elif (Position > 225 and Position <= 315):
                         Orient = "Ouest"
        elif (Position > 315 and Position <= 400):
                         Orient = "Nord"
        elif (Position > 0 and Position <= 45):
                         Orient = "Nord"
        elif (Position == 0):
                         Orient = ""                 
        print Orient
        row.setValue("Orientation", Orient)
        cur.updateRow(row)

Best Answer

I had the same experience, but I realized that when I converted the script to an ArcGIS script tool, the execution time went down to 12 minutes. A script tool is like a toolbox. You connect your script to the tool. You will need to add a few lines in your script to set the parameters. This is done through the GetParameterAsText ArcPy function. This http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001500000006000000.htm provides a quick tour on how to make script tools.

Related Question