0

I'm using ArcPy and would like to copy features in a feature class to geometry objects and then perform a buffer on the geometry objects and copy them back to a feature class.

I am doing this in hopes that it will provide the performance and speed improvements that will allow a tool to run on larger datasets that are currently being hung up and/or failing in the normal buffer tool.

My question is, what is the best strategy for preserving an attribute field in this process? I need to preserve at least one ID field to join the original fields back to the buffered shapes after converting back to a feature class. Is this possible?

Here is my process so far for getting the geometries buffered:

import arcpy
i = 'PCPP_sites'
o = 'C:\buffer_test'
geometries = arcpy.CopyFeatures_management(i,arcpy.Geometry())
buffer_geom = []
for geom in geometries:
    b = geom.buffer(1)
    buffer_geom.append(b)
arcpy.CopyFeatures_management(buffer_geom, o)

How do I preserve attributes?

  • Any specific reason to not performing the buffer on the Feature Class using the Buffer tool? – Marcelo Villa Jun 5 at 19:11
  • The buffer tool is failing/getting hung up on larger datasets. I'm attempting a workaround that will hopefully be more efficient for large datasets since it seems from my tests that buffering the geometry objects instead of the features is faster/uses less memory. Hoping to avoid breaking up the larger datasets as this is part of a broader tool that accepts all sizes of inputs. – mmoore Jun 5 at 19:24
  • 2
    I would use search and insert cursors to read and write your geometries and their attributes either side of buffering your geometry. – PolyGeo Jun 5 at 19:33
  • 1
    Perhaps update cursor on a copy of original. – FelixIP Jun 5 at 19:53
  • I may try the update cursor as well and compare to the insert cursor below. Thanks for the suggestion! – mmoore Jun 5 at 23:06
2

I would use data access search and insert cursors after creating a new feature class. This will most likely be slower than the Buffer tool but it may be more successful, or you may be able to find your problematic geometry with try/except statements.

Basic script:

import arcpy
import os
i = 'PCPP_sites'
o = 'C:\buffer_test'
outPath, outName = os.path.split (o)
#new feature class with input as template
o = arcpy.CreateFeatureclass_management (outPath, outName, "POLYGON", i,
                                         spatial_reference = i) [0]

flds = ["list", "all", "shared", "fields"] #use arcpy.ListFields (), manual entry, or some other means to determine shared fields
flds += ["SHAPE@"] #add geom token to fields

#iterate input, get geometry, insert into output
with arcpy.da.SearchCursor (i, flds) as sCurs:
    with arcpy.da.InsertCursor (o, flds) as iCurs:
        for row in sCurs:
            row = list (row)
            geom = row [-1] #get geom object
            b = geom.buffer(1)
            row [-1] = b #add buffer to row
            iCurs.insertRow (row)
  • I'm hoping this will do the trick! It is currently working on a small dataset and will be testing on the large dataset that was not working with the Buffer tool. Thanks! – mmoore Jun 5 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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