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);
}
}
}
Solved! Go to Solution.
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);
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.
}
}
}
}
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.
Instead of using the Intersects SpatialRelationship, try Contains
Yes. I have tried Contains but get the same result.
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);