T U T O R I A L S
Consuming
a map service
In this tutorial, we will create a desktop
client application in Microsoft Visual Studio to consume an ArcGIS Server
Web map service. Visual Studio 2005 is used
in the example, but the pattern will work in later versions. The
MapServer proxy and value objects for the map service will be created
dynamically in Visual Studio using the SOAP toolkit (wsdl.exe) included
with the .NET 2.0+ SDK. The application will use
the ArcGIS Server SOAP API via the MapServer proxy and value objects to
request a new map image and display it in the desktop client application.
An ArcGIS Server map service that is enabled as
a Web service will be required for this tutorial.
Sample code for this tutorial is available here: ConsumingMapService.zip
Create the Windows Application
project
In Visual Studio, click File, New Project.
In the Add New Project dialog, under Project Types, click Visual C# Projects.
Under Templates, select Windows Application. For the project name, specify SOAPClient.
Click OK. The project will open with a form named Form1.
Add a reference to an ArcGIS Server Web map service
In
Solution Explorer under the SOAPClient project, right-click the References
folder and select Add Web Reference.
In
the Add Web Reference dialog, in the URL combo box, enter the Web service
endpoint to the WSDL for an ArcGIS Server Web map service. For
example: http://localhost/arcgis/services/NorthAmerica/MapServer?wsdl
Note that "?wsdl" as appended
to specify that the WSDL for the map service should be returned. The
methods on the MapServer SOAP proxy should be displayed. Change
the Web Reference name to something intuitive, such as "wsmap",
and click Add Reference. The MapServer proxy and value objects are created
for you. Note that the Web reference name is the
namespace for the MapServer proxy and value objects.
Add controls the Windows form
Add a Button control to the form and set it's Text property to "Get Map".
Add
a PictureBox control to the form. Organize the
controls on the form using the screenshot below.
Add code to get a map image and display in the PictureBox control
Double-click on the Get Map button to open the Form1.cs code file to the OnClick event. The following code will use the dynamic ArcGIS Server Web service proxy and value objects to request a map image from an ArcGIS Server map service.
In
the OnClick event method, add the following code:
Create a new instance of the MapServer proxy and set the Url property
to a valid ArcGIS Server Web map service endpoint.
wsmap.NorthAmerica_MapServer mapserver = new wsmap.NorthAmerica_MapServer();
mapserver.Url = "http://localhost/arcgis/services/NorthAmerica/MapServer";
To create a map image two value objects are required, a MapDescription and an ImageDescription. The MapDescription provides the information on the content of the map and the ImageDescription provides image properties. For the MapDescription, we'll use the default MapDescription of the default map data frame in the map document associated with the map service.
wsmap.MapServerInfo mapinfo = mapserver.GetServerInfo(mapserver.GetDefaultMapName());
wsmap.MapDescription mapdesc = mapinfo.DefaultMapDescription;
The ImageType value object stores the image format and how it will be returned to the client. In this case, a jpeg image will be created and the url to the image on a Web server will be returned to the client.
wsmap.ImageType imgtype = new wsmap.ImageType();
imgtype.ImageFormat = wsmap.esriImageFormat.esriImageJPG;
imgtype.ImageReturnType = wsmap.esriImageReturnType.esriImageReturnURL;
The ImageDisplay value object stores the size and resolution of the map image. The size is defined in pixels - in this case the size of the PictureBox control. The image DPI is used by the map server object to calculate map scale. It will be used to determine if scale dependent layers should be drawn and the size or width of feature or graphic symbols.
wsmap.ImageDisplay imgdisp = new wsmap.ImageDisplay();
imgdisp.ImageHeight = pictureBox1.Height;
imgdisp.ImageWidth = pictureBox1.Width;
imgdisp.ImageDPI = 96;
ImageDescription is a value object that stores a reference to the ImageDisplay and ImageType value objects.
wsmap.ImageDescription imgdesc = new wsmap.ImageDescription();
imgdesc.ImageDisplay = imgdisp;
imgdesc.ImageType = imgtype;
The ExportMapImage method on the MapServer proxy serializes the MapDescription and ImageDescription value objects into a SOAP request for a new map image and submits it to the Web service endpoint defined earlier. A SOAP response is returned and the MapServer proxy deserializes the contents and returns a MapImage value object. In this case, MapImage has a property, ImageURL, which stores the url to the map image generated by the ArcGIS Server map service.
wsmap.MapImage mapimg = mapserver.ExportMapImage(mapdesc, imgdesc);
Using standard .NET network classes to manage HTTP requests and responses, the map image can be retrieved as a byte stream from the map image url. A new native .NET Image is created locally in memory and assigned to the PictureBox control Image property for display.
System.Net.HttpWebRequest webreq =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(mapimg.ImageURL);
System.Net.HttpWebResponse webresp =
(System.Net.HttpWebResponse)webreq.GetResponse();
System.Drawing.Image img = System.Drawing.Image.FromStream(webresp.GetResponseStream());
pictureBox1.Image = img;
Run the application
Start the application and click on the Get
Map button. The PictureBox control should display
the map image generated by the ArcGIS Server map service.