Hello,
try to select by attributes a feature class, but it doesn't work?
Any ideas why?
fields = ["OBJECTID", "TARGET_FID"]
with arcpy.da.SearchCursor(outMerge, fields) as cursor:
for row in cursor:
if row[1] > 0:
selClause = str(row[1])
selClause1 = "OBJECTID = {0} Or TARGET_FID = {0}".format(selClause)
print (selClause1)
arcpy.management.SelectLayerByAttribute(outMerge, "NEW_SELECTION", selClause1)
result = arcpy.management.GetCount(outMerge)
print (result[0])
if int(result[0]) > 1:
outIntersLines = "outIntersLines_{}".format(row[0])
# arcpy.management.PointsToLine(outMerge, outIntersLines)
else:
continue
else:
continue
The print statements line 9 looks ok for me:
OBJECTID = 1 Or TARGET_FID = 1
But there's nothing selected?
Solved! Go to Solution.
I am assuming outMerge is a path to data? I think you are calling GetCount on the input Feature Class, instead you need to wrap a layer using arcpy.management.MakeFeatureLayer and use that as the first to Select by Attributes, and then GetCount on the layer. Alternatively use getOutput on Select by Attributes - this exposes a layer created by the tool which will have the count on it. call GetCount on that.
sel_lyr = arcpy.management.SelectLayerByAttribute(outMerge, "NEW_SELECTION", selClause1).getOutput(0)
count1 = int(arcpy.management.GetCount(sel_lyr).getOutput(0))
Hi
So if you run a "SQL query" and no rows / features are selected then it means you have a poorly constructed where clause.
I think 'Or' (line 7 in your code snippet above) needs to be capitalised (I think...).
Many thanks ed
Dear EdMorris,
I hope you are doing great when reading this message,
SQL keywords are not case-sensitive, which means you can write them in uppercase, lowercase, or a combination of both.
Have a great day
thanks!
... as i said... "I think..."
happy to be corrected
I am assuming outMerge is a path to data? I think you are calling GetCount on the input Feature Class, instead you need to wrap a layer using arcpy.management.MakeFeatureLayer and use that as the first to Select by Attributes, and then GetCount on the layer. Alternatively use getOutput on Select by Attributes - this exposes a layer created by the tool which will have the count on it. call GetCount on that.
sel_lyr = arcpy.management.SelectLayerByAttribute(outMerge, "NEW_SELECTION", selClause1).getOutput(0)
count1 = int(arcpy.management.GetCount(sel_lyr).getOutput(0))
Thank you ...
Dear Johannes,
I hope you are doing well,
The problem of your script is this, in the for loop you are generating a new SQL clause, but you should add to the previous SQL clause and apply the select method out of for loop.
please check tthe below image, which is correct version of your script.
Please make sure about the 'outMerge',
If you have more info please let me know.
Hope it helps, Stay Safe
Hello Amir,
thank you for your help - but i think RHmapping was right about the missing make feature layer ...
Hello Johannes,
As I mentioned you should be sure the 'outMerge' is feature layer, but if you have a feature layer with high number of records, calling arcpy inside a for loop in each iteration is not efficient method.
If you try the both solutions in your feature layer data, you will see the differences.
I just tried in a feature layer with 257 records,
first one took 7.94 seconds.
second one took 0.18 seconds.
Anyway, I am so happy you find your solution,
Best ragrads
Looks like you got a solution, but for next time:
If you want to use a selection as input into something else, you need to make that selection a variable.
outMerge = arcpy.management.SelectLayerByAttribute(outMerge,
"NEW_SELECTION",
selClause1)
result = arcpy.management.GetCount(outMerge,...)