1

Im trying to create a script that will created two buffer shapefiles using SelectLayerByAttribute, but same error keeps popping up. This tool runs fine in Modelbuilder, but I want to create a script that will be able to automate the entire mapping process and export pdfs.

Here is what I have so far:

# Parcels.py
# Created on: 2018-06-01 10:22:37.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Parcels <Parcels> <Selection_type> <Distance__value_or_field_> <Distance__value_or_field___2_> <Expression> <v300ft_shp> <v500ft_shp> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Script arguments
Parcels = arcpy.GetParameterAsText(0)

Selection_type = arcpy.GetParameterAsText(1)
if Selection_type == '#' or not Selection_type:
    Selection_type = "NEW_SELECTION" # provide a default value if unspecified

Distance__value_or_field_ = arcpy.GetParameterAsText(2)
if Distance__value_or_field_ == '#' or not Distance__value_or_field_:
    Distance__value_or_field_ = "300 Feet" # provide a default value if unspecified

Distance__value_or_field___2_ = arcpy.GetParameterAsText(3)
if Distance__value_or_field___2_ == '#' or not Distance__value_or_field___2_:
    Distance__value_or_field___2_ = "500 Feet" # provide a default value if unspecified

Expression = arcpy.GetParameterAsText(4)
if Expression == '#' or not Expression:
    Expression = "\"AIN\" = ''" # provide a default value if unspecified

v300ft_shp = arcpy.GetParameterAsText(5)

v500ft_shp = arcpy.GetParameterAsText(6)

# Local variables:
Parcel_Address = Parcels

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Parcels, Selection_type, Expression)

# Process: Buffer
arcpy.Buffer_analysis(Parcel_Address, v300ft_shp, Distance__value_or_field_, "FULL", "ROUND", "NONE", "", "PLANAR")

# Process: Buffer (2)
arcpy.Buffer_analysis(Parcel_Address, v500ft_shp, Distance__value_or_field___2_, "FULL", "ROUND", "NONE", "", "PLANAR")

mxd = arcpy.mapping.MapDocument("CURRENT")

# your title has to have "title" in the element name box (Right-click on it \properties\size and position)
titleItem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title")[0]

# change the title's text
titleItem.text = "some new title"

I made the changes to the script and I no longer have issues with SelectLayerByAttribute. so, I'm trying to update the title and date text within the layout view, but when I run the code nothing changes. I have tried two different codes and they don't seem to work. Any suggestions?

# Import arcpy module
import arcpy

#Work Env
arcpy.env.workspace = "F:\APPDATA\GISMAPS\Projects\Planning\Requests\Bryan\2018\Planning\Teresita_care\Test_File"

# Script arguments
Parcels = arcpy.GetParameterAsText(0)

Selection_type = arcpy.GetParameterAsText(1)
if Selection_type == '#' or not Selection_type:
    Selection_type = "NEW_SELECTION" # provide a default value if unspecified

Distance__value_or_field_ = arcpy.GetParameterAsText(2)
if Distance__value_or_field_ == '#' or not Distance__value_or_field_:
    Distance__value_or_field_ = "300 Feet" # provide a default value if unspecified

Distance__value_or_field___2_ = arcpy.GetParameterAsText(3)
if Distance__value_or_field___2_ == '#' or not Distance__value_or_field___2_:
    Distance__value_or_field___2_ = "500 Feet" # provide a default value if unspecified

Expression = arcpy.GetParameterAsText(4)
if Expression == '#' or not Expression:
    Expression = "\"AIN\" = '7038006009'" # provide a default value if unspecified

v300ft_shp = arcpy.GetParameterAsText(5)

v500ft_shp = arcpy.GetParameterAsText(6)

# Local variables:
#Parcel_Address = Parcels

#If layer already exist overwrite
arcpy.env.overwriteOutput=True

#Create Parcels layer
arcpy.MakeFeatureLayer_management("F:/APPDATA/GISMAPS/Projects/Planning/Requests/Bryan/2018/Planning/Teresita_care/Test_File/Parcels.shp","Parcels.lyr")


# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management("Parcels.lyr", Selection_type, Expression)

# identify the current map document

mxd = arcpy.mapping.MapDocument('CURRENT')

# identify the data frame
df = arcpy.mapping.ListDataFrames(mxd)[0]

# use the zoom to selected features method of the data frame to update the extent
df.zoomToSelectedFeatures ()

#Set Scale
df.scale = 3860

# Process: Buffer
arcpy.Buffer_analysis("Parcels.lyr", "F:/APPDATA/GISMAPS/Projects/Planning/Requests/Bryan/2018/Planning/Teresita_care/Test_File/samv300ft_shp", Distance__value_or_field_, "FULL", "ROUND", "NONE", "", "PLANAR")

#Import Symbology layer to buffer
arcpy.ApplySymbologyFromLayer_management ("samv300ft_shp", "F:/APPDATA/GISMAPS/Projects/Planning/Requests/Bryan/2018/Planning/Teresita_care/Test_File/Symbol_300ft.lyr")

# Process: Buffer (2)
arcpy.Buffer_analysis("Parcels.lyr", "F:/APPDATA/GISMAPS/Projects/Planning/Requests/Bryan/2018/Planning/Teresita_care/Test_File/samv500ft_shp", Distance__value_or_field___2_, "FULL", "ROUND", "NONE", "", "PLANAR")

#Import Symbology layer to buffer
arcpy.ApplySymbologyFromLayer_management ("samv500ft_shp", "F:/APPDATA/GISMAPS/Projects/Planning/Requests/Bryan/2018/Planning/Teresita_care/Test_File/Symbol_300ft.lyr")

import arcpy

# set env (r"C:\Project\Project.mxd")
mxd = arcpy.mapping.MapDocument (r"F:\APPDATA\GISMAPS\Projects\Planning\Requests\Bryan\2018\Planning\Teresita_care\Test_File\Test_Script.mxd")

for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):

    if elm.text == "title":

        elm.text = "Andy"
#mxd.save()
#del mxd


#mxd = arcpy.mapping.MapDocument("CURRENT")

# your title has to have "title" in the element name box (Right-click on it \properties\size and position)
#titleItem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title*")[0]

# change the title's text
#titleItem.text = "Andy"
  • 4
    It should also give you a line number in your error, that would be nice to help narrow down the line that's causing your error. My best guess is that you're providing a feature class for your first argument where it needs to be a feature layer, add a line arcpy.MakeFeatureLayer_management to convert your feature class into a feature layer before select layer by location. – Michael Stimson Jun 12 '18 at 0:02
  • Welcome to GIS SE. As a new user, please take the Tour. Please remember to put the exact error message and exact software release in the body of the question. I certainly hope you aren't using the long-retired ArcGIS 10.0, since very few have it available anymore, and it was significantly less capable with respect to Python. – Vince Jun 12 '18 at 0:45
  • 1
    If the input for Parcels is a file on disk and not a layer in an mxd the code will throw an error. You cannot use a feature class/shapefile as an input directly, you have to use the MakeFeatureLayer tool on it first to make it a layer before passing it to the SelectLayerByAttribute tool. – Richard Fairhurst Jun 12 '18 at 14:06

Your Answer

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

Browse other questions tagged or ask your own question.