[GIS] Create layer from ArcGIS REST URL in ArcObjects using C#

arcgis-rest-apiarcgis-serverarcobjects

I am trying to create a layer and add it into a MapControl from ArcGIS REST URL. For example I use the following URL http://sampleserver6.arcgisonline.com/arcgis/rest/services/EmergencyFacilities/MapServer

I can do this with the help of this post

However, the code in the post seems to derive a SOAP url from the REST url. So I have few questions regarding this.

  1. Can I create the layer without deriving a SOAP url from the REST url.
  2. Does it always guaranteed that the SOAP url can be derived in this way. For example If the service URL uses the "URL mapping", how will this method work? For instance how can I work with this URL https://maps.stats.govt.nz/wss/service/arcgis1/guest

Best Answer

In Arc 10.2 you can use IMapServerRESTLayer. Using this avoids using SOAP. Here is some code (VB.Net) that shows how it is used, with some extra code. This code is for a AddIn button. The extra code checks if the connection is OK by requesting a json file. Also, there is code for displaying the json file, and getting the REST file general information. I have not figured out a way of getting the other layer properties such as those under the source tab i.e. Data Type.

The url is typical. You need to find the url by going to the web service and looking it up.

There are other ways of NOT using SOAP. This article by Anthony Baker talks about using json files to consume REST services. Also the background information in http://rest.elkstein.org/2008/02/what-is-rest.html is very good.

Here is the code (tested under Arc10.2) Protected Overrides Sub OnClick() Const csProceedureName As String = "cmdListBaseMap_OnClick" MsgBox("In text version")

    Try
        Dim pApp As IApplication
        Dim pMxDoc As IMxDocument
        Dim pMap As IMap
        Dim pView As IActiveView
        Dim resturl As String

        resturl = "http://services.thelist.tas.gov.au/arcgis/rest/services/Basemaps/Topographic/ImageServer/?f=lyr"        ' v2 ' works

        ' there was a problem getting the interface to work. 
        ' In addition to carto needed to include ESRI.ArcGIS.DataSourceRaster
        Dim RESTLayer As IMapServerRESTLayer
        RESTLayer = New MapServerRESTLayer

        Dim pLayer As ILayer


        pApp = My.ArcMap.Application
        pMxDoc = pApp.Document
        pMap = pMxDoc.FocusMap
        pView = pMxDoc.ActiveView


        ' this is done to test the connection
        ' Use web HttpWebRequest to see if connection OK and so can retrieve file
        Dim request_json_url As String = "http://services.thelist.tas.gov.au/arcgis/rest/services/Basemaps/Topographic/ImageServer/?f=json"
        Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(request_json_url), HttpWebRequest)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)

        If response.StatusCode = HttpStatusCode.OK Then
            ' connection and response OK
            'MsgBox("Status Code " & response.StatusCode & vbNewLine & " Status description " & response.StatusDescription & vbNewLine & " Encoding " & response.CharacterSet)


            '' this is one method of getting the json file
            ''Dim encoding As Text.Encoding = System.Text.Encoding.GetEncoding(response.CharacterSet)
            ''Dim reader As New StreamReader(response.GetResponseStream(), encoding)
            ''Dim streamtext As String = reader.ReadToEnd()
            ''MsgBox(streamtext)

            ' '' use a webclient to check connection, the try cast does exception handling
            ''Dim wc As WebClient = New WebClient
            ''Dim response_client As String
            ''    response_client = wc.DownloadString(request_json_url)
            ''    MsgBox(response_client)

            ' Get the Rest layer
            RESTLayer.Connect(resturl)
            RESTLayer.TransparentBackground(True)

            ' Cast to ILayer so it can be added to map
            ' this cast works
            pLayer = TryCast(RESTLayer, ILayer)
            If pLayer Is Nothing Then
                MsgBox("ERROR ( " & csProceedureName & ")  Cast from MapServerRESTLayer to Ilayer failed")
            Else
                'MsgBox("Cast from MapServerRESTLayer to Ilayer OK")
            End If


            ' add layer to map
            pMap.AddLayer(pLayer)
            pView.Refresh()

            '' This works
            '' Returns the Description 
            '' Dim pGenProperties As ILayerGeneralProperties
            ''pGenProperties = TryCast(pLayer, ILayerGeneralProperties)
            ''If pGenProperties Is Nothing Then
            ''    MsgBox("No general properties")
            ''Else
            ''    MsgBox("Properties -" & pGenProperties.LayerDescription & "-")
            ''End If

        Else
            MsgBox("ERROR ( " & csProceedureName & " ) " & response.StatusCode & vbNewLine & response.StatusDescription)
        End If


    Catch ex As Exception
        MsgBox("Error " & ex.Message)
    End Try


End