Select to view content in your preferred language

Make feature layer from selected features

717
3
Jump to solution
10-25-2023 03:28 PM
CCWeedcontrol
Frequent Contributor

I am trying to create a feature layer from the current selection. The selection can be one feature or multiple features. I have an ERROR 000358: Invalid expression on line 9. I am assuming my where clause is wrong?

ptSelection = "points"
parcel = 'TaxParcels'
PF = "memory\PointFeat"


selected_features = arcpy.Describe(ptSelection).FIDSet

where_clause="OBJECTID = {}".format(selected_features)
arcpy.MakeFeatureLayer_management(ptSelection, PF, where_claus

 

0 Kudos
1 Solution

Accepted Solutions
HaydenWelch
Frequent Contributor

The format SQL expects for a list is

 

<FIELD> IN (LIST,OF,VALUES)

 

According to the Docs, FIDSet returns a string in the format:

 

"LIST;OF;VALUES"

 

So to properly format that query, you'd need to do this:

 

selected_features = arcpy.Describe(selection).FIDSet

query_list = f"({','.join([val for val in selected_features.split(';')])})"

where_clause = f"OBJECTID IN {query_list}"

 

It also appears according to the docs that the TableView Describe object has a "whereClause" property that returns the exact query string used in the table view, so hypothetically you could just:

#...
where_clause = arcpy.Describe(selection).whereClause
arcpy.MakeFeatureLayer(ptSelection, PF, where_clause)

 

View solution in original post

3 Replies
AlfredBaldenweck
MVP Regular Contributor

Try using IN instead of = and see if that fixes it.

Also, not to be that guy, but line 9 is cut off.

0 Kudos
HaydenWelch
Frequent Contributor

The format SQL expects for a list is

 

<FIELD> IN (LIST,OF,VALUES)

 

According to the Docs, FIDSet returns a string in the format:

 

"LIST;OF;VALUES"

 

So to properly format that query, you'd need to do this:

 

selected_features = arcpy.Describe(selection).FIDSet

query_list = f"({','.join([val for val in selected_features.split(';')])})"

where_clause = f"OBJECTID IN {query_list}"

 

It also appears according to the docs that the TableView Describe object has a "whereClause" property that returns the exact query string used in the table view, so hypothetically you could just:

#...
where_clause = arcpy.Describe(selection).whereClause
arcpy.MakeFeatureLayer(ptSelection, PF, where_clause)

 

CCWeedcontrol
Frequent Contributor

Thank you, that worked