-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
ArcGIS Blueprints
By :
In the previous section, you learned how to create a Python Toolbox and add tools. You created a new toolbox called InsertWildfires and added a tool called USGS Download. However, in that exercise, you didn't complete the geoprocessing operations that connect to an ArcGIS Server map service, query the service for current wildfires, and populate the feature class from the data pulled from the map service query. You'll complete these steps in the following section.
This section of the application uses the Python requests module. If you don't already have this module installed on your computer, you will need to do this at this time using pip.
The pip is a package manager that serves as a repository and installation manager for Python modules. It makes finding and installing Python modules much easier. There are several steps that you'll need to follow in order to install pip and the requests module. Instructions to install pip and the requests module are provided in the first few steps:
C:\Python27\ArcGIS10.3; C:\Python27\ArcGIS10.3\Scripts. The first path will provide a reference to the location of the Python executable and the second will reference the location of pip when it is installed. This makes it possible to run Python and pip from the Command Prompt in Windows.get-pip.py and select Save Link As or something similar. This will vary depending upon the browser you are using. Save it to your C:\ArcGIS_Blueprint_Python folder.C:\ArcGIS_Blueprint_Python\get-pip.py, and press Enter on your keyboard. This will install pip.pip install requests and press Enter on your keyboard. This will install the requests module.In the following steps, we will learn how to request data from ArcGIS Server:

def execute(self, parameters, messages):
inFeatures = parameters[0].valueAsText
outFeatureClass = parameters[1].valueAsTextQueryString parameters that will be passed into the query of the map service. First, import the requests and json modules:import arcpy import requests, json class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "Toolbox" self.alias = "" # List of tool classes associated with this toolbox self.tools = [USGSDownload]
agisurl and json_payload variables that will hold the QueryString parameters. Note that, in this case, we have defined a WHERE clause so that only wildfires where the acres are greater than 5 will be returned. The inFeatures variable holds the ArcGIS Server Wildfire URL: def execute(self, parameters, messages):
inFeatures = parameters[0].valueAsText
outFeatureClass = parameters[1].valueAsText
agisurl = inFeatures
json_payload = { 'where': 'acres > 5', 'f': 'pjson',
'outFields': 'latitude,longitude,incidentname,acres' }
r. Print a message to the dialog box indicating the response as:def execute(self, parameters, messages):
inFeatures = parameters[0].valueAsText
outFeatureClass = parameters[1].valueAsText
agisurl = inFeatures
json_payload = { 'where': 'acres > 5', 'f': 'pjson', 'outFields': 'latitude,longitude,incidentname,acres' }
r = requests.get(agisurl, params=json_payload)
arcpy.AddMessage("The response: " + r.text)
InsertWildfires in ArcCatalog. Execute the tool and leave the default URL. If everything is working as expected, you should see a JSON object output to the progress dialog box. Your output will probably vary from the following screenshot:
execute() method and convert the JSON object to a Python dictionary using the json.loads() method:def execute(self, parameters, messages):
inFeatures = parameters[0].valueAsText
outFeatureClass = parameters[1].valueAsText
agisurl = inFeatures
json_payload = { 'where': 'acres > 5', 'f': 'pjson', 'outFields': 'latitude,longitude,incidentname,acres' }
r = requests.get(inFeatures, params=json_payload)
arcpy.AddMessage("The response: " + r.text)
decoded = json.loads(r.text)
The following steps will guide you, to insert data in a feature class with the help of the ArcPy data access module:
ArcPy data access module, that is Arcpy.da, to create an InsertCursor object by passing the output feature class defined in the tool dialog box along with the fields that will be populated: def execute(self, parameters, messages):
inFeatures = parameters[0].valueAsText
outFeatureClass = parameters[1].valueAsText
agisurl = inFeatures
json_payload = { 'where': 'acres > 5', 'f': 'pjson', 'outFields': 'latitude,longitude,fire_name,acres' }
r = requests.get(inFeatures, params=json_payload)
arcpy.AddMessage("The response: " + r.text)
decoded = json.loads(r.text)
cur = arcpy.da.InsertCursor(outFeatureClass, ("SHAPE@XY", "NAME", "ACRES"))
For loop that you can see in the following code, and then we'll discuss what this section of code accomplishes:def execute(self, parameters, messages):
inFeatures = parameters[0].valueAsText
outFeatureClass = parameters[1].valueAsText
agisurl = inFeatures
json_payload = { 'where': 'acres > 5', 'f': 'pjson', 'outFields': 'latitude,longitude,fire_name,acres' }
r = requests.get(inFeatures, params=json_payload)
arcpy.AddMessage("The response: " + r.text)
decoded = json.loads(r.text)
cur = arcpy.da.InsertCursor(outFeatureClass, ("SHAPE@XY", "NAME", "ACRES"))
cntr = 1
for rslt in decoded['features']:
fireName = rslt['attributes']['incidentname']
latitude = rslt['attributes']['latitude']
longitude = rslt['attributes']['longitude']
acres = rslt['attributes']['acres']
cur.insertRow([(longitude,latitude),fireName, acres])
arcpy.AddMessage("Record number: " + str(cntr) + " written to feature class")
cntr = cntr + 1
del cur
The first line simply creates a counter that will be used to display the progress information in the Progress Dialog box. We then start a For loop that loops through each of the features (wildfires) that have been returned. The decoded variable is a Python dictionary. Inside the For loop, we retrieve the wildfire name, latitude, longitude, and acres from the attributes dictionary. Finally, we call the insertRow() method to insert a new row into the feature class along with the wildfire name and acres as attributes. The progress information is written to the Progress Dialog box and the counter is updated.
CurrentFires feature class in the WildlandFires geodatabase. The CurrentFires feature class is empty and has fields for NAMES and ACRES:


Change the font size
Change margin width
Change background colour