Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Effects
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Symbols
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
Partial Public Class FeatureSetJson
Inherits UserControl
Private graphicsLayerFromFeatureSet As GraphicsLayer
Private Shared _mercator As New ESRI.ArcGIS.Client.Projection.WebMercator()
Public Sub New()
InitializeComponent()
CreateFeatureSetJson()
End Sub
Private Sub Button_Load(sender As Object, e As System.Windows.RoutedEventArgs)
Try
Dim featureSet__1 As FeatureSet = FeatureSet.FromJson(JsonTextBox.Text)
Dim graphicsLayerFromFeatureSet As New GraphicsLayer() With { _
.Graphics = New GraphicCollection(featureSet__1) _
}
If Not featureSet__1.SpatialReference.Equals(MyMap.SpatialReference) Then
If MyMap.SpatialReference.Equals(New SpatialReference(102100)) AndAlso featureSet__1.SpatialReference.Equals(New SpatialReference(4326)) Then
For Each g As Graphic In graphicsLayerFromFeatureSet.Graphics
g.Geometry = _mercator.FromGeographic(g.Geometry)
Next
ElseIf MyMap.SpatialReference.Equals(New SpatialReference(4326)) AndAlso featureSet__1.SpatialReference.Equals(New SpatialReference(102100)) Then
For Each g As Graphic In graphicsLayerFromFeatureSet.Graphics
g.Geometry = _mercator.ToGeographic(g.Geometry)
Next
Else
Dim geometryService As New GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.ProjectCompleted, AddressOf geometryService_ProjectCompleted
AddHandler geometryService.Failed, AddressOf Geometry_Failed
geometryService.ProjectAsync(graphicsLayerFromFeatureSet.Graphics, MyMap.SpatialReference)
End If
End If
Dim simpleRenderer As New SimpleRenderer()
Dim symbolColor As New SolidColorBrush(Colors.Blue)
graphicsLayerFromFeatureSet.Renderer = simpleRenderer
If featureSet__1.GeometryType = GeometryType.Polygon OrElse featureSet__1.GeometryType = GeometryType.Polygon Then
simpleRenderer.Symbol = New SimpleFillSymbol() With { _
.Fill = symbolColor _
}
ElseIf featureSet__1.GeometryType = GeometryType.Polyline Then
simpleRenderer.Symbol = New SimpleLineSymbol() With { _
.Color = symbolColor _
}
Else
' Point
simpleRenderer.Symbol = New SimpleMarkerSymbol() With { _
.Color = symbolColor, _
.Size = 12 _
}
End If
Dim border As New Border() With { _
.Background = New SolidColorBrush(Colors.White), _
.BorderBrush = New SolidColorBrush(Colors.Black), _
.BorderThickness = New Thickness(1), _
.CornerRadius = New CornerRadius(10), _
.Effect = New DropShadowEffect() _
}
Dim stackPanelParent As New StackPanel() With { _
.Orientation = Orientation.Vertical, _
.Margin = New Thickness(12) _
}
For Each keyvalue As KeyValuePair(Of String, String) In featureSet__1.FieldAliases
Dim stackPanelChild As New StackPanel() With { _
.Orientation = Orientation.Horizontal, _
.Margin = New Thickness(0, 0, 0, 6) _
}
Dim textValue As New TextBlock() With { _
.Text = keyvalue.Value & ": " _
}
Dim textKey As New TextBlock()
Dim keyBinding As New Binding(String.Format("[{0}]", keyvalue.Key))
textKey.SetBinding(TextBlock.TextProperty, keyBinding)
stackPanelChild.Children.Add(textValue)
stackPanelChild.Children.Add(textKey)
If keyvalue.Key = featureSet__1.DisplayFieldName Then
textKey.FontWeight = InlineAssignHelper(textValue.FontWeight, FontWeights.Bold)
stackPanelParent.Children.Insert(0, stackPanelChild)
Else
stackPanelParent.Children.Add(stackPanelChild)
End If
Next
border.Child = stackPanelParent
graphicsLayerFromFeatureSet.MapTip = border
MyMap.Layers.Add(graphicsLayerFromFeatureSet)
Catch ex As Exception
MessageBox.Show(ex.Message, "GraphicsLayer creation failed", MessageBoxButton.OK)
End Try
End Sub
Private Sub geometryService_ProjectCompleted(ByVal s As Object, ByVal a As ESRI.ArcGIS.Client.Tasks.GraphicsEventArgs)
For i As Integer = 0 To a.Results.Count - 1
graphicsLayerFromFeatureSet.Graphics(i).Geometry = a.Results(i).Geometry
Next i
End Sub
Private Sub Geometry_Failed(ByVal s As Object, ByVal a As TaskFailedEventArgs)
MessageBox.Show("Projection error: " & a.Error.Message.ToString())
End Sub
Private Sub Button_ClearMap(sender As Object, e As RoutedEventArgs)
Dim graphicsLayers As New List(Of GraphicsLayer)()
For Each layer As Layer In MyMap.Layers
If TypeOf layer Is GraphicsLayer Then
graphicsLayers.Add(TryCast(layer, GraphicsLayer))
End If
Next
For i As Integer = 0 To graphicsLayers.Count - 1
MyMap.Layers.Remove(graphicsLayers(i))
Next
OutTextBox.Text = String.Empty
End Sub
Private Sub CreateFeatureSetJson()
Dim jsonInput As String = "{" & vbCr & vbLf & " \""displayFieldName\"" : \""AREANAME\""," & vbCr & vbLf & " \""geometryType\"" : \""esriGeometryPoint\""," & vbCr & vbLf & " \""spatialReference\"" : {\""wkid\"" : 4326}," & vbCr & vbLf & " \""fieldAliases\"" : {" & vbCr & vbLf & " \""ST\"" : \""State Name\""," & vbCr & vbLf & " \""POP2000\"" : \""Population\""," & vbCr & vbLf & " \""AREANAME\"" : \""City Name\"" " & vbCr & vbLf & " }, " & vbCr & vbLf & " \""features\"" : [" & vbCr & vbLf & " {" & vbCr & vbLf & " \""attributes\"" : {" & vbCr & vbLf & " \""ST\"" : \""CA\""," & vbCr & vbLf & " \""POP2000\"" : 3694820," & vbCr & vbLf & " \""AREANAME\"" : \""Los Angeles\""" & vbCr & vbLf & " }," & vbCr & vbLf & " \""geometry\"" : { \""x\"" : -118.4, \""y\"" : 34.1 }" & vbCr & vbLf & " }," & vbCr & vbLf & " {" & vbCr & vbLf & " \""attributes\"" : {" & vbCr & vbLf & " \""ST\"" : \""WA\""," & vbCr & vbLf & " \""POP2000\"" : 563374," & vbCr & vbLf & " \""AREANAME\"" : \""Seattle\""" & vbCr & vbLf & " }," & vbCr & vbLf & " \""geometry\"" : { \""x\"" : -122.3, \""y\"" : 47.5 }" & vbCr & vbLf & " }" & vbCr & vbLf & " ]" & vbCr & vbLf & "}"
' string.Replace necessary here to handle double-quotes escaped for the '@' string literal
JsonTextBox.Text = jsonInput.Replace("\", "")
End Sub
Private Sub Button_Show(sender As Object, e As RoutedEventArgs)
OutTextBox.Text = String.Empty
Dim featureLayer As New FeatureLayer() With { _
.Url = FeatureLayerUrlTextBox.Text _
}
AddHandler featureLayer.InitializationFailed, AddressOf featureLayer_InitializationFailed
AddHandler featureLayer.UpdateCompleted, AddressOf featureLayer_UpdateCompleted
AddHandler featureLayer.UpdateFailed, AddressOf featureLayer_UpdateFailed
MyMap.Layers.Add(featureLayer)
End Sub
Private Sub featureLayer_InitializationFailed(sender As Object, e As EventArgs)
MessageBox.Show("Url must reference a feature layer in a map or feature service.", "FeatureLayer initialization failed", MessageBoxButton.OK)
End Sub
Private Sub featureLayer_UpdateCompleted(sender As Object, e As EventArgs)
Dim featureSet As New FeatureSet(TryCast(sender, FeatureLayer).Graphics)
Dim jsonOutput As String = featureSet.ToJson()
OutTextBox.Text = jsonOutput
End Sub
Private Sub featureLayer_UpdateFailed(sender As Object, e As TaskFailedEventArgs)
MessageBox.Show(e.[Error].Message, "FeatureLayer update failed", MessageBoxButton.OK)
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class
End Namespace