Select to view content in your preferred language

Viewshed

1000
8
06-27-2011 08:54 AM
JayKappy
Frequent Contributor
I am looking at the viewshed example....wondering how I can fire this off from a button.
I assume leave the "_geoprocessorTask" in the initialize component....

But I want to remove the MapClick from the map and assign that to fire from a button not just by default...
esri:Map x:Name="MyMap" MouseClick="MyMap_MouseClick"

Say I have an Identify, this process and another that all require map clicks....I cant do that from teh same sub...how do I move this away from the example and create a button to specify what is going to happen on the Map Mouse click???

THanks
0 Kudos
8 Replies
JenniferNery
Esri Regular Contributor
You can use your Button click to enable/disable calculation of viewshed by keeping a boolean. If false, you can return from the MouseClick event, otherwise, you can continue executing the GeoprocressorTask in the MouseClick event handler.
0 Kudos
JayKappy
Frequent Contributor
You can use your Button click to enable/disable calculation of viewshed by keeping a boolean. If false, you can return from the MouseClick event, otherwise, you can continue executing the GeoprocressorTask in the MouseClick event handler.


I thank you for your responce but not following that....return from the MouseClick?

You saying that I create a global Varaible.  This varaible starts as 0.  If I click the Viewshed Button I set it to 1.  I am not follwing this....can you exaplin a bit further...not askign for code but nto following...

Thanks
0 Kudos
JenniferNery
Esri Regular Contributor
For example:
bool calculateViewshed = false;
private void CalculateViewshed_Click(object sender, RoutedEventArgs e)
{
 calculateViewshed = !calculateViewshed;
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
 if (!calculateViewshed) return;
 //TODO: code to execute GeoprocessorTask.
}
0 Kudos
JayKappy
Frequent Contributor
Jennifer thanks.....I am lost on this one.. this is what I have....I thank you for your patience...

XAML
<esri:Map x:Name="MyMap" MouseClick="MyMap_MouseClick" 
<!-- SNIP -->


<!-- DOWN HERE A BUTTON -->
<Button HorizontalAlignment="Center" Click="CalculateViewshed_Click" Tag="0" Margin="5,5,0,0" VerticalAlignment="Center" Width="50" Height="20" Content="ViewShed" Foreground="#FFFFFFFF" 
 Style="{StaticResource GlassButton}"/>



VB
    
Private Sub MyMap_MouseClick(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs)

        If Not calculateViewshed Then
            Return
        End If


        ' BUFFER
        _geometryService = New GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
        AddHandler _geometryService.BufferCompleted, AddressOf GeometryService_BufferCompleted
        AddHandler _geometryService.Failed, AddressOf GeometryService_Failed

        _queryTask = New QueryTask("http://gis/arcgis/rest/services/MapServer/1")
        AddHandler _queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedBuffer
        AddHandler _queryTask.Failed, AddressOf QueryTask_Failed

        _pointAndBufferGraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerBuffer"), GraphicsLayer)
        _resultsGraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerBufferResults"), GraphicsLayer)


        Dim clickGraphic As New Graphic()
        clickGraphic.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol2"), ESRI.ArcGIS.Client.Symbols.Symbol)
        clickGraphic.Geometry = e.MapPoint
        ' Input spatial reference for buffer operation defined by first feature of input geometry array
        clickGraphic.Geometry.SpatialReference = MyMap.SpatialReference

        _pointAndBufferGraphicsLayer.ClearGraphics()
        _resultsGraphicsLayer.ClearGraphics()

        clickGraphic.SetZIndex(2)
        _pointAndBufferGraphicsLayer.Graphics.Add(clickGraphic)

        ' If buffer spatial reference is GCS and unit is linear, geometry service will do geodesic buffering
        Dim bufferParams As New ESRI.ArcGIS.Client.Tasks.BufferParameters() With
            {
                .BufferSpatialReference = New SpatialReference(4326),
                .OutSpatialReference = MyMap.SpatialReference,
                .Unit = LinearUnit.Meter
            }
        bufferParams.Distances.Add(4000)
        bufferParams.Features.Add(clickGraphic)

        _geometryService.BufferAsync(bufferParams)



    End Sub



    Private calculateViewshed As Boolean = False
    Private Sub CalculateViewshed_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        calculateViewshed = Not calculateViewshed
    End Sub

    '========================================================================================
    ' CALCULATE VIEWSHED VIEWER
    '========================================================================================

    Private Sub GetViewShed(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
        _geoprocessorTask.CancelAsync()

        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerViewShed"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        Dim mapPoint As MapPoint = e.MapPoint
        mapPoint.SpatialReference = New SpatialReference(3857)

        Dim graphic As New Graphic() With {.Symbol = TryCast(LayoutRoot.Resources("StartMarkerSymbol"), Symbol), .Geometry = mapPoint}
        graphicsLayer.Graphics.Add(graphic)

        MyMap.Cursor = System.Windows.Input.Cursors.Wait

        Dim parameters As New List(Of GPParameter)()
        parameters.Add(New GPFeatureRecordSetLayer("Input_Observation_Point", mapPoint))
        parameters.Add(New GPLinearUnit("Viewshed_Distance", esriUnits.esriMiles, Convert.ToDouble(MilesTextBox.Text)))

        _geoprocessorTask.OutputSpatialReference = New SpatialReference(3857)
        _geoprocessorTask.ExecuteAsync(parameters)
    End Sub

    Private Sub GeoprocessorTask_ExecuteCompletedViewShed(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.GPExecuteCompleteEventArgs)
        MyMap.Cursor = System.Windows.Input.Cursors.Hand
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerViewShed"), GraphicsLayer)

        For Each gpParameter As GPParameter In args.Results.OutParameters
            If TypeOf gpParameter Is GPFeatureRecordSetLayer Then
                Dim layer As GPFeatureRecordSetLayer = TryCast(gpParameter, GPFeatureRecordSetLayer)
                For Each graphic As Graphic In layer.FeatureSet.Features
                    graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), Symbol)
                    graphicsLayer.Graphics.Add(graphic)
                Next graphic
            End If
        Next gpParameter
    End Sub
0 Kudos
JayKappy
Frequent Contributor
I just cant see where to fire off the Viewshed code....I know this is very easy...I just cant wrap my brain around it...
Not sure where the Return is going or doing...

If button is not clicked then run the code int he MyMap_MouseClick.....
If button is clicked then run the code that is in GetViewShed

Thanks
0 Kudos
JayKappy
Frequent Contributor
The thing I am trying to get to is having 3-4 different buttons that rely on a map click...test for which one is clicked and then run the appropriate code.....

I jsut cant see how that is working...how do I get back to each section of code...all the examples show them individually under the MyMap_MouseClick sub...
Then I am gettign confused on the Return call....return to where?

I can get all to work independanlty .... but then I have to change the MapClick = ???

Thanks again sorry for my confusion....
0 Kudos
JayKappy
Frequent Contributor
I think that I can just duplicate the code below for each differnt type of map click on need.

        If Not calculateViewshed Then
            Return
        End If
        If Not ProfilemapThen
            Return
        End If
        ETC, ETC

My issue is that in the MyMap_MouseClick I am returning to somewhere, some code...I cant see where or how....they bot are map clicks and both require ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs    I would understand a call to that sub but am confused in the return statement
0 Kudos
JayKappy
Frequent Contributor
THink I got it...thanks Jennifer....
Will post after testing...

THANKS AGAIN
0 Kudos