1

I imported a CSV file using the MakeXYEventLayer_management and trying to use the SelectlayerByAttribute_management function, but I am getting error message saying on "Object ID(OID)" is present.. Is there a way to work around this?

Below is my code so far:

import arcpy
arcpy.env.overwriteOutput = True

xFieldName = 'LON'
yFieldName = 'LAT'
outFolder = "C:\\GEOG485\\FinalProject\\Output"
asos = "ASOS Stations"
spatialRef = arcpy.SpatialReference(4326)
csvFilePath = "C:\\GEOG485\\FinalProject\\isd-history.csv"
targetState = 'NJ'

arcpy.MakeXYEventLayer_management(csvFilePath, xFieldName, yFieldName, asos, spatialRef)
arcpy.FeatureClassToShapefile_conversion(asos, outFolder)

arcpy.SelectLayerByAttribute_management(asos, "NEW_SELECTION", '"STATE_NAME" = targetState')
3
  • Try to make a feature layer from the fc/shapefile. Then use the feature layer in the selection.
    – smiller
    Apr 19, 2019 at 17:28
  • A little surprised you're not getting errors at the fc to shapefile conversion (eg shapefile naming conventions such as spaces.)
    – smiller
    Apr 19, 2019 at 17:31
  • 1
    So would i use something like this: arcpy.MakeFeatureLayer_management(asos, "asos_lyr")
    – Will
    Apr 20, 2019 at 2:00

1 Answer 1

2

As discussed in Making XY Event Layer into Copy Feature layer using ArcPy?, you need to select from the feature class (or a layer based on that feature class, actually), not on the event layer. Try something like the version below to create the new layer from a temporary in-memory feature class, and then the select uses that new layer instead of the event layer.

import arcpy
arcpy.env.overwriteOutput = True

xFieldName = 'LON'
yFieldName = 'LAT'
outFolder = "C:\\GEOG485\\FinalProject\\Output"
eventLayer = "ASOS"
spatialRef = arcpy.SpatialReference(4326)
csvFilePath = "C:\\GEOG485\\FinalProject\\isd-history.csv"
targetState = 'NJ'
featureClassLayer = "fc_layer"

arcpy.MakeXYEventLayer_management(csvFilePath, xFieldName, yFieldName, eventLayer, spatialRef)
arcpy.FeatureClassToFeatureClass_conversion(eventLayer, "in_memory", "tmpFeatureClass")
arcpy.MakeFeatureLayer("in_memory/tmpFeatureClass", featureClassLayer)
arcpy.SelectLayerByAttribute_management(featureClassLayer, "NEW_SELECTION", "STATE_NAME = '{}'".format(targetState))

I've moved your targetState out of the string so that it's not considered as a string literal, but as a variable to be included in the string - and put it's placeholder within single quotes. I have also renamed your asos variable to eventLayer and its value to 'ASOS' as I think this makes the variables' uses clearer, and the value is more suitable for use as a shapefile name (which will be derived from the event layer name in this case.)

Depending on what you're planning to do with the selected features, you may not even need to do a select. You can create the new layer with the same 'where' clause that you use in the select to have the layer only include those matching features. Then you don't need to do a select at all. If this suits your purposes, you could replace the last two lines above with the single line:

arcpy.MakeFeatureLayer("in_memory/tmpFeatureClass", featureClassLayer, where_clause="STATE_NAME = '{}'".format(targetState))

If your script needs to continue other work later, you may want to do the following at some point, to free up memory after the temporary feature class and its layer are no longer required for the rest of the script:

arcpy.Delete_management(featureClassLayer)
arcpy.Delete_management("in_memory/tmpFeatureClass")
5
  • So using the Make feature layer the program would run, but it didn't do the select by attribute step. Would copy feature class work instead of feature class to shapefile? I did it manually through ArcMap and when I did the copy feature class the new class had OID's and I was able to run the selected attribute table. Now I am just trying to figure out how to make the output class in copy feature....
    – Will
    Apr 22, 2019 at 16:12
  • So when I run the make feature layer, FID are created instead of OID. Since FID's are created I can't run an attribute query. However when I run the copy features I still get FID's, but when I run it manually in ArcMap I get OID's.. I have reposted the new question.
    – Will
    Apr 23, 2019 at 1:39
  • Frustrating indeed.... Even though FIDs and OIDs are the same, you can't run an attribute query if you have FIDs. Maybe its because I am a newbie and are confused about what is going on...
    – Will
    Apr 23, 2019 at 1:44
  • 1
    OK, I've updated this answer to avoid creating a temporary shapefile. Instead it creates a temporary feature class in memory (which should have OIDs). This should be quicker anyhow. You just can't see it on disk for debugging purposes. Apr 23, 2019 at 1:47
  • I realized my issue was that I wasn't saving the layer to a geodatabase, Once I did this that added the Object ID's to the rows. Now I have to try and get my attribute table queries, but I think I need to add a make feature layer line of code. because as the attribute queries failed.
    – Will
    Apr 23, 2019 at 12:07

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.