Geoprocessing service example: Selecting data
Folder |
SelectingData |
Purpose |
Demonstrates various ways to select data by attribute query or location query. |
Services |
|
Geoprocessing tasks |
SelectAndMail, SelectAndPrint |
Inputs |
Varies by model; usually a query string or a feature set |
Output |
Selected data feature class, text, and PDF file |
Data |
Uses a dataset of an area in the city of Manhattan, Kansas |
Extensions |
None |
About these services
A common task for geoprocessing services is to select a subset of data based on an attribute query or a spatial query. Another common task is to print the output of a service in different formats so the results can be easily disseminated. The models in this example demonstrate the combination of these two tasks. The primary geoprocessing tools used in these models are Select Layer By Attribute and Select Layer By Location. The tools take layers as input. Their output is the updated input layer containing the selected features. Two script tools are used to print selected features: one prints the selected feature table attributes and values as a text file, the other prints the selected feature map as a PDF file.
In this example, you'll be working with the SelectParcel.tbx toolbox. SelectParcel.tbx contains two toolsets: SelectScripts and SelectModels. SelectScripts contains two script tools:
- MailTable takes a parcel layer as input and prints the parcel ID and address of each selected feature to a text file.
- PrintMap takes an ArcMap document, data frame, layer name, source of layer as inputs, and exports the content of the map document to a PDF file.
- SelectAndMail demonstrates selecting all parcels within a search distance of an input parcel ID and uses the MailTable script tool to print the output as a text file.
- SelectAndPrint combines the SelectAndMail model and PrintMap script tool to select and print the selected features to a PDF file.
Data
The data is of a small area in the city of Manhattan, Kansas. The data for this example can be downloaded from ArcGIS.com. Once downloaded, extract the zip file to C:\arcgis\ArcTutor. The geodatabase used in this example is GP Service Examples\SelectingData\ToolData\ManhattanKS.gdb.
Overview of the models
SelectAndMail model
The first few processes of the SelectAndMail model is illustrated below. The model contains the project data variable Parcels and the variable Parcel ID, a model parameter, which is used in the Expression parameter for the Select Layer By Attribute tool. This parameter uses variable substitution to construct an expression that selects features whose PID attribute matches the value of the Parcel ID variable.
Although it is not required, it is a good practice to make variables used in variable substitutions a precondition to the process that substitutes the variable. To make a precondition, follow these steps:
- In ModelBuilder, right-click a tool and click Properties.
- Click the Preconditions tab.
- Choose the variable or variables that are a precondition to tool execution.
![SelectAndMail model illustration part 1 SelectAndMail model illustration part 1](0154/GUID-14974BBF-85F6-49C0-BB9E-CFE0F6B4DD06-web.png)
Both the Select Layer By Attribute and Select Layer By Location tools have several options for selection type. For example, the Select Layer by Attribute tool above uses the default NEW_SELECTION. You can use other selection types such as add or remove features from the current selection.
The Select Layer By Location tool lets you establish a spatial relationship, such as CONTAINS, WITHIN, and INTERSECT. The SelectAndMail model uses INTERSECT as illustrated below.
![This is required This is required](0154/GUID-DFDB9795-0270-4EE7-BD02-64C1EEEB6A25-web.png)
The remainder of the SelectAndMail model is illustrated below. The output of Select Layer By Location is copied to a feature class Selected Parcels using Copy Features. Selected Parcels is used as an input to the script tool MailTable, which prints the parcel ID and address to the text file Output.txt.
![SelectAndMail model illustration part 2 SelectAndMail model illustration part 2](0154/GUID-1AC25664-A9ED-4BE1-8266-1213DA4F3139-web.png)
SelectAndPrint model
The model SelectAndPrint combines the SelectAndMail model and the PrintMap script tool to select and print the selected features as a PDF file. The output of the model SelectAndMail is Selected Parcels, which is used as the input of the script tool PrintMap. PrintMap requires you to provide the values for input parameters Map, DataFrame, Layer, DataSource, WorkspaceType, and the output parameter OutputPDF, as illustrated below:
![SelectAndPrint model SelectAndPrint model](0154/GUID-A80F6AE1-35A5-4917-8AE0-649F65695BBA-web.png)
Make a connection to ArcGIS Server
Make sure you have the permission to publish to the server. If you do, follow the steps to create a server publish or manage connection:
- In the Catalog window, navigate to GIS Servers > Add ArcGIS Server to bring up the ArcGIS for Server wizard.
- Check Publish GIS Services or Administer GIS Server and click Next to bring up the General window.
- In the General window, enter the value of Server URL and provide User Name and Password for authentification.
- Click Finish to complete the connection.
You should see the server connection appear under GIS Servers.
Share the map service
Follow the next steps to create a map service to be used as a reference map:
- From the Catalog window, navigate to C:\arcgis\ArcTutor\GP Service Examples\SelectingData.
- Open ParcelBaseMap.mxd.
- From the ArcMap pull down menu, select File > Share as > Service. The Share a Service wizard opens.
- Check the radio button of Publish a service and click Next.
- Provide a connection to ArcGIS Server. Name the service ParcelBaseMap. Click Next.
- Choose the default root folder or select another folder and click Continue to open the Service Editor.
- In the Service Editor window, click Capabilities to show all the options. Mapping and KML are checked by default. Uncheck KML.
- Click Item Description and provide content for the required fields Summary and Tags.
- Click Analyze
.
- If you see any warning
or error
messages in the Prepare window, you can fix them by right-clicking the row with the error or warning message and clicking the highlighted message.
- When you are ready, click Publish
to publish the service.
You should get a successful message when it is published. After refreshing the server connection that you created in the previous section, you should be able to see the map service under the server connection.
Share the geoprocessing service
Run SelectAndMail tool and share the result
- From the Catalog window, navigate to C:\arcgis\ArcTutor\GP Service Examples\SelectedParcelMap. If you do not want to run from the directory, copy and paste to a local directory.
- Open SelectAndMail.mxd.
- From the Catalog window, navigate to the Home
directory. Expand the Models toolset of SelectParcel.tbx and double-click SelectAndMail to open the tool dialog box.
- For the first parameter Parcel ID, you can set the ID you would like to select or use the default value.
- For the parameter Search Distance, you can set the distance and unit or use the default value.
- For the parameter Selected Parcels, you can set your own output path or use the default value. This is where you save the feature class of selected parcels.
- For the parameter OutputTxt, you can set your own output text file path or use the default value. This is where you save the text file that shows the values of parcel ID and addresses.
- Click OK to run the tool.
- If the Results window is not open, click Geoprocessing > Results on the ArcMap pull down menu.
- In the Current Session node of the Results window, examine the SelectAndMail result created by the steps above.
The result should display the model tool icon
. If an error
is displayed, you'll need to expand the result, read the error message contained in the Messages node, then rerun the tool with modifications until a valid result is created.
To examine the contents of the output text file, expand the result in the Results window and double-click Output.txt to open the file in your default text editor.
- To share the model task as a geoprocessing service:
- Right-click the result in the Results window and select Share As > Geoprocessing Service.
- The Share as Service window opens and displays three options. Check the option Publish a service and click Next.
- Select the server administration connection you created previously in Choose a connection and provide a service name in Service name. Click Next.
When providing the Service name, if you leave the default name SelectAndMail, your service and task name will be the same. If you would like the service name to be different than the task name, provide a different service name.
- Choose either Use existing folder or Create new folder and click Continue to bring up Service Editor window.
- In the left pane of Service Editor, click Parameters. In the right pane of Service Editor, navigate to Maximum number of records returned by the server under Properties. Change the default value 1000 to 2000 because the number of output features may exceed 1000.
- Click Publish on the upper right corner of the Service Editor. A Copying Data to Server window appears showing the layer Parcel will be uploaded to the server. Click OK.
- You will see two windows: Package in progress and Package in Service. These will be followed by a Succeeded window with a message Successfully published service when publishing is done. Click OK to finish the publishing process.
Run SelectAndPrint and share the result
- From the Catalog window, navigate to C:\arcgis\ArcTutor\GP Service Examples\SelectedParcelMap. If you do not want to run from the directory, copy and paste to a local directory.
- Open SelectAndPrint.mxd.
- From the Catalog window, navigate to the Home
directory. Expand the Models toolset of SelectParcel.tbx and double-click SelectAndPrint to open the tool dialog box.
- For first parameter Parcel ID, you can set the ID you would like to select or use the default value.
- For the parameter Search Distance, you can set the distance and unit or use the default value.
- For the parameter Selected Parcels, you can set your own output path or use the default value. This is where you save the feature class of selected parcels.
- For the parameter OutputPDF, you can set your own output PDF file path or use the default value. This is where you save your selected parcels in a Map file with PDF format.
- Click OK to run the tool.
- If the Results window is not open, click Geoprocessing > Results on the ArcMap main menu.
- In the Current Session node of the Results window, examine the SelectAndPrint result created by the steps above.
The result should display the model tool icon
. If an error
is displayed, you'll need to expand the result, read the error message contained in the Messages node, then rerun the tool with modifications until a valid result is created.
To examine the contents of the output PDF file, expand the result in the Results window and double-click OutputPDF.pdf to open the file.
- To share the model task as a geoprocessing service, follow the next steps:
- Right-click the model tool icon that you ran in the previous step and select Share As > Geoprocessing Service.
- A Share as Service window displays with three options. Check the option Publish a service and click Next.
- Select the server administration connection you created previously in Choose a connection and provide a service name in Service name. Click Next.
When providing the Service name, if you leave the default name SelectAndPrint, your service and task name will be the same. If you would like the service name to be different than the task name, provide a different service name.
- Choose either Use existing folder or Create new folder and click Continue to bring up Service Editor window.
- Click Publish on the upper right corner of the Service Editor. A Copying Data to Server window appears showing the layer Parcel, and the Map file SelectAndPrint.mxd will be uploaded to the server. Click OK.
- You will see two windows: Package in progress and Package in Service. These will be followed by a Succeeded window with a message Successfully published service when publishing is done. Click OK to finish the publishing process.
Use map and geoprocessing service
The following steps show how to use the map service ParcelBaseMap and the two geoprocessing services SelectAndMail and SelectAndPrint that you previously published.
Use SelectAndMail service
- Open a new ArcMap document.
- In the Catalog window
- Navigate to your server connection under GIS Servers, select the map service ParcelBaseMap, and drag it into the ArcMap table of contents.
- Expand the map service in the table of contents to view the Parcels layer.
- In the Catalog window, navigate to your server connection under GIS Servers, find the geoprocessing service SelectAndMail that you published in the previous section. Expand the service to see the task SelectAndMail.
- Double-click the task SelectAndMail to open the task dialog box.
- Keep the default value of the parameters or provide your own values, and click OK to run the task.
- The output file Output will be added to the table of contents as a table.
- Check the Results window. Under the session Current Session and task SelectAndMail, you should see the output text file Output.txt.
- Open Output.txt by double-clicking the file in the result. You can see the content containing the attributes and values of Parcel ID and Address of the selected parcels.
Use SelectAndPrint service
- Open a new ArcMap document.
- In the Catalog window
- Navigate to your server connection under GIS Servers, select the map service ParcelBaseMap, and drag it into the ArcMap table of contents.
- Expand the map service in the table of contents to view the Parcels layer.
- In the Catalog window, navigate to your server connection under GIS Servers, find the geoprocessing service SelectAndPrint that you published in the previous section. Expand the service to see the task SelectAndPrint.
- Double-click the task SelectAndPrint to open the task dialog box.
- Keep the default value of the parameters or provide your own values, and click OK to run the task.
- Check the Results window. Under the session Current Session and task SelectAndPrint you will see the output file OutputPDF.pdf.
- Open OutputPDF.pdf by double-clicking it in the result. You will see a map file in PDF format showing the selected parcels.