Selecting attribute tool for a layer

488
2
04-23-2019 06:19 PM
WilliamVan_Eck
New Contributor

Hello:

I am trying to run the select attribute tool for a layer that I created.  I can manually run the tool in ArcMap, but when I write the script the code fails after the copy feature tool was used.  Not sure what I am doing wrong, I am using the ArcMap help section to get a general idea of the code that I needed. 

I am new to python, so I appreciate any guidance anyone has:

#Import a CSV file that will show you what ASOS weather stations are in the state.
import arcpy
import os 

#Lets you write over the shape file if needed
arcpy.env.overwriteOutput = True

arcpy.env.workspace = "C:\\GEOG485\\FinalProject"

#Latitude and Longitude Coordinates 
xFieldName = 'LON'
yFieldName = 'LAT'

#Output folder
outFolder = "C:\\GEOG485\\FinalProject\\Output"

#ASOS stations layer
eventLayer = "ASOS"
outPutlayer = "C:\\GEOG485\\FinalProject\\FinalProject.gdb\\CurrentStations"
stateLayer = "C:\\GEOG485\\FinalProject\\FinalProject.gdb\\State_ASOS"

#Spatial Reference
spatialRef = arcpy.SpatialReference(4326)

#CSV file that will be imported into ArcMap
csvFilePath = "C:\\GEOG485\\FinalProject\\isd-history.csv"

#Select state of interest
targetState = 'NJ'

#Date of file last updated
targetEnd = '20190329'


try:
    #import csv file using the xy event layer tool
    arcpy.MakeXYEventLayer_management(csvFilePath, xFieldName, yFieldName, eventLayer, spatialRef)
    print ("success1")

    #Copy features from the XY Event Layer in order to asign Object ID's to the rows in the file
    arcpy.CopyFeatures_management(eventLayer, outPutlayer)
    print ("success2")

    stateQuery = '"STATE" = ' + "'" + targetState + "'"
    #Select Attribute management to narrow down the state of interest
    arcpy.SelectLayerByAttribute_management(outPutlayer, "NEW_SELECTION", stateQuery)
    print ("success4")
    
    arcpy.SelectLayerByAttribute_management(outPutlayer, 'SUBSET_SELECTION', '"END" = ' + "'" + targetEnd + "'")
    print ("success5")

    arcpy.SaveToLayerFile_management(outPutlayer, stateLayer)

except:
    print ("error2")

try:
    mxd = arcpy.mapping.MapDocument("C:\\GEOG485\\FinalProject\\basemap.mxd")
    df = arcpy.mapping.ListDataFrame(mxd, "*")[0]
    newLayer = arcpy.mapping.Layer(outPutlayer)
    arcpy.mapping.Layer(df, newLayer, "Bottom")
except:
    print ("error4")
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

You need a 'feature layer' for select layer by attribute, so make it from the 

Make Feature Layer—Data Management toolbox | ArcGIS Desktop 

curtvprice
MVP Esteemed Contributor

Dan, I don't understand your suggestion, as MakeXYEventLayer also makes a feature layer.

You may have a better luck if you have a better except block, that is, one that will report the error from the copy features tool (or whatever else is failing). It is possible copy features is trying to overwrite a file that has a lock on it or something.

Error handling with Python—Help | ArcGIS Desktop 

Good luck on your last week of class! Ho ho!

0 Kudos