Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Good Afternoon

I am trying to write a python script which buffers a large number of features from a geodatabase (but not all) and then dissolves the newly created buffer shapefiles together. Currently I have only been able to achieve this by listing the specific locations of the newly created buffer shapefiles (which is OK for a couple of features but a nightmare with more). I am new to python and unsure how to references too (or ideally make a list) of the newly produced buffer shapefiles without individually listing there locations. This is also further complicated by producing 3 different size buffers for each feature class. Here is the first bit of my script before I carry out the dissolve. Many thanks in advance for any pointers.

# imports arcpy and os
import arcpy, os
from arcpy import env

# sets the workspace to my gdb
arcpy.env.workspace = "C:/GIS Home/Mining Features (MATTHEW).gdb"

# Output workspace location is defined
output_workspace = "C:/GIS Home/project_1"

# list of features to buffer in gdb
fcList = ["Shaft_Metalliferous", "Quarry_Metalliferous"]

for featureClass in fcList:

arcpy.Buffer_analysis(featureClass, output_workspace + "/" + featureClass[:-4] + "_b250.shp", 250, "", "", "ALL")
arcpy.Buffer_analysis(featureClass, output_workspace + "/" + featureClass[:-4] + "_b500.shp", 500, "", "", "ALL")
arcpy.Buffer_analysis(featureClass, output_workspace + "/" + featureClass[:-4] + "_b750.shp", 750, "", "", "ALL")
share|improve this question
2  
Just a suggestion, it's generally good practice to avoid spaces and special characters with naming conventions (your gdb name). –  Roy Sep 26 '12 at 12:17
add comment

2 Answers

up vote 3 down vote accepted

Firstly, welcome to the site @MattWalsh!

You've got the right idea with iterating through a loop and creating a list of output buffers to merge and dissolve together. I'm going to guess you want a separate output for each different buffer level, but hopefully this will be easy to change later if you want.

Also I'm assuming the buffers are only temporary so it doesn't matter where they end up, but again hopefully this will be easy to change. We'll use tempfile to handle this.

# imports arcpy and os.path (we're not using os)
import arcpy
import os.path
from arcpy import env
import tempfile

# sets the workspace to my gdb
arcpy.env.workspace = "C:/GIS Home/Mining Features (MATTHEW).gdb"

# Output workspace location is defined
output_workspace = "C:/GIS Home/project_1"
temp_workspace = tempfile.mkdtemp() #Make a temporary directory inside the system
                                    #temporary directory (so probably C:\Temp)


# list of features to buffer in gdb
fcList = ["Shaft_Metalliferous", "Quarry_Metalliferous"]

#create some empty lists for output
#note I'm only going to show for 250m, but you can do the same for 500 and 750
l_250 = []

for featureClass in fcList:
    #make some temporary output files
    b250 = os.path.join(temp_workspace, featureClass + "_b250.shp") #you don't need to use
                                                                    #[:-4] to get rid of
                                                                    #the last 4 characters,
                                                                    #as gdb files don't
                                                                    #have extensions

    arcpy.Buffer_analysis(featureClass, b250, 250, "", "", "ALL")

    #append the name of the output files to the lists
    l_250.append(b250)

#Merge features before dissolve
merge_250 = os.path.join(temp_workspace, "250_merge.shp")
arcpy.Merge_management(l_250, merge_250)

#Finally dissolve for output
dissolve_250 = os.path.join(output_workspace, "250_dissolve.shp")
arcpy.Dissolve_management(merge_250, dissolve_250)

As a last tip, if you're new to Python I'd strongly recommend taking a look at the Python Tutorial and getting a good IDE with Code Complete. Also, the ArcGIS arcpy documentation is pretty good as well.

share|improve this answer
    
Thankyou this has helped me a great deal! –  Matt Sep 26 '12 at 12:28
1  
Also of note is the creation of buffers in the "in_memory" workspace, which would help you avoid unnecessary writes/reads from the hard drive. –  Michalis Avraam Sep 26 '12 at 16:21
add comment

You need to create an empty list, then append the features the new empty list. Rather than building the output location within the method you can create it in advance, and store it as a variable. Then you can use that same variable to append to the list.

output_workspace = "C:/GIS Home/project_1"

dissolveList = [] #empty list
fcList = ["Shaft_Metalliferous", "Quarry_Metalliferous"]
buffer = [250, 500, 750]

for featureClass in fcList:'
    for buff in buffer:
        output = output_workspace + "/" + featureClass + "_b" + str(buff)

        arcpy.Buffer_analysis(featureClass, output, buff, "", "", "ALL")
        dissolveList.append(output) #append a variable (string) to the list
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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