3

I'm trying to find a code that will get the values from a specific columm (width) of a lines feature dataset and sum it with a fixed set of values (10, 20 and 30 meters) in order to apply a Multiple Ring Buffer.

Attribute table: enter image description here

The code sample below uses the fixed set of values (10, 20 and 30 meters) but not the width values of my lines shapefile.

import arcpy
arcpy.env.workspace = "C:/data/Py.gdb"
outFeatureClass = "c:/output/multibuffer"
distances = [10,20,30]
bufferUnit = "meters"
Field_Name = ""
dissolveType = "ALL"
arcpy.MultipleRingBuffer_analysis("lines", outFeatureClass, distances, bufferUnit, Field_Name, dissolveType)

enter image description here

  • Add a screenshot of the attribute table – BERA Jan 26 '18 at 10:46
  • 2
    Just run the Merge tool to combine the outputs? As your loop is creating output names store that in a list and pass that into a Merge tool. – Hornbydd Jan 26 '18 at 11:15
3

As @Hornbydd mentions in comments, merging all feature class will do it for you.

import arcpy
arcpy.env.workspace = "C:/data/Py.gdb"
fc = "lines"
distances = [10,20,30]

#empty list for feature classes
fcs = []
with arcpy.da.SearchCursor(fc, ['SHAPE@', 'width','OID@']) as cursor:
    for geom, width, oid in cursor:
        arcpy.MultipleRingBuffer_analysis(Input_Features=geom,
                                         Output_Feature_class='Multibuffer_{0}'.format(oid),
                                         Distances=[i+width for i in distances],
                                         Buffer_Unit='meters',
                                         Dissolve_Option='ALL')
        #add buffer to feature class list
        fcs += ['Multibuffer_{0}'.format(oid)]

#merge feature classes
arcpy.Merge_management (fcs, "Multibuffers")

#clean up
for fc in fcs:
    arcpy.Delete_management (fc)

If you want to go faster, create a new feature class, create your buffers in memory, and insert the geometries as you go.

import arcpy
arcpy.env.workspace = "C:/data/Py.gdb"
fc = "lines"
distances = [10,20,30]

#fc spatial refernce
sr = arcpy.Describe (fc).spatialReference
#create feature class
arcpy.CreateFeatureclass_management ("C:/data/Py.gdb", "Multibuffers", "POLYGON", spatial_reference = sr)
#insert cursor to insert geometries into feature class
with arcpy.da.InsertCursor ("Multibuffers", ["SHAPE@"]) as iCurs:
    with arcpy.da.SearchCursor(fc, ['SHAPE@', 'width','OID@']) as cursor:
        for geom, width, oid in cursor:
            #buffer in memory
            arcpy.MultipleRingBuffer_analysis(Input_Features=geom,
                                             Output_Feature_class=r'in_memory\buffs',
                                             Distances=[i+width for i in distances],
                                             Buffer_Unit='meters')

            #iterate buffers and add to output feature class
            with arcpy.da.SearchCursor (r'in_memory\buffs', ["SHAPE@"]) as sCurs:
                for row in sCurs:
                    iCurs.insertRow (row)

            #delete in memory buffers
            arcpy.Delete_management (r'in_memory\buffs')
  • Thank you Emil, I am gonna go for the second solution since my real dataset is made of hundred of thousands of roads. I hope it will not saturate the memory but I guess the buffers are stored in the memory the same way when you launch a standard buffer from the toolbox. – ePoQ Jan 26 '18 at 16:34
  • With my code only one multi ring buffer is stored in memory at a time. – Emil Brundage Jan 26 '18 at 16:42

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.