AnsweredAssumed Answered

Selecting attribute tool for a layer

Question asked by wzv6_PennStateGIS on Apr 23, 2019
Latest reply on Apr 23, 2019 by curtvprice



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'

    #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)

    print ("error2")

    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")
    print ("error4")