How could I use IQueryByLayer to get all objects outside the from layer to a certain distance?

1809
0
05-16-2016 11:47 AM
ScottLaFoy
New Contributor


Right now I am working on searching a layer by layers. I have 3 different searches I need to implement.

1. Search 2 layers to see if any features on the second layer are within a feature on the first layer. For example: How many points from layer 2 are within a polygon on layer 1

2, Search 2 layers to see if any features on the second layer are within a given distance of the border of a feature on layer 1. For example: How many points from layer 2 are within 5 feet inside or out of the parameter of a polygon from layer 1

3. Search 2 layers so see if any features on the second layer are outside a feature on the first layer within a certain distance. For example: How many points from layer 2 are outside the selected polygon on layer 1  up to 20 feet.

I have the first one working, see the code below. But I cannot get the second or third queries to work. When I change the selected feature to use esriLayerSelectWithinADistance it will crash when using a polygon layer as the from layer and a point layer as the by layer. Any ideas how I could implement this?

public static ICollection<int> SelectCompletelyContains([NotNull] ILayer fromLayer,
                                                        [NotNull] IEnumerable<ILayer> byLayers,
                                                        [CanBeNull] IGeometry spatialFilterGeometry)
        {
            Validate.NotNull("fromLayer", fromLayer);
            Validate.NotNull("byLayers", byLayers);


            IFeatureLayer fromFeatureLayer = (IFeatureLayer)fromLayer;


            // Set up the query by layer ...
            IQueryByLayer queryByLayer = new QueryByLayerClass();
            queryByLayer.FromLayer = fromFeatureLayer;
            queryByLayer.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectCompletelyWithin;
            queryByLayer.UseSelectedFeatures = (null != spatialFilterGeometry);
            queryByLayer.ResultType = esriSelectionResultEnum.esriSelectionResultNew;
            ISelectionSet selectionSet = null;


            HashSet<int> objectIds = new HashSet<int>();
            foreach (ILayer byLayer in byLayers)
            {                 
                IFeatureLayer byFeatureLayer = (IFeatureLayer)byLayer;


                // Save the current selection set for the by feature layer, so it can be restored later ...
                IFeatureSelection byFeatureSelection = (IFeatureSelection)byFeatureLayer;
                ISelectionSet savedByFeatureSelectionSet = byFeatureSelection.SelectionSet;


                // Clear any selected features prior to performing the spatial query on the by feature layer ...
                byFeatureSelection.Clear();


                if (null != spatialFilterGeometry)
                {
                    // Perform a spatial query on the by feature layer to obtain features in the view extent ...
                    byFeatureSelection = ApplySpatialFilter(byFeatureLayer, spatialFilterGeometry);
                }


                // Set up the query by layer to use the current by feature layer ...
                queryByLayer.ByLayer = byFeatureLayer;


                ISelectionSet tempSelectionSet = queryByLayer.Select();
                if (null == selectionSet)
                {
                    // First time ...
                    selectionSet = tempSelectionSet;


                    queryByLayer.ResultType = esriSelectionResultEnum.esriSelectionResultAdd;
                }
                else
                {
                    ISelectionSet resultSet;
                    selectionSet.Combine(tempSelectionSet, esriSetOperation.esriSetUnion, out resultSet);
                    Marshal.ReleaseComObject(selectionSet);


                    selectionSet = resultSet;
                    Marshal.ReleaseComObject(tempSelectionSet);
                }


                // Restore previous selection set for the by feature layer.
                if (savedByFeatureSelectionSet != null)
                {
                    byFeatureSelection.SelectionSet = savedByFeatureSelectionSet;
                    Marshal.ReleaseComObject(savedByFeatureSelectionSet);
                }
            }


            IEnumIDs enumIDs = selectionSet.IDs;
            enumIDs.Reset();
            int id;
            while ((id = enumIDs.Next()) != -1)
            {
                objectIds.Add(id);


            }


            Marshal.ReleaseComObject(enumIDs);


            Marshal.ReleaseComObject(selectionSet);


            return objectIds;


        }

Thank you in advance for your time.

0 Kudos
0 Replies