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.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 
    Neighbors = "Neighbors_2" + str(i)
    whereclause2 = output_table_2+'.src_OBJECTID='+str(i)    
    #print whereclause2
    target_neighbor_parcels = "target_neighbor_parcels_2" + str(i)
    centroide = "centroide_2" + str(i)
    cur = arcpy.UpdateCursor(centroide)
    arcpy.AddField_management(centroide,"Gisement", "DOUBLE")
    arcpy.AddField_management(centroide,"Orientation", "TEXT")
    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)
        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)
  • 2
    Are you executing in IDLE, as a script tool, or in the ArcMap Python window? A general note: I often find it useful to include timestamps in my code, particularly after each geoprocessing step. This shows you directly how much time each tool takes and can help focus troubleshooting.
    – Erica
    Commented Dec 16, 2014 at 12:42
  • 3
    You should replace your update cursors with arcpy.da.UpdateCursor this should improve cursor performance. Commented Dec 16, 2014 at 12:57
  • 1
    Please update the question with the version of ArcGIS in use. Have you built spatial indexes? Your application appears to be busier than necessary.
    – Vince
    Commented Dec 16, 2014 at 13:01
  • Thank you all for your answers ! Erica, i'am just compiling in Python windows, because under IDLE, i got a lot of errors !
    – geosevda
    Commented Dec 16, 2014 at 13:04
  • Vince i'am using Arcgis 10.1
    – geosevda
    Commented Dec 16, 2014 at 13:05

3 Answers 3


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.


I would confirm as @Vince suggests if your input datasets have spatial indices. If your inputs are Shapefiles then these by default do not have spatial indices and need building.

Another suggestion that will speed up your code is to replace the cursor you are using with a cursor from the da module as described here.

  • In fact, i dont know what are you meaning by builduig, and how it can help in my case !!
    – geosevda
    Commented Dec 16, 2014 at 14:51
  • Just run the tool I linked to on your input datasets so the spatial indices are built then run your code above.
    – Hornbydd
    Commented Dec 16, 2014 at 15:47

I don't totally understand the output of your analysis, but it seems like that for each feature being evaluated you're doing a lot fiddling - copying datatsets, joining them, adding fields, deleting them. If you don't need to do them every time do them once outside your loop.

Some benchmarking might help you figure out where the bottleneck is. You can do something like this, adding these lines at the top of your file:

import time
start_time = time.time()

and then where ever you want to see the elapsed time, add:

print time.time() - start_time
  • Hi Marc, really i know that my code is not perfect, but it just do what i want !! because i just don't know if there is an other way to do it ! i will edit my question and explain what i'm trying to do step by step, and i really hope get a help to resume some blocks of code on few lines ! if it is possible of course !
    – geosevda
    Commented Dec 19, 2014 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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