protected virtual void OnFindNearbyExecuted(FindNearbyEventArgs args)
 {
     if (FindNearbyExecuted != null)
     {
         FindNearbyExecuted(this, args);
     }
 }
        void buffer(string agsGeometryServerUrl, BufferParameters bufferParams, FindNearbyEventArgs findNearbyRequest)
        {
            if (string.IsNullOrEmpty(agsGeometryServerUrl))
            {
                return;
            }

            GeometryService geomService = new GeometryService
            {
                Url = agsGeometryServerUrl
            };

            geomService.BufferCompleted += GeometryService_BufferCompleted;
            geomService.Failed          += (o, e) =>
            {
                if (findNearbyToolWindow != null)
                {
                    findNearbyToolWindow.StopBusyIndicator();
                    MapApplication.Current.HideWindow(findNearbyToolWindow);
                }
                MessageBoxDialog.Show(Resources.Strings.MsgErrorExecutingBufferOperation + Environment.NewLine + e.Error.ToString());
            };
            geomService.BufferAsync(bufferParams, findNearbyRequest);
        }
        void findNearbyToolWindow_FindNearby(object sender, FindNearbyEventArgs e)
        {
            if (Layer == null)
                return;

            GraphicsLayer graphicsLayer = Layer as GraphicsLayer;
            if (graphicsLayer == null)
                return;

            if (graphicsLayer.SelectionCount < 1)
            {
                findNearbyToolWindow.StopBusyIndicator();
                MessageBoxDialog.Show(Resources.Strings.MsgNoFeaturesSelected, Resources.Strings.ErrorCaption, MessageBoxButton.OK);
                return;
            }

            BufferParameters bufferParams = new BufferParameters();
            switch (e.LinearUnit)
            {
                case LinearUnit.Miles:
                    bufferParams.Unit = ESRI.ArcGIS.Client.Tasks.LinearUnit.StatuteMile;
                    break;
                case LinearUnit.Meters:
                    bufferParams.Unit = ESRI.ArcGIS.Client.Tasks.LinearUnit.Meter;
                    break;
                case LinearUnit.Kilometers:
                    bufferParams.Unit = ESRI.ArcGIS.Client.Tasks.LinearUnit.Kilometer;
                    break;
            }
            bufferParams.UnionResults = true;
            bufferParams.OutSpatialReference = Map.SpatialReference;
            SpatialReference gcs = new SpatialReference(4326);
            bufferParams.BufferSpatialReference = gcs;
            bufferParams.Geodesic = true;
            bufferParams.Distances.Add(e.Distance);

            // Check the spatial reference of the first graphic
            Graphic firstGraphic = graphicsLayer.SelectedGraphics.ElementAt(0);
            bool isInGcs = firstGraphic.Geometry != null
                             && firstGraphic.Geometry.SpatialReference != null
                             && firstGraphic.Geometry.SpatialReference.Equals(gcs);

            // In order to perform geodesic buffering we need to pass geometries in GCS to the geom service
            if (isInGcs)
            {
                foreach (Graphic selectedGraphic in graphicsLayer.SelectedGraphics)
                    bufferParams.Features.Add(selectedGraphic);

                buffer(GeometryServiceUrl, bufferParams, e);
            }
            else
            {
                GeometryServiceOperationHelper helper = new GeometryServiceOperationHelper(GeometryServiceUrl);
                helper.ProjectGraphicsCompleted += (o, args) => {
                    foreach (Graphic selectedGraphic in args.Graphics)
                        bufferParams.Features.Add(selectedGraphic);
                    buffer(GeometryServiceUrl, bufferParams, e);
                };
                helper.ProjectGraphics(graphicsLayer.SelectedGraphics.ToList(), new SpatialReference(4326));
            }
        }        
        void buffer(string agsGeometryServerUrl, BufferParameters bufferParams, FindNearbyEventArgs findNearbyRequest)
        {
            if (string.IsNullOrEmpty(agsGeometryServerUrl))
                return;

            GeometryService geomService = new GeometryService
            {
                Url = agsGeometryServerUrl
            };
            geomService.BufferCompleted += GeometryService_BufferCompleted;
            geomService.Failed += (o, e) =>
            {
                if (findNearbyToolWindow != null)
                {
                    findNearbyToolWindow.StopBusyIndicator();
                    MapApplication.Current.HideWindow(findNearbyToolWindow);
                }
                MessageBoxDialog.Show(Resources.Strings.MsgErrorExecutingBufferOperation + Environment.NewLine + e.Error.ToString());                
            };
            geomService.BufferAsync(bufferParams, findNearbyRequest);
        }
        void findNearbyToolWindow_FindNearby(object sender, FindNearbyEventArgs e)
        {
            if (Layer == null)
            {
                return;
            }

            GraphicsLayer graphicsLayer = Layer as GraphicsLayer;

            if (graphicsLayer == null)
            {
                return;
            }

            if (graphicsLayer.SelectionCount < 1)
            {
                findNearbyToolWindow.StopBusyIndicator();
                MessageBoxDialog.Show(Resources.Strings.MsgNoFeaturesSelected, Resources.Strings.ErrorCaption, MessageBoxButton.OK);
                return;
            }

            BufferParameters bufferParams = new BufferParameters();

            switch (e.LinearUnit)
            {
            case LinearUnit.Miles:
                bufferParams.Unit = ESRI.ArcGIS.Client.Tasks.LinearUnit.StatuteMile;
                break;

            case LinearUnit.Meters:
                bufferParams.Unit = ESRI.ArcGIS.Client.Tasks.LinearUnit.Meter;
                break;

            case LinearUnit.Kilometers:
                bufferParams.Unit = ESRI.ArcGIS.Client.Tasks.LinearUnit.Kilometer;
                break;
            }
            bufferParams.UnionResults        = true;
            bufferParams.OutSpatialReference = Map.SpatialReference;
            SpatialReference gcs = new SpatialReference(4326);

            bufferParams.BufferSpatialReference = gcs;
            bufferParams.Geodesic = true;
            bufferParams.Distances.Add(e.Distance);

            // Check the spatial reference of the first graphic
            Graphic firstGraphic = graphicsLayer.SelectedGraphics.ElementAt(0);
            bool    isInGcs      = firstGraphic.Geometry != null &&
                                   firstGraphic.Geometry.SpatialReference != null &&
                                   firstGraphic.Geometry.SpatialReference.Equals(gcs);

            // In order to perform geodesic buffering we need to pass geometries in GCS to the geom service
            if (isInGcs)
            {
                foreach (Graphic selectedGraphic in graphicsLayer.SelectedGraphics)
                {
                    bufferParams.Features.Add(selectedGraphic);
                }

                buffer(GeometryServiceUrl, bufferParams, e);
            }
            else
            {
                GeometryServiceOperationHelper helper = new GeometryServiceOperationHelper(GeometryServiceUrl);
                helper.ProjectGraphicsCompleted += (o, args) => {
                    foreach (Graphic selectedGraphic in args.Graphics)
                    {
                        bufferParams.Features.Add(selectedGraphic);
                    }
                    buffer(GeometryServiceUrl, bufferParams, e);
                };
                helper.ProjectGraphics(graphicsLayer.SelectedGraphics.ToList(), new SpatialReference(4326));
            }
        }
        void geometryService_RelationCompleted(object sender, RelationEventArgs e)
        {
            object[] state = e.UserState as object[];
            if (state == null || state.Length < 1)
            {
                return;
            }

            FindNearbyEventArgs eventArgs = state[0] as FindNearbyEventArgs;

            if (eventArgs == null)
            {
                return;
            }

            GraphicsLayer layerToCompare = eventArgs.SelectedLayer;

            if (layerToCompare == null)
            {
                return;
            }

            GraphicsLayer resultsLayer = new GraphicsLayer()
            {
                Opacity = layerToCompare.Opacity,
            };

            if (layerToCompare.Renderer != null)
            {
                resultsLayer.Renderer = layerToCompare.Renderer.CloneRenderer();
            }
            GraphicsLayer bufferGraphicsLayer = null;

            if (state.Length > 1)
            {
                bufferGraphicsLayer = state[1] as GraphicsLayer;
            }

            foreach (GeometryRelationPair geomPair in e.Results)
            {
                int index = geomPair.Graphic1Index;
                if (index < 0 || index >= layerToCompare.Graphics.Count)
                {
                    continue;
                }
                Graphic intersectingGraphic = layerToCompare.Graphics[index];
                resultsLayer.Graphics.Add(copyGraphic(intersectingGraphic));
            }

            CopyLayerConfiguration(layerToCompare, resultsLayer);
            int    insertIndex = Map.Layers.IndexOf(layerToCompare);
            string displayName = getDisplayNameForResultLayer(eventArgs.LayerDisplayName);

            AddResultLayer(bufferGraphicsLayer, true, Resources.Strings.FindNearbyBufferPrefix + displayName, ESRI.ArcGIS.Mapping.Core.GeometryType.Polygon, insertIndex);
            AddResultLayer(resultsLayer, true, Resources.Strings.FindNearbyPrefix + displayName, Core.LayerExtensions.GetGeometryType(layerToCompare), insertIndex);
            if (findNearbyToolWindow != null)
            {
                findNearbyToolWindow.StopBusyIndicator();
                MapApplication.Current.HideWindow(findNearbyToolWindow);
            }
        }
        void GeometryService_BufferCompleted(object sender, GraphicsEventArgs args)
        {
            IList <Graphic> results = args.Results;

            if (results == null || results.Count < 1)
            {
                return;
            }

            FindNearbyEventArgs eventArgs = args.UserState as FindNearbyEventArgs;

            if (eventArgs == null)
            {
                return;
            }

            GraphicsLayer targetGraphicsLayer = eventArgs.SelectedLayer;

            if (targetGraphicsLayer == null)
            {
                return;
            }

            #region Draw the buffer graphics layer
            GraphicsLayer bufferGraphicsLayer = new GraphicsLayer()
            {
                ID = eventArgs.EventId
            };

            // Add the circle graphic
            Graphic circleGraphic = results[0];
            circleGraphic.Attributes.Add(Resources.Strings.BufferDistance, string.Format("{0} {1}", eventArgs.Distance, eventArgs.LinearUnit.ToString()));
            SimpleFillSymbol defaultBufferSymbol = new SimpleFillSymbol()
            {
                BorderBrush     = new SolidColorBrush(Colors.Blue),
                BorderThickness = 1,
                Fill            = new SolidColorBrush(new Color()
                {
                    A = (byte)102, R = (byte)255, G = (byte)0, B = (byte)0
                })
            };
            SimpleRenderer simpleRenderer = new SimpleRenderer()
            {
                Symbol = defaultBufferSymbol,
            };
            bufferGraphicsLayer.Renderer = simpleRenderer;
            Collection <FieldInfo> layerFields = Core.LayerExtensions.GetFields(bufferGraphicsLayer);
            if (layerFields != null)
            {
                layerFields.Add(new FieldInfo()
                {
                    DisplayName = Resources.Strings.BufferDistance, Name = Resources.Strings.BufferDistance, FieldType = FieldType.Text
                });
            }
            bufferGraphicsLayer.Graphics.Add(circleGraphic);
            #endregion

            Graphic graphicCircle = results[0];
            if (graphicCircle != null)
            {
                ESRI.ArcGIS.Client.Geometry.Geometry graphicCircleGeometry = graphicCircle.Geometry;
                if (graphicCircleGeometry != null)
                {
                    // Ensure that atleast the first graphic of the graphics layer has a spatial reference set
                    // because ESRI.ArcGIS.Client.Tasks checks it
                    if (targetGraphicsLayer.Graphics.Count > 0)
                    {
                        if (targetGraphicsLayer.Graphics[0].Geometry != null &&
                            targetGraphicsLayer.Graphics[0].Geometry.SpatialReference == null && Map != null)
                        {
                            targetGraphicsLayer.Graphics[0].Geometry.SpatialReference = Map.SpatialReference;
                        }
                    }

                    // The extent of the result layer should be the extent of the circle
                    if (Map != null)
                    {
                        Map.ZoomTo(expandExtent(graphicCircleGeometry.Extent));
                    }

                    relationAsync(GeometryServiceUrl, targetGraphicsLayer.Graphics.ToList(), results, new object[] { eventArgs, bufferGraphicsLayer });
                }
            }
        }
 protected virtual void OnFindNearbyExecuted(FindNearbyEventArgs args)
 {
     if (FindNearbyExecuted != null)
         FindNearbyExecuted(this, args);
 }