4

I have a layer with 2000 or so points, and I selected 50 of them (with no attributes in common, they were randomly chosen). Now I want to make a definition query, so should I will only visualize these 50 points from now.

What is the most practical way to turn the selected features into a definition query filter?

1

5 Answers 5

5

You would not make a definition query, especially if there is no logic to select them. What you can do is right click on the layer in the contents panel go to selection in the context menu and select "Make Layer from selected features". This will create another layer based upon your selection. This layer is a sort of definition query but based upon selections, so it's pointing to the source dataset and not creating a brand new dataset.

4

There are several ways to do it, but the most practical way, since it's arbitrary is to create a new field (name it what ever you want, i.e. "mydefquery"), and field calculate that field to what ever makes sense (i.e. "Random 50 points").

You can then make a definition query where mydefquery='Random 50 points'.

This is better than doing a Selection to Layer approach or a DQ where Object ID IN (210, 5002, 3321, ....) because it will persist if you migrate the data to another geodatabase or add that FC to another map.

3

Here's a full script that can be run in the Python window in ArcGIS Pro.

Source: @MarkBryant's previous answer.

def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs 
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer   
    lyr.definitionQuery = query

aprx = arcpy.mp.ArcGISProject('current') 
m = aprx.activeMap 
lyr = m.listLayers('MY_USER.ROADS')[0] 
qdef_selected_features(lyr)

The script successfully creates a definition query from the selection. It uses the first layer in the Contents pane that is called MY_USER.ROADS.

enter image description here

1
2

You can also just do it with a few lines of code. This has the advantage over the make layer from selected feature, in that you can see the query definition.

def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs 
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer   
    lyr.definitionQuery = query
1
  • aprx = arcpy.mp.ArcGISProject('current') m = aprx.activeMap lyr = m.listLayers('INFRAST.COPY_STRLN')[0] qdef_selected_features(lyr) Commented Mar 27, 2023 at 20:33
1

This functionality was added in ArcGIS Pro 3.4:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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