Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.WebMap
Imports Microsoft.Win32
Imports System.Web.Script.Serialization
Imports ESRI.ArcGIS.Client.ArcGISWebClient
Namespace ArcGISWPFSDK
Partial Public Class GenerateFeatures
Inherits UserControl
' Use ArcGISWebClient POST shapefile to the ArcGIS Portal generate operation. The generate operation requires a file to be passed
' in a multi-part post request.
Dim WithEvents agsWebClient As New ArcGISWebClient()
Private openFileDialog As OpenFileDialog
Public Sub New()
InitializeComponent()
openFileDialog = New OpenFileDialog()
openFileDialog.Filter = "Shapefiles (*.zip)|*.zip"
openFileDialog.RestoreDirectory = True
openFileDialog.Multiselect = False
End Sub
Private Sub BrowseButton_Click(sender As Object, e As RoutedEventArgs)
' Call the ShowDialog method to show the dialog box.
Dim userClickedOK As System.Nullable(Of Boolean) = openFileDialog.ShowDialog()
If userClickedOK = True Then
' Open the selected file to read.
Dim fileName As String = openFileDialog.FileName
' Uri to the ArcGIS Portal API generate operation.
' Reference documentation available here: http://www.arcgis.com/apidocs/rest/generate.html
Dim address As New Uri("http://www.arcgis.com/sharing/rest/content/features/generate")
' Get the file contents for the local file
Dim fs As New FileStream(fileName, FileMode.Open)
' Create ArcGISWebClient.StreamContent instance
Dim streamContent As New ArcGISWebClient.StreamContent() With { _
.Name = "file", _
.Filename = fileName, _
.Stream = fs, _
.ContentType = "application/zip" _
}
' Create a list of stream content to POST
Dim filestream As IList(Of ArcGISWebClient.StreamContent) = New List(Of ArcGISWebClient.StreamContent)()
filestream.Add(streamContent)
' Create dictionary to store parameter to POST
Dim postParameters As New Dictionary(Of String, String)()
' A class created to store publish parameters for the generate operation
Dim param As New GenerateFeaturesParams() With { _
.name = fileName.Substring(0, fileName.LastIndexOf(".")), _
.maxRecordCount = 1000, _
.generalize = False, _
.reducePrecision = True, _
.targetSR = MyMap.SpatialReference _
}
' Must specify the output type (json) the file type (shapefile) and the publish parameters
postParameters.Add("f", "json")
postParameters.Add("filetype", "shapefile")
postParameters.Add("publishParameters", SerializeToJsonString(param))
' Url to the generate operation, part of the ArcGIS Portal REST API (http://www.arcgis.com/apidocs/rest/generate.html)
Dim postURL As String = "http://www.arcgis.com/sharing/rest/content/features/generate"
fs.Close()
agsWebClient.PostMultipartAsync(New Uri(postURL), postParameters, filestream, Nothing)
End If
End Sub
Private Sub PostMultipartCompleted(ByVal a As System.Object, ByVal b As PostMultipartCompletedEventArgs) Handles agsWebClient.PostMultipartCompleted
If b.[Error] Is Nothing Then
Try
Dim reader As New StreamReader(b.Result)
Dim streamAsText As String = reader.ReadToEnd()
Dim serializer = New JavaScriptSerializer()
Dim jsonObject As Dictionary(Of [String], [Object]) = DirectCast(serializer.DeserializeObject(streamAsText), Dictionary(Of [String], [Object]))
Dim featureCollection As Dictionary(Of [String], [Object]) = DirectCast(jsonObject("featureCollection"), Dictionary(Of [String], [Object]))
Dim layers As Object() = DirectCast(featureCollection("layers"), Object())
For Each layer As Dictionary(Of [String], [Object]) In layers
Dim layerString As String = serializer.Serialize(layer)
Dim featureLayer__1 As FeatureLayer = FeatureLayer.FromJson(layerString)
If featureLayer__1 IsNot Nothing Then
' Add the feature layer to the map and zoom to it
MyMap.Layers.Add(featureLayer__1)
MyMap.ZoomTo(featureLayer__1.FullExtent.Expand(1.25))
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "FeatureLayer creation failed", MessageBoxButton.OK)
End Try
End If
End Sub
' See MSDN for more info: http://msdn.microsoft.com/en-us/library/bb412179(VS.100).aspx
Public Shared Function SerializeToJsonString(objectToSerialize As Object) As String
Using ms As New MemoryStream()
Dim serializer As New DataContractJsonSerializer(objectToSerialize.[GetType]())
serializer.WriteObject(ms, objectToSerialize)
ms.Position = 0
Using reader As New StreamReader(ms)
Return reader.ReadToEnd()
End Using
End Using
End Function
Private Sub Button_ClearMap(sender As Object, e As RoutedEventArgs)
Dim featureLayers As New List(Of FeatureLayer)()
For Each layer As Layer In MyMap.Layers
If TypeOf layer Is FeatureLayer Then
featureLayers.Add(TryCast(layer, FeatureLayer))
End If
Next
For i As Integer = 0 To featureLayers.Count - 1
MyMap.Layers.Remove(featureLayers(i))
Next
End Sub
End Class
' Parameter class for Generate operation
Partial Public Class GenerateFeaturesParams
Public Property name() As String
Get
Return m_name
End Get
Set(value As String)
m_name = value
End Set
End Property
Private m_name As String
Public Property targetSR() As SpatialReference
Get
Return m_targetSR
End Get
Set(value As SpatialReference)
m_targetSR = value
End Set
End Property
Private m_targetSR As SpatialReference
Public Property maxRecordCount() As Integer
Get
Return m_maxRecordCount
End Get
Set(value As Integer)
m_maxRecordCount = value
End Set
End Property
Private m_maxRecordCount As Integer
Public Property generalize() As Boolean
Get
Return m_generalize
End Get
Set(value As Boolean)
m_generalize = value
End Set
End Property
Private m_generalize As Boolean
Public Property reducePrecision() As Boolean
Get
Return m_reducePrecision
End Get
Set(value As Boolean)
m_reducePrecision = value
End Set
End Property
Private m_reducePrecision As Boolean
End Class
End Namespace