示例#1
0
        private async Task <ArcGIS.Core.Geometry.GeometryEngine.ProximityResult> ComputeDistanceToGeometryAsync(MapPoint clickPoint, FeatureLayer targetLayer, int hitNumber, double searchDistance)
        {
            FeatureClass featureClass = await targetLayer.GetTableAsync() as FeatureClass;

            Geometry        searchBuffer = null;
            List <Geometry> geometryList = new List <Geometry>();

            double closestDistance = double.MaxValue;

            ArcGIS.Core.Geometry.GeometryEngine.ProximityResult clickResult = null;
            MapPoint closestPoint = null;
            bool     foundFeatureAndComputedDistance = false;


            await QueuingTaskFactory.StartNew(() =>
            {
                var classDefinition = featureClass.Definition as FeatureClassDefinition;

                searchBuffer = GeometryEngine.Project(GeometryEngine.Buffer(clickPoint, searchDistance), classDefinition.SpatialReference);

                SpatialQueryFilter spatialFilter = new SpatialQueryFilter()
                {
                    FilterGeometry      = searchBuffer,
                    SpatialRelationship = SpatialRelationship.EnvelopeIntersects
                };

                RowCursor featureCursor = featureClass.Search(spatialFilter, false);

                while (featureCursor.MoveNext())
                {
                    var feature = featureCursor.Current as Feature;

                    geometryList.Add(feature.Shape);
                }

                clickPoint = GeometryEngine.Project(clickPoint, classDefinition.SpatialReference) as MapPoint;

                foreach (var geometry in geometryList)
                {
                    var geoResult = GeometryEngine.NearestBoundaryPoint(geometry, clickPoint, searchDistance);
                    if (geoResult.Distance < closestDistance)
                    {
                        closestDistance = geoResult.Distance;
                        clickResult     = geoResult;
                        foundFeatureAndComputedDistance = true;
                    }
                }
            });

            if (foundFeatureAndComputedDistance)
            {
                DockPane dockPane = FrameworkApplication.FindDockPane("GeometrySamples_ClosestGeometryPane");
                if (dockPane == null)
                {
                    return(new ArcGIS.Core.Geometry.GeometryEngine.ProximityResult());
                }

                DistancePaneViewModel cgVM = dockPane as DistancePaneViewModel;
                cgVM.DistanceDisplayUnit = _linearDisplayUnit;
                cgVM.ClickResult         = clickResult;
            }

            return(clickResult);
        }