I am taking a CSV file and importing it with the XY Event feature layer. I then am trying to use the Copy Feature tool to get "Object ID" assigned to each row in order to run an attribute query. My issues is that I can't get the Copy Feature layer to work properly. I believe that the issue is with the feature class I am trying to make using the tool.

I am new to python and can't seem to get this to work right. I am sure I have other issues with my code, but one step at a time.

When I do the try/expect the code works till the copy features layer, so I know that is probably my first of many issues.

#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 = "CurrentStations.shp"

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

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

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

    print ("error")

    mxd = arcpy.mapping.MapDocument("C:\\GEOG485\\FinalProject\\basemap.mxd")
    df = arcpy.mapping.ListDataFrame(mxd, "*")[0]
    newLayer = arcpy.mapping.Layer("currentasos")
    arcpy.mapping.Layer(df, newLayer, "Bottom")
    print ("error4")
  • 1
    Your try/except is masking a potentially useful error. Do line by line printing in between to see what the error is and update your question. – smiller Apr 22 at 21:15
  • 1
    You're writing to a folder but also not giving a file extension. Either make your workspace a gdb or give the output from copy features a. shp extension – smiller Apr 22 at 21:17
  • Is success (variable) defined somewhere? This will fail currently. If you want it to print the string "success", enclose in quotes. – smiller Apr 22 at 21:21
  • So i assigned a shp extension, but still having the same problem with FID being assigned to the rows instead of OID. – Will Apr 23 at 9:36
  • Didn't catch that you were looking for objectid earlier. You need a feature class in a geodatabase for that. Shapefiles have FID. support.esri.com/en/technical-article/000010834 – smiller Apr 23 at 12:00

There are a few syntax errors in your code. Overall, when testing out a new process be careful what you include in try/except blocks as they can mask helpful errors. When in doubt, remove it from the try/except and just do each line separately, printing each line (or print(arcpy.GetMessages()) ) to see what errors occur and at what stage.

A few things of note:

You're writing to a folder but also not giving a file extension. Either use a GDB as your workspace or give the output from copy features a. shp extension. If you explicitly want ObjectIDs (featureclasses) vs FIDs (shapefiles), set your workspace to a geodatabase. http://support.esri.com/en/technical-article/000010834

On an earlier edit you had a line printing success (variable) - Is this defined somewhere? This would fail if not. To print the string "success", enclose in quotes.

Before performing SelectByAttributes, create a layer. In the current edit of the Question, there is a reference to a feature class "currentasos"; use MakeFeatureLayer_management to create a layer first, as this is required for SelectByAttributes.

Your Answer

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

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