ArcGIS Pro 3.5 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / DefinitionQuery Class / SetFilterGeometry Method
The geometry to be set as the filter
Example

In This Topic
    SetFilterGeometry Method
    In This Topic
    Set the definition query filter geometry. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void SetFilterGeometry( 
       Geometry filterGeometry
    )
    Public Sub SetFilterGeometry( _
       ByVal filterGeometry As Geometry _
    ) 

    Parameters

    filterGeometry
    The geometry to be set as the filter
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Invalid filter geometry
    Remarks
    Specifying an invalid geometry as the filter geometry will throw a System.ArgumentException. Refer to CanSetFilterGeometry. The filter geometry will be converted to a Uri and assigned to GeometryUri. SpatialReference will be set to the spatial reference of the filter geometry
    Example
    Querying a feature layer with a spatial filter
              // Note: Run within QueuedTask.Run
              // Set the spatial filter geometry
    
              //Get the geometry from the selected features in the feature layer
              var spatialClauseGeom = spatialDefnLayer.GetFeatureOutline(MapView.Active, FeatureOutlineType.Selected);
    
    DefinitionQuery definitionQuery = new DefinitionQuery
    {
      WhereClause = whereClause,
      Name = $"{layerToQuery.Name}"
    };
    //Setting the spatial filter to the Definition Query
    if (definitionQuery.CanSetFilterGeometry(spatialClauseGeom))
    {
      definitionQuery.SetFilterGeometry(spatialClauseGeom);
    }
    
    layerToQuery.InsertDefinitionQuery(definitionQuery);
    layerToQuery.SetActiveDefinitionQuery(definitionQuery.Name);
    Apply A Definition Query Filter With a Filter Geometry to a Feature Layer
        var greatLakes = map.GetLayersAsFlattenedList()
                    .OfType<FeatureLayer>().First(l => l.Name == "Great Lakes");
        var usa_states = map.GetLayersAsFlattenedList()
        .OfType<FeatureLayer>().First(l => l.Name == "US_States");
    
        QueuedTask.Run(() =>
        {
    //name must be unique
            var def_query = new DefinitionQuery("GreatLakes",
                                                        "NAME in ('Huron','Michigan','Erie')");
    
            //create a filter geometry - in this example we will use the outline geometry
            //of all visible features from a us states layer...the filter geometry will be
    //intersected with the layer feature geometry when added to the def query
            var filter_geom = usa_states.GetFeatureOutline(mv, FeatureOutlineType.Visible);
            //other options...
            //var filter_geom = usa_states.GetFeatureOutline(mv, FeatureOutlineType.All);
            //var filter_geom = usa_states.GetFeatureOutline(mv, FeatureOutlineType.Selected);
    
            //Geometry must have a valid SR and be point, multi-point, line, or poly
            if (def_query.CanSetFilterGeometry(filter_geom))
    {
      def_query.SetFilterGeometry(filter_geom);
    }
    
            //Apply the def query
            greatLakes.InsertDefinitionQuery(def_query);
            //Set it active
            greatLakes.SetActiveDefinitionQuery(def_query.Name);
    
            //or....also - set it active when it is inserted
            //greatLakes.InsertDefinitionQuery(def_query, true);
        });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.5 or higher.
    See Also