Silverlight + WCF + ArcObjects Implementation of obtaining the map service layer list

Source: Internet
Author: User

I haven't written a blog for a long time. I have been busy with exams recently, but I am really hurt by the miserable mathematical and physical equations .....

After completing the examination, I started my mentor's project. However, I found that many of the project's things could not be implemented using the original ArcGIS API for Silverlight. As a result, we began to contact the powerful arcobject. Although I have been in touch with some ArcGIS engines before, I still think that the learning is in the fog, and now I forget about it, so I was hurt again by AO.

This article is a summary of AO. I would like to share it with you. If not, please kindly advise.

I. What does Silverlight need to call ao?

  • Install arcojects for. NET Framework
  • You need to use WCF or WebService
  • Add ArcGIS reference to a web project

I am a beginner with basic AO knowledge, so I won't sell it here. You can view the official help documentation on the premise that you need a certain degree of knowledge in English.

Arcobject official online documentation:

Http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html

I think the usage of WCF and WebService is similar, but it is more powerful. Therefore, we recommend that you use WCF. For details about the concept of WCF or WebService, refer to Microsoft's official documentation:

Official WCF documentation and examples:

Http://msdn.microsoft.com/en-us/library/dd456779.aspx

II. Implementation Process

The function is simple, that is, to obtain all layers under a map service (mapserver. For example, the map service published in this article is as follows:

We can see that there is another layer ):

  • Route (0)
  • Rivercenterline
  • Riverdataset_nd
  • Riverpolygon

Next we will obtain these layers by calling AO.

1. Create a Silverlight Project

Select enable WCF Ria Service

2. Add ArcGIS-related references to a web project, as shown in:

3. Add Silverlight-enabled WCF Service to a web project

Select Silverlight-enabled WCF Service, set the service name, and click Add

At this time, the basic work of the Web has been completed. Next, let's take a look at how to implement it on the WCF server.

4. Code implemented by the WCF Client

First, we need to connect to the GIS service, such as mapservice, before we can obtain all layers of the service and connect to the GIS service, as shown in:

For details about how to connect to the GIS server, refer to this Article in ESRI China Community diligentpig:

Http://bbs.esrichina-bj.cn/ESRI/viewthread.php? Tid = 42676

And instructions on the official website:

Http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Connecting_to_a_GIS_server/000100000200000000/

(1) connection code of the WCF Server:

First, add the following reference:

using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Server;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.Geometry;using System.Text;using ESRI.ArcGIS.Location;

 

And define the following variables:

// Used to store the names of all layers of GIS service. Public String [] layername = NULL; // obtain the host's SOM object public ESRI. arcGIS. server. iserverobjectmanager PSOM = NULL; // obtain the context public ESRI of the GIS service. arcGIS. server. iservercontext pservercontext = NULL; // The server connects to ESRI. arcGIS. ADF. connection. AGS. agsserverconnection agsconnection = NULL; // ID Recognition ESRI. arcGIS. ADF. identity identity = NULL; // GIS service ESRI. arcGIS. carto. imapserver2 pmapserver = NULL; // The object ESRI of the GIS service. arcGIS. carto. imapserverobjects2 pmapserverobject = NULL; // map of the GIS service object, which contains all ESRI layers of the service. arcGIS. carto. IMAP pmap = NULL;

 

Then it is instantiated in the WCF constructor:

// Set the username, password, and domain name identity = new ESRI. arcGIS. ADF. identity ("arcgiswebservices", "quzijing", "qzj"); // set the service address agsconnection = new ESRI. arcGIS. ADF. connection. AGS. agsserverconnection ("localhost", identity); agsconnection. connect (); PSOM = agsconnection. serverobjectmanager; // specify the address of the corresponding GIS service and the type of GIS service pservercontext = PSOM. createservercontext ("networkanalystservicefolder/rivermap", "mapserver"); // create a GIS service object and map pmapserver = pservercontext. serverobject as ESRI. arcGIS. carto. imapserver2; pmapserverobject = pmapserver as ESRI. arcGIS. carto. imapserverobjects2; pmap = pmapserverobject. get_map (pmapserver. defaultmapname );

 

Note the following:

A. Identify settings. It is consistent with the username, domain, and password of ArcGIS Server Manager.

B. Set the address of the ArcGIS Server. Generally, you can use localhost or an IP address. We recommend that you use the IP address when deploying an application.

C. Specify the GIS service address and service type:

The GIS service address is relative to the root directory of our ArcGIS Server. Assume that our existing GIS service is shown in:

If we want to connect to the mapserver corresponding to chinamap, the address used is chinamap, and the corresponding code is:

pServerContext = pSOM.CreateServerContext("ChinaMap", "MapServer");

If you want to obtain the mapserver corresponding to rivermap, You need to specify its path relative to the GIS service, that is, networkanalystservicefolder/rivermap. The sample code is:

 pServerContext = pSOM.CreateServerContext("NetworkAnalystServiceFolder/RiverMap", "MapServer");

Note that the GIS service type in the second parameter must be consistent.

(2). Obtain the layer name.

We have obtained the map of the specified service. With this map, we can obtain all the layers contained in the map. The sample code is as follows:

 if (pMap.LayerCount > 0)            {                LayerName = new string[pMap.LayerCount];                for (int i = 0; i < pMap.LayerCount; i++)                {                    ILayer layer = pMap.Layer[i];                    LayerName[i] = string.Format("Map Layer {0}:{1}", i, layer.Name);                }            }

In addition, we can also obtain the specified layer and its elements. Sample Code:

            IFeatureClass pFC = (pMap.get_Layer(1) as ESRI.ArcGIS.Carto.IFeatureLayer).FeatureClass;            IFeatureCursor pPolyLineCursor = pFC.Search(null, false);            IFeature pPolyFeature = pPolyLineCursor.NextFeature();

(3) define the contract for implementing the service in WCF

This contract method returns the names of all layers in GIS, that is, the preceding layername character array. The sample code is as follows:

[Operationcontract] Public String [] getlayerstring () {If (layername = NULL) {layername = new string [1]; layername [0] = "error: no layer obtained ";} return layername ;}

The above process completes the work on the server side. Next let's take a look at the implementation of the client.

5. Client implementation

Add a reference to the WCF Service

The structure of the project file is as follows:

Next we will implement the functions of the client. The implementation of the client is very simple. Here we will just get the name of the layer for the GIS service. We will define a button and a ListBox

 <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="83,23,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />        <ListBox Margin="12,61,172,0" Name="listBox1" VerticalAlignment="Top" HorizontalAlignment="Left" />  

Background code:

Aoservicereference. arcgis_aoserviceclient client = new arcgis_aoserviceclient (); Public mainpage () {initializecomponent (); client. getlayerstringcompleted + = new eventhandler <strong> (client_getlayerstringcompleted);} void complete (Object sender, subject e) {// bind the WCF result to ListBox listbox1.itemssource = E. result;} private void button#click (Object sender, routedeventargs e) {client. getlayerstringasync ();}

Final effect:

Click button to obtain the layer list.

 

(All Rights Reserved. For details, refer to the source)

 
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.