Select to view content in your preferred language

Buffer Query with Line Input

1048
3
02-15-2011 01:22 PM
ChristineZeller
Occasional Contributor
I'm trying to modify the example http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#BufferQuery

I want to use a input of a line instead of a point.  I almost have it working but the two problems I am having is it is drawing a huge buffer distance when it should be doing like 500 feet it creating a buffer around half of the world so every parcel (or whatever layer is chosen in the drop down list I have it hooked to select a layer that the user defines).

My other problem is the buffer it is drawing is a Circle (that covers all of North America and some parts of Africa) and obvisouly it should be a polygon shaped like the line that was drawn.

Could someone look at my code and maybe give me a heads up on what I'm doing wrong.  The


Here's my code behind:

private void CreateBuffLine_Click(object sender, RoutedEventArgs e)
        {

            _geometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
            _geometryService.BufferCompleted += GeometryService_BufferLineCompleted;
            _geometryService.Failed += BuffLineSelectGeometryService_Failed;

            _queryTask = new QueryTask((Layers.SelectedItem as FeatureLayer).Url);
            _queryTask.ExecuteCompleted += BuffLineQueryTask_ExecuteCompleted;
            _queryTask.Failed += BuffLineQueryTask_Failed;

            _lineAndBufferGraphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;
            GraphicsLayer graphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;

            _geometryService.CancelAsync();
            _queryTask.CancelAsync();


            //Gives users ability to draw line
            MyDrawObject = new Draw(Map)
            {
                DrawMode = DrawMode.Polyline,
                IsEnabled = true,
                LineSymbol = LayoutRoot.Resources["ClipLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol

            };

            // Start MyDrawObject_DrawComplete, which turns the drawn poly into a graphic
            MyDrawObject.DrawComplete += MyDrawObject_BuffLineDrawComplete;
        }



            private void MyDrawObject_BuffLineDrawComplete(object sender, DrawEventArgs args)
        {
            //Disable the drawing tool
            MyDrawObject.IsEnabled = false;


            //Assign the graphic draw to a graphiclayer, the graphic layer is defined in xaml
            GraphicsLayer graphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;

            //Ready the graphic 
            Graphic graphic = new Graphic()
            {
                Symbol = LayoutRoot.Resources["ClipLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol,
                Geometry = args.Geometry

            };

            // Input spatial reference for buffer operation defined by first feature of input geometry array
            graphic.Geometry.SpatialReference = Map.SpatialReference;

            _lineAndBufferGraphicsLayer.ClearGraphics();
            graphicsLayer.ClearGraphics();

            graphic.SetZIndex(2);
       
            _lineAndBufferGraphicsLayer.Graphics.Add(graphic);

            // If buffer spatial reference is GCS and unit is linear, geometry service will do geodesic buffering
            ESRI.ArcGIS.Client.Tasks.BufferParameters bufferParams = new ESRI.ArcGIS.Client.Tasks.BufferParameters()
            {
                BufferSpatialReference = new SpatialReference(4326),
                OutSpatialReference = Map.SpatialReference,
                Unit = LinearUnit.Foot,
            };
            bufferParams.Distances.Add(Int32.Parse(BufferLineSelectDistanceTextBox.Text));
            bufferParams.Features.Add(graphic);

            _geometryService.BufferAsync(bufferParams);
        }



        void GeometryService_BufferLineCompleted(object sender, GraphicsEventArgs args)
        {
            Graphic bufferGraphic = new Graphic();
            bufferGraphic.Geometry = args.Results[0].Geometry;
            bufferGraphic.Symbol = LayoutRoot.Resources["BufferSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
            bufferGraphic.SetZIndex(1);

            _lineAndBufferGraphicsLayer.Graphics.Add(bufferGraphic);

            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            query.ReturnGeometry = true;
            query.OutSpatialReference = Map.SpatialReference;
            query.Geometry = bufferGraphic.Geometry;
            query.OutFields.Add("*");
            _queryTask.ExecuteAsync(query);
        }

        private void BuffLineQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {

            FeatureSet featureSet = args.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features retured from query");
                return;
            }

            GraphicsLayer graphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;


            if (featureSet != null && featureSet.Features.Count > 0)
            {
                foreach (Graphic feature in featureSet.Features)
                {

                    switch (featureSet.GeometryType.ToString())
                    {
                        case "Polygon":
                            feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;
                            break;
                        case "Polyline":
                            feature.Symbol = LayoutRoot.Resources["CustomGrowLineSymbol"] as LineSymbol;
                            break;
                        case "Point":
                            feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;
                            break;
                    }

                    graphicsLayer.Graphics.Insert(0, feature);

                }

                ResultsDisplay.IsExpanded = true;
            }
            MyDrawSurface.IsEnabled = false;


        }

        private void BuffLineSelectGeometryService_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Geometry service failed: " + args.Error);
        }

        private void BuffLineQueryTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query failed: " + args.Error);
        }

0 Kudos
3 Replies
ChristineZeller
Occasional Contributor
I think my problem lies in my code behind but just incase here's most of xaml code

Here's my xaml for the buffer graphic (resource)
<esri:SimpleFillSymbol x:Key="BufferSymbol" Fill="#66BB0000" BorderBrush="#88CC0000" BorderThickness="2"  />

and my line graphic (resource) the line is good:
<esri:SimpleLineSymbol x:Key="ClipLineSymbol" Color="Red" Width="2" />

Here's my xaml for my user panel:

 <!-- Line Buffer Point Select Window -->
        <userControls:CollapsiblePanel x:Name="BufferLineSelect"  IsExpanded="False" 
                                           RenderTransformOrigin="1,0"
                                           VerticalContentAlignment="Top" HorizontalContentAlignment="Right" Margin="0,75,10,0" Effect="{StaticResource dropShadow}" >
            <Grid x:Name="BufferLineSelectGrid"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  >
                <Border Background="{StaticResource CommonPanelBorderBackgroundBrush}" BorderBrush="{StaticResource CommonBorderBrush}" BorderThickness="1" CornerRadius="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Border Effect="{StaticResource miniDropShadow}" Style="{StaticResource RibbonElementBorder}" Padding="15" Margin="10" >
                        <StackPanel Orientation="Vertical" Margin="10" HorizontalAlignment="Left" >
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">

                                <TextBlock x:Name="BufferSelectLineInformationTextBlock" 
                                         Text="Select a layer from the drop down list.  Then enter a Buffer Distance. Next click on map to set a location. A buffer will used to display the layer that is selected in drop down menu within buffer distance specified (feet) of the location." 
                                            Margin="0,0,5,0"  Width="290" HorizontalAlignment="Left" TextWrapping="Wrap" FontFamily="Arial" Foreground="#FFFFFFFF"  />
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                                    <TextBlock Text="Enter a Buffer Distance (in feet). The Value must be greater than 0: " VerticalAlignment="Center" FontFamily="Arial" Foreground="#FFFFFFFF"  />
                                    <TextBox x:Name="BufferLineSelectDistanceTextBox" Text="500" Width="50" TextAlignment="Right" Margin="0,0,5,0" />
                                </StackPanel>

                                <!-- Button for line creation-->
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="3">
                                    <TextBlock Text="Click Button to enable Draw Polyline: " VerticalAlignment="Center" FontFamily="Arial" Foreground="#FFFFFFFF" />

                                    <Button Style="{StaticResource ClearButtonStyle}" Cursor="Hand" HorizontalAlignment="Center" Margin="0,0,0,5"  ToolTipService.ToolTip="Create Polyline" Click="CreateBuffLine_Click">

                                        <Grid  HorizontalAlignment="Center" VerticalAlignment="Top" Margin="-5">
                                            <Image Source="images/DrawPolyline.png" Height="50" Width="50" Stretch="UniformToFill" Margin="5,0,5,0" />
                                        </Grid>
                                    </Button>

                                </StackPanel>
                                <!--End Button for line creation-->
                                
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </Border>
                <Image Source="images/CloseX.png" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="3" Stretch="None" Cursor="Hand" ToolTipService.ToolTip="Close" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonDown" >
                            <actions:ToggleCollapseAction TargetName="BufferLineSelect" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Image>
            </Grid>
        </userControls:CollapsiblePanel>

        <!--END Line Point Select Window -->
0 Kudos
JMcNeil
Deactivated User
Right away I thought your code looked solid but just to be sure I tested it and it is good.

Change this line and it will work

BufferSpatialReference = new SpatialReference(4326),

To

BufferSpatialReference = new SpatialReference(54034),

I think your problem is a decimal degree buffer spatial ref before flipping it out try to give it a super small buffer like 1 and it should look like 500 miles.

J.
0 Kudos
ChristineZeller
Occasional Contributor
Thanks J! That Worked!:)
0 Kudos