TransWikia.com

ArcPy script extremely slow with Search and Update Cursor

Geographic Information Systems Asked by GIS_USAr on July 16, 2021

I have developed a Python script to automatically retrieve the maximum height in a certain distance around my input polygon feature class. The height should be calculated for each 5 meters.

For a small input with around 50 polygon features, the script takes around 5 minutes. Whenever I input a really large dataset (10.000 features), the script gets extremely slow and after hours interrupts.

My (pseudo) code is as follows:

with arcpy.da.SearchCursor(fc, ['FID']) as rows:
        for row in rows:
            my_fid = row[0]
            expression = '"FID" = ' + str(my_fid)
            fc_tmp = r"fc.shp"
            arcpy.Select_analysis(fc, fc_tmp, expression)
            
            output_tmp = r"output_tmp.shp"
            arcpy.management.CreateFeatureclass(r"", output_tmp)

            -- pseudo code
            # 1) Buffer  feature class
            # 2) Clip DGM to raster
            # 3) convert feature class to polyline
            # 4) Make points along polyline
            # 5) make tmp result feature class
            # 6) Buffer points
            # 7) get max height in buffer around points using zonal stats
            # 8) join max height with tmp result feature class
            
            with arcpy.da.UpdateCursor(output_tmp, ["FID"]) as recs:
                for rec in recs:
                    rec[0] = my_fid
                    recs.updateRow(rec)
                del rec, recs

            arcpy.management.Append(output_tmp, r"myResult.shp", "NO_TEST")
            arcpy.management.Delete(output_tmp)
            arcpy.management.Delete(fc_tmp)
        del row, rows 

Can it have to do something with the cursors?
I thought maybe the selection takes long because the feature has to be selected from 10.000 features, but the selection is done very fast.
I do not understand that when I input 10.000 features, also the first 50 features are not done within 5 minutes, although when I input onyl 50 features, it does not take longer than 5 minutes. I just tested it, the first 3 features are done within 20 minutes. Thats absolutely disproportionate.

Any ideas?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP