Select to view content in your preferred language

SpatialQueryFilter not return results

505
5
Jump to solution
10-20-2023 12:23 PM
JonathanDewalt
New Contributor III

I have a simple task:  Get all point features that are within the view extent of the current map.

The point feature layer and map view are both in the same projection and i have tried all possible spatial relationship options but the featurelayer search function always comes back empty.  using ArcGIS Pro 3.0.2

Any suggestions?

 

var spatialQueryFilter = new SpatialQueryFilter
{
SpatialRelationship = SpatialRelationship.EnvelopeIntersects ,
FilterGeometry = theMapView.Extent,
};

// Get the features
using (var cursor = featureLayer.Search(spatialQueryFilter))
{
while (cursor.MoveNext())
{
using (var feature = cursor.Current as Feature)
{
features.Add(feature);
}
}
}

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JonathanDewalt
New Contributor III

This works

var map_pt1 = new Coordinate2D(theMapView.Extent.XMax, theMapView.Extent.YMax);
var map_pt2 = new Coordinate2D(theMapView.Extent.XMin, theMapView.Extent.YMin);

//Convert to an envelope
var temp_env = EnvelopeBuilderEx.CreateEnvelope(map_pt1, map_pt2, theMapView.Map.SpatialReference);

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor

The documentation for SpatialQeryFilter has an example that selects all features within the current map view. Does this return any values?

  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.
        }
      }
    }
  }
0 Kudos
JonathanDewalt
New Contributor III

This works if my return is how many polygons are within the view extent, but if I pass in a point layer, I get zero.

0 Kudos
KenBuja
MVP Esteemed Contributor

Instead of using the Intersects SpatialRelationship, try Contains

0 Kudos
JonathanDewalt
New Contributor III

Yes.  I have tried Contains but get the same result.

0 Kudos
JonathanDewalt
New Contributor III

This works

var map_pt1 = new Coordinate2D(theMapView.Extent.XMax, theMapView.Extent.YMax);
var map_pt2 = new Coordinate2D(theMapView.Extent.XMin, theMapView.Extent.YMin);

//Convert to an envelope
var temp_env = EnvelopeBuilderEx.CreateEnvelope(map_pt1, map_pt2, theMapView.Map.SpatialReference);

0 Kudos