ArcGIS Pro 3.7 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 Property SpatialRelationship As SpatialRelationship
    public SpatialRelationship SpatialRelationship {get; set;}
    Example
    Searching a FeatureClass using SpatialQueryFilter
    {
      await 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
    {
      await 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 an 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))
          {
          }
        }
      });
    }
    Spatial query filter with DE9-IM spatial relationships
    {
      // Must be called within QueuedTask.Run
      void FindSpatiallyRelatedFeaturesUsingDE9IMPredicate(Geodatabase geodatabase, FeatureClass polygonFeatureClass, FeatureClass polylineFeatureClass)
      {
        using (RowCursor polygonRowCursor = polygonFeatureClass.Search(new QueryFilter()))
        {
          if (polygonRowCursor.MoveNext())
          {
            using (Feature polygonFeature = polygonRowCursor.Current as Feature)
            {
              // DE9IM predicate string to find overlapping features
              string overlappingDE9IM = "1*T***T**";
    
              SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter()
              {
                FilterGeometry = polygonFeature.GetShape(),
                SpatialRelationship = SpatialRelationship.Relation,
                SpatialRelationshipDescription = overlappingDE9IM
              };
    
              using (RowCursor overlappingPolyline = polylineFeatureClass.Search(spatialQueryFilter))
              {
                while (overlappingPolyline.MoveNext())
                {
                  // Overlapping polylines on the polygon
                }
              }
            }
          }
        }
      }
    }
    Use Select or Search with a Spatial Query
    // Note: call within 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)
            {
              oid = feature.GetObjectID();
              //etc.
            }
          }
        }
      }
      MapView.Active.Map.ClearSelection();
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also