ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Core.Data Namespace / SpatialRelationship Enumeration
Example Example

In This Topic
    SpatialRelationship Enumeration
    In This Topic
    Specifies the spatial relationship.
    Syntax
    Members
    MemberDescription
    Contains The filter geometry wholly contains within it a feature from the target feature class.
    Crosses The filter geometry crosses a feature from the target feature class.
    EnvelopeIntersects The envelope of the filter geometry intersects with the envelope of a feature in the target feature class.
    IndexIntersects The envelope of the filter geometry intersects with the index entry for a feature in the target feature class.
    Intersects The filter geometry intersects a feature from the target feature class.
    Overlaps The filter geometry overlaps a feature in the target feature class.
    Relation The filter geometry is involved in an interior-boundary-exterior relationship with a feature from the target feature class.
    Touches The filter geometry touches a feature in the target feature class.
    Undefined No defined spatial relationship.
    Within The filter geometry is within a feature in the target feature class.
    Remarks
    • Intersects - Returns a feature if any spatial relationship is found.
    • EnvelopeIntersects - Returns a feature if the envelope of the two shapes intersect.
    • Contains - Returns a feature if its shape is wholly contained within the search geometry. Valid of all shape type combinations.
    • Crosses - Returns a feature if the intersection of the interiors of the two shapes is not empty and has a lower dimension that the maximum dimension of the two shapes. Two lines that share an endpoint do not cross. Valid for polyline/polyline, polyline/Area, multipoint/Area, and multipoint/polyline shape type combinations.
    • IndexIntersects uses the underlying index grid of the target feature class which is faster than using the envelope of the features, and is often used to return features for display purposes.
    • Overlaps - Returns a feature if the intersection of the two shapes results in an object of the same dimension, but different from both of the shapes. Applies to Area/Area, polyline/polyline, and multipoint/multipoint shape type combinations.
    • Touches - Returns a feature if the two shapes share a common boundary. However, the intersection of the interiors of the two shapes must be empty. In the point/polyline case, the point may touch an endpoint only of the polyline. Applies to all combinations except for point/point.
    • Within - Returns a feature if its shape wholly contains the search geometry. Valid for all shape type combinations.
    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();
    
    });
    Inheritance Hierarchy

    System.Object
       System.ValueType
          System.Enum
             ArcGIS.Core.Data.SpatialRelationship

    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also