ArcGIS Pro 3.1 API Reference Guide
ArcGIS.Core.Data Namespace / SpatialQueryFilter Class / SpatialRelationship Property
Example

In This Topic
    SpatialRelationship Property
    In This Topic
    Gets and sets the SpatialRelationship to use for the spatial filter.
    Syntax
    public SpatialRelationship SpatialRelationship {get; set;}
    Public Property SpatialRelationship As SpatialRelationship
    Example
    Searching a FeatureClass using SpatialQueryFilter
    public async Task SearchingAFeatureClass()
    {
      await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
        using (FeatureClass schoolBoundaryFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.SchoolBoundary"))
        {
          // Using a spatial query filter to find all features which have a certain district name and lying within a given Polygon.
          SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
          {
            WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'",
            FilterGeometry = new PolygonBuilderEx(new List<Coordinate2D>
            {
              new Coordinate2D(1021880, 1867396),
              new Coordinate2D(1028223, 1870705),
              new Coordinate2D(1031165, 1866844),
              new Coordinate2D(1025373, 1860501),
              new Coordinate2D(1021788, 1863810)
            }).ToGeometry(),
    
            SpatialRelationship = SpatialRelationship.Within
          };
    
          using (RowCursor indianPrairieCursor = schoolBoundaryFeatureClass.Search(spatialQueryFilter, false))
          {
            while (indianPrairieCursor.MoveNext())
            {
              using (Feature feature = (Feature)indianPrairieCursor.Current)
              {
                // Process the feature. For example...
                Console.WriteLine(feature.GetObjectID());
              }
            }
          }
        }
      });
    }
    Selecting Features from a FeatureClass
    public async Task SelectingFeaturesFromAFeatureClass()
    {
      await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
        using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.FacilitySite"))
        {
          List<Coordinate2D> newCoordinates = new List<Coordinate2D>
          {
            new Coordinate2D(1021570, 1880583),
            new Coordinate2D(1028730, 1880994),
            new Coordinate2D(1029718, 1875644),
            new Coordinate2D(1021405, 1875397)
          };
    
          SpatialQueryFilter spatialFilter = new SpatialQueryFilter
          {
            WhereClause = "FCODE = 'Park'",
            FilterGeometry = new PolygonBuilderEx(newCoordinates).ToGeometry(),
            SpatialRelationship = SpatialRelationship.Crosses
          };
    
          // For Selecting all matching entries.
          using (Selection anotherSelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.Normal))
          {
          }
    
          // This can be used to get one record which matches the criteria. No assumptions can be made about which record satisfying the 
          // criteria is selected.
          using (Selection onlyOneSelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.OnlyOne))
          {
          }
    
          // This can be used to obtain a empty selction which can be used as a container to combine results from different selections.
          using (Selection emptySelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.Empty))
          {
          }
    
          // If you want to select all the records in a table.
          using (Selection allRecordSelection = enterpriseFeatureClass.Select(null, SelectionType.ObjectID, SelectionOption.Normal))
          {
          }
        }
      });
    }
    Use Select or Search with a Spatial Query
    //var featSceneLayer = ...;
    //var sname = featSceneLayer.Name;
    await QueuedTask.Run(() =>
    {
      if (!featSceneLayer.HasAssociatedFeatureService)
        return;//no search or select
    
      //Select all features within the current map view
      var sz = MapView.Active.GetViewSize();
      var map_pt1 = MapView.Active.ClientToMap(new System.Windows.Point(0, sz.Height));
      var map_pt2 = MapView.Active.ClientToMap(new System.Windows.Point(sz.Width, 0));
    
      //Convert to an envelope
      var temp_env = EnvelopeBuilderEx.CreateEnvelope(map_pt1, map_pt2, MapView.Active.Map.SpatialReference);
    
      //Project if needed to the layer spatial ref
      SpatialReference sr = null;
      using (var fc = featSceneLayer.GetFeatureClass())
      using (var fdef = fc.GetDefinition())
        sr = fdef.GetSpatialReference();
    
      var env = GeometryEngine.Instance.Project(temp_env, sr) as Envelope;
    
      //Set up a query filter
      var sf = new SpatialQueryFilter()
      {
        FilterGeometry = env,
        SpatialRelationship = SpatialRelationship.Intersects,
        SubFields = "*"
      };
    
      //Select against the feature service
      var select = featSceneLayer.Select(sf);
      if (select.GetCount() > 0)
      {
        //enumerate over the selected features
        using (var rc = select.Search())
        {
          while (rc.MoveNext())
          {
            using (var feature = rc.Current as Feature)
            {
              var oid = feature.GetObjectID();
              //etc.
            }
          }
        }
      }
    
      MapView.Active.Map.ClearSelection();
    
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.0 or higher.
    See Also