Select to view content in your preferred language

Setting Workspace

903
2
08-02-2011 10:32 AM
AliciaSoto
Frequent Contributor
Hello,

I'm a python and arcpy beginner. I have a few simple scripts I'm working on for practice, but I'm having trouble with this one.

Purpose = Create a 200 ft buffer around a user selected feature.

Issue = I would like the 'output feature class' to default to a certain file path. I attempted to do this using arcpy.env.workspace. However, the default path keeps pointing to my C:\....Documents\ArcGIS\Default.gdb

I've looked at some other examples and cannot figure out what I am doing wrong. I'd appreciate any help. Thanks.

import arcpy
from arcpy import env
import os

#----------------------------------
# Set the geoprocessing environment
#----------------------------------

env.workspace = r"J:\Work\Projects\Case Change Maps\Python\Temp"
env.overwriteOutput = True

#----------------------------------
# Get Parameters from the user
#----------------------------------

DataToBuffer = arcpy.GetParameterAsText(0)
if DataToBuffer == '#' or not DataToBuffer:
    DataToBuffer = "BASE.BASEMAP" # provide a default value if unspecified

BufferName = arcpy.GetParameterAsText(1)
if BufferName == '#' or not BufferName:
    BufferName = "BUFF.shp" # provide a default value if unspecified

#----------------------------------
# Process
#----------------------------------

# Buffer the basemap selection
arcpy.Buffer_analysis(DataToBuffer, BufferName, "200 Feet", "FULL", "ROUND", "NONE", "")
Tags (2)
0 Kudos
2 Replies
AndrewPerry
New Contributor
I think your safest bet would be to create an output folder location as another option for the script tool:

env.scratchWorkspace = arcpy.GetParameterAsText(2)

then call it something like "Save To" and as a data type = Workspace.

If a person doesn't specify a location for their files to be dropped arcmap throws it in the default.gdb.
0 Kudos
AliciaSoto
Frequent Contributor
Thank you for the advice adperry. I went back and restructured my code.

The script now works the way I had originally planned.The user selects a feature and a 200ft buffer shapefile is created and saved in a specific folder. The only question I have left now is how to get the resulting shapefile to appear in the TOC. I've tried using the arcpy.mapping.AddLayer() module, but I think I'm missing something.....


# ---------------------------------------------------------------------------
# Description:
#   Creates a 200 ft buffer around a user selected feature
# ---------------------------------------------------------------------------

import arcpy
from arcpy import env
import os

#----------------------------------
# Set the geoprocessing environment
#----------------------------------

OutputBufferData = r"J:\Work\Projects\Case Change Maps\TempWorkspace\Buffer.shp"
env.overwriteOutput = True

#----------------------------------
# Get Parameters from the user
#----------------------------------

DataToBuffer = arcpy.GetParameterAsText(0)

#----------------------------------
# Processes
#----------------------------------

# Buffer the user selection
arcpy.Buffer_analysis(DataToBuffer, OutputBufferData, "200 Feet", "FULL", "ROUND", "NONE", "")

# Add results to the TOC
mxd = arcpy.mapping.MapDocument(r"J:\Work\Projects\Case Change Maps\Testing.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
addLayer = arcpy.mapping.Layer(OutputBufferData)
arcpy.mapping.AddLayer(df,addLayer,"TOP")
0 Kudos