Tutorial: Publishing a geoprocessing service
With ArcGIS Server, you can publish geoprocessing services that allow you to submit jobs to the server and get back a set of results. The server accesses the model and does the work, freeing your computer's resources and eliminating the problem of sharing copies of the model.
Many ArcGIS applications can use geoprocessing services, including ArcMap, ArcGIS Explorer, and Web mapping applications that you create with ArcGIS Server. This tutorial focuses on building a geoprocessing service for use within a Web mapping application. If you're interested in building a geoprocessing service that will be mainly used in ArcMap, see the various Guide_to_the_geoprocessing_service_examples.
Before getting started with creating a geoprocessing service, you should know the following:
What's required to create a geoprocessing service?
Building a geoprocessing service requires that you first create a model using ModelBuilder in ArcGIS Desktop. A model is a logical sequence of geoprocessing tools and scripts that help you automate a GIS operation. A model contains geoprocessing tools and, optionally, scripts that you've written to help automate your work. You can't publish individual tools or scripts as geoprocessing services; you can only publish models. It's possible, however, to publish a model that contains only one tool or script.
If you haven't previously worked with models in ArcGIS, or you need a review, a good place to start is What is ModelBuilder in the ArcGIS Desktop Help.
What types of data can you use in your model?
Geoprocessing services support a limited set of input and output data types. For your model to run correctly on the server, you need to verify that all the input and output data types used by your model are supported. There are different strategies for modifying your model so that it uses only supported input and output data types. You can find these strategies, along with a list of all supported data types, in the topic Key_concepts_for_geoprocessing_services.
The server jobs directory
When you install ArcGIS Server, a directory is created for working with geoprocessing services. This is the server jobs directory, and by default, it's located at c:\arcgisserver\arcgisjobs. When you use a geoprocessing service, intermediate and output data from the model are stored in the server jobs directory. Each execution of the model, or job, has its own unique folder in the server jobs directory. This allows different people to use the service at the same time without the results getting confused.
Because the server jobs directory takes care of managing your intermediate and output data, you don't have to worry about setting the scratch and current workspaces in the model environment.
This example walks you through the process of building a simple model, publishing it as a geoprocessing service, and running the service in a Web mapping application. The steps include tips and best practices for working with geoprocessing services.
Building the model
The model you'll create in this example uses two tools: Buffer and Clip. When you run the model, you'll interactively define a set of points on the map along with a buffer distance. When the model runs, it will buffer the points at the specified distance. To exclude areas in the ocean, the model will then clip the resulting buffers to the shape of a feature class representing the coastline.
To build this model, you can use any data of your own, preferably with a feature class representing a boundary or coastline that you can clip to. The illustrations in this example use some data of the San Diego, California area. The model will clip to the feature class Land.
This model also requires that you've previously saved a point layer file somewhere on disk, containing the symbology you want to use for your input points.
Follow these steps to build the model:
- Start ArcMap and click OK to open a new, blank map.
- In the Catalog window, browse to the location of the data you've chosen to use for this tutorial and create a new folder named scratch. If the data is stored in your local profile (for example, C:\Documents and Settings\<user name>\...), move it out of your profile closer to the root (for example, C:\Data\GeoprocessingTutorial).
- Click Geoprocessing > Environments to display the Environment Settings.
- Expand Workspace and clear the box for the Current Workspace.
- Set the Scratch Workspace location by browsing to the scratch folder you just created. Then click OK to close the Environment Settings. As you'll see later in this tutorial, the scratch workspace is used to store intermediate data that's created when you run your model.
- Click Geoprocessing > Geoprocessing Options. Under Background Processing, uncheck Enable.
Disabling background processing makes it more convenient to run your model while you're designing it in ArcMap. This tutorial doesn't include a step to run the model while you're designing it, but this step is included in case you need to run your own model during the design phase.
- Save the map document into the folder containing the data you want to use for this tutorial. This will be your home folder for the map.
In the Catalog window, create a new toolbox in the home folder by right-clicking My Toolboxes and clicking New > Toolbox. You can name this toolbox GeoprocessingTutorial. The .tbx extension will be appended to the name.
Create a new model by right-clicking the toolbox and clicking New > Model. By default, the name is simply "Model". You can change this later.
- When you create a new model, the ModelBuilder window opens. At this point, you can start adding the tools that you want in the model. Begin by clicking Geoprocessing > Search for Tools and type Buffer in the search box.
The Buffer tool should be one of the first results of the search. Drag the Buffer tool from the Search window into the ModelBuilder window.
There are two Buffer tools in ArcGIS. Make sure you use the Buffer from toolboxes\system toolboxes\analysis tools.
- The end user will be able to select both the points to buffer and the buffer distance, so you need to make these variables and set them as model parameters. Make the first variable by right-clicking the Buffer box and clicking Make Variable > From Parameter > Input Features. An oval with the name Input Features appears in the model, connected to the Buffer tool.
- Right-click the Input Features oval and check Model Parameter. The model parameters are the inputs and settings that the end user can enter when running the model.
Right-click the Input Features oval, click Properties, then click the Data Type tab. Note that the data type is set to Feature Layer by default. Remember that geoprocessing services cannot take all data types as inputs; Feature Layer is not a supported input type, but Feature Set is. Change the value in the drop-down list to Feature Set. Additionally, while you are looking at this tab, browse to any point layer file to designate the symbology you want to use for your input points.
- Repeat the previous steps 12–14 to make an additional variable for the Buffer tool for Distance [value or field]. Make this a model parameter, and set the data type as Linear Unit. Once you're done creating the variable, double-click it and set a default value (for example, using the San Diego data, 2 kilometers might be an appropriate default buffer distance).
- Double-click the green oval Output Features and set the path to %SCRATCHWORKSPACE%\buffer.shp. SCRATCHWORKSPACE is a variable that, in this case, ensures that the intermediate data will be written to a temporary location. When you run the model in ArcMap, this temporary location is your scratch folder you created at the beginning of this tutorial. When you run the model from the server, this temporary location is the server jobs directory.
To make the model easier to read, rename Distance [value or field] to Buffer distance, and rename your output features (probably now Buffer.shp) to Buffers. To rename these model components, right-click them and click Rename.
You may need to drag around the model components to see everything. Your model should look similar to the one below:
Now add the second tool to the model. In the Search window, type Clip and press ENTER. In the results that appear, find the Clip tool and drag it onto the right side of the model.
There are several Clip tools in ArcGIS. Make sure you use the one in toolboxes\system toolboxes\analysis tools.
- Click the Add Connection button , then click the Buffers oval followed by the Clip box. You'll be prompted to specify whether you want Buffers to act as the Input Features or the Clip Features. Choose Input Features.
Switch back to the Select tool and double-click the Clip box in ModelBuilder. Set the Clip Features to be the boundaries or coastline feature class that you want to clip to. This example uses the Land feature class to eliminate buffer areas that are in the ocean.
Remember where the clip features are located. When you publish the geoprocessing service, you must make sure that the SOC account has access to this location.
- Set the output feature class location by double-clicking the final green oval of your model (probably called Output feature class) and typing the path %SCRATCHWORKSPACE%\ClippedBuffers.shp.
- Right-click the green oval representing your output (in this example, ClippedBuffers.shp) and check Model Parameter. Also check Add to Display.
You've completed the model. It should look similar to the one below. Save your model and close the ModelBuilder window.
- Find your model in the Catalog window. It should still be named "Model". Rename it BufferAndClip.
- Save your map document and go on to the next section.
Preparing the model and an associated map for publishing
Now that you've created the model, you can take the next steps to get its functionality into a Web application. At this point, you need to put some thought into what will happen in the Web application. The Web application can draw the model results in two ways: it can send the results data to the client to be drawn there, or it can draw the results on the server and send the completed image to the client.
If you decide that the client machine should draw the results, you don't need to make any additional preparations in ArcGIS Desktop; you only need to publish the toolbox as a geoprocessing service. There is, however, a limitation when drawing the results directly on the client: only symbology from the Web Application Developer Framework (ADF) graphics layer can be used. Available symbology includes simple marker, line, and fill symbols, as well as a few other symbol types listed in Defining output symbology for geoprocessing tasks. Cartographic line symbols, as well as gradient fill symbols and other types of complex symbologies, require that the results be drawn on the server.
If the results are drawn on the server, they can use a much greater variety of symbols. This is because the geoprocessing service works with an associated map service that can do the rendering necessary for all the symbols. Additional advantages to drawing the results on the server include the ability to use layers in the map service as input to the model, and the ability to work with large numbers of features in the results (if you tried to send a lot of features to be drawn on the client, it might take a long time or might not be permitted by the server administrator).
This example shows how to draw the results on the server, which requires you to create an associated map service. You need to create a map that can be used to visualize the inputs and results of the model. Once you've created the map, you need to add your model to the map as a tool layer. The following steps explain this process:
- Open the map document that you created in the previous section and add some data. For the purposes of this tutorial, the data can include a few reference datasets. A best practice for performance is to not to include a lot of base map data in this map service but rather connect directly to another base map service in your client application.
- Click Geoprocessing > Geoprocessing Options and make sure Overwrite the outputs of geoprocessing operations is checked.
Now create the tool layer by dragging your model into the ArcMap table of contents. Place it as the topmost layer.
Remember: Having a tool layer in your map is the key to getting a geoprocessing service that will work with your map service.
- You need to run the model once before publishing your service. Right-click the tool layer and click Open.
- Add some input features by clicking the pointer tool and clicking the map. Then click OK to run the model.
The input features for the model are already symbolized according to the schema you imported when you were configuring the Input Features parameter in your model. Now, use the layer properties to set the symbology that you want for the output features.
Remember: In this example, the output features are symbolized with an angled line fill symbol as shown below. This symbology is only possible when the server draws the results using an associated map service; it would not be available if you attempted to draw the results on the client.
- Make the output features (shown above as ClippedBuffers) invisible (unchecked) in the table of contents.
- You now have everything you need to publish the services. Save your map document and close ArcMap before continuing with the next section.
Publishing the service
Once you have a map document with a tool layer, you're ready to publish these as services. You'll publish the map document as a service and enable the Geoprocessing capability, which will create a geoprocessing service of the same name. You can use these two services within your Web application to run the model on the server and display the results.
Important: Before publishing your services, be sure to read and follow the guidelines presented in Preparing resources for publishing as services. These will help you ensure that the server has permissions to read and access all the data for your model and your map. Specifically, the SOC account needs to have read permissions to your model (toolbox), any data referenced by the model (such as the clip features), the map document, and any data in the map document. The SOC account also needs read and write access to the server jobs directory. If you're using the default server jobs directory installed with ArcGIS Server (C:\arcgisserver\arcgisjobs), then the installation process already applied the appropriate permissions for the jobs directory.
Follow these steps to publish the services:
Log in to ArcGIS Server Manager and click Publish a map, globe, or other GIS resource as a service.
Tip: There are two wizards in Manager that you can use to publish services. This link takes you to the Publish GIS Resource wizard, which is the simplest way to create a service. It asks you the minimum amount of information needed to create a service. Another feature of the Publish GIS Resource wizard is that you can use it to create associated services, like the geoprocessing and map services that you're going to create in this example.
The other way to create services is the Add New Service wizard. This wizard is the completely manual way of publishing a service; it prompts you for all the service parameters. You cannot use the Add New Service wizard to automatically create associated services.
- On the first panel of the wizard, you're prompted for the Resource that you want to publish. This is the map document with the tool layer. If it's in a shared directory, you can browse to the map document. If it's not in a shared location, carefully type the path of the map document.
Give your service a Name. The name you enter here will be used for both the map service and the geoprocessing service. Optionally, if you have your services grouped in folders, choose the appropriate folder, then click Next to move to the next page of the wizard.
On the second page, notice that two capabilities are enabled by default. Mapping is enabled for all map services. Geoprocessing is enabled because Manager detected that the map you're publishing has a tool layer. Publishing a map service with the Geoprocessing capability enabled creates an associated geoprocessing service with the same name. Leave these capabilities checked and click Next to move to the final wizard page.
- The final page of the wizard explains that two services will be created and gives their URLs. Click Finish to publish the services and close the wizard.
Creating a Web application with the Geoprocessing task
The final step in exposing your model to others is to create a Web application with a Geoprocessing task. You'll use ArcGIS Server Manager to create the Web application. Manager will help you choose the map service to display and the geoprocessing service that you want to expose to your users through the Geoprocessing task. Follow these steps to create the Web application in Manager:
- Click the Home tab in ArcGIS Server Manager and click Create a Web application. This will take you to the first page of a wizard that will help you set up the application.
Type a Name for your application. This name will appear in the URL that users type in their browser when they access your application. Optionally, you can also type a Description on this page that administrators can read when they view the list of Web applications in Manager. When you're finished with this page, click Next.
- On the second page, you choose which services will appear in your map. Click Add Layer, browse to the map service that you just published in the previous section, then click Add. You may need to first create a connection to your GIS server. If you need help with this page, see Adding layers to a Web application in Manager. When you're finished adding services, click Next to move to the next page.
On this page of the wizard, you will add the Geoprocessing task. Click Add Task, choose Geoprocessing, then click OK to add the task.
Before configuring the task, you need to designate a geoprocessing service for the task to use. Click Supporting Services, click Add, then, in the same way that you browsed for your map layer on the previous page, browse to your geoprocessing service.
In the list of Current Tasks, click Geoprocessing, then click the Configure button. On this dialog box, you can change the task name from Geoprocessing to something more descriptive. You can also add a help tip that will appear when users pause the mouse pointer on the task.
If you have multiple models in your toolbox, this is where you can choose the one that will appear in your task. You can only use one model per task.
In this example, you can keep the defaults for the Geoprocessing Task Settings. The Geoprocessing task section of the help can help you understand these settings, including results files and synchronous versus asynchronous tasks.
- Once you've configured the task the way you want it, click OK to close the dialog box and click Next to continue creating the Web application. The rest of the wizard allows you to set the color scheme, text, links, and map elements in your application. You can alter these settings however you want, proceeding through the rest of the wizard. The final page displays some information about the application that will be created and prompts you to click Finish to create the Web application.
Test your Web application by opening the Geoprocessing task. Define some points and run the tool to see the clipped buffers.