>  Docs Center  >  Geospatial Services Framework  >  GSF Tutorial: Python Engine
Geospatial Services Framework

GSF Tutorial: Python Engine

GSF - Tutorial - Python Engine

Configure the Server to Enable the Python Engine


To enable the gsf-python-engine, add it to the list of engines in the config.json file.

To do this from a command line, start a command prompt in the GSFxx folder and execute the following command:

node updateConfig.js config.json --arrayadd engines={\"type\":\"gsf-python-engine\"}

*Note that each time you execute this command, another Python engine will be added to the list. You should only configure one Python engine, so you should only run this command once.

Create a new folder called 'python_tasks' in the GSFxx folder. This folder will contain the Python task files we create in this tutorial.

Next, update the Python engine configuration to specify the location of the task folder.

node updateConfig.js config.json --set engines[type:gsf-python-engine].taskDir=C:\GSFXX\python_tasks

The updateConfig.js script will automatically back up the original config.json file for you.

You may also manually enable the gsf-python-engine by editing the config.json as shown below. It is highly recommended that you save a backup copy of the config.json file before making any changes.

  "engines": [
    ...,
    {
      "type": "gsf-python-engine",
      "taskDir": "C:\\GSFXX\\python_tasks"
    }
  ],

Restart the GSF server any time this configuration file is changed so it reflects the new configuration.

You may also need to configure the Python engine to point to a specific version of Python on the server. By default, the Python engine uses the Python that is available on the system path.

If Python is not in your system path, or if you wish to use a specific Python installation, you must add the path to the Python executable to your GSFXX/config.json file.

node updateConfig.js config.json --set engines[type:gsf-python-engine].command=C:\python27\python.exe

Alternatively, make a backup copy of the config.json and edit the value for the "command" property to be the full path of the python executable you wish to use. As in:

  "engines": [
    ...,
    {
      "type": "gsf-python-engine",
      "taskDir": "C:\\GSFXX\\python_tasks",
        "command": "C:\\python27\\python.exe"
    }
  ],

Restart the server to use the new configuration.

Example: Creating a Python Task - "Addition"


The following steps will demonstrate the steps necessary to create a simple Python task. In this case, the example Python task will be one that calculates the sum of two numbers.

Create the .task File

The first step is to create a .task file that describes the task to the Python engine. Task definition files are JSON files describing the task, and have a ".task" file extension. The contents must be a TaskInfo JavaScript object describing the task.

Save the following as GSFXX/python_tasks/Addition.task

Addition.task

{
  "taskName": "Addition",
  "description": "Add two integers.",
  "inputParameters": {
    "ADDEND_1": {
      "type": "number",
      "required": true
    },
    "ADDEND_2": {
      "type": "number",
      "required": true,
      "default": 1
    }
  },
  "outputParameters": {
    "SUM": {
      "type": "number",
      "required": true
    }
  }
}

Create the Python Script

The task implementation file is a Python file which accepts the input parameters and implements the logic to be performed.

Save the following as GSFXX/python_tasks/Addition.py

Addition.py

import sys
import json
import time
import logging
import os 
logger = logging.getLogger('AdditionPythonLogger')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('server.log')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
def main(args):
  # Send a progress message
  print(json.dumps({"jobProgress":0,"message": "Preparing to add numbers."}))
  # Do the work
  sum = args['ADDEND_1'] + args['ADDEND_2']
  logger.info("Sum is " + str(sum))
  # Send another progress message
  print(json.dumps({"jobProgress":100,"message": "Done adding numbers."}))
  # Create the return value
  return {"SUM": sum}
if __name__ == "__main__":
  # Run the task using the inputs
  params = json.loads(sys.stdin.readline())
  # Print results to standard out
  print(json.dumps(main(params)))

Run the Example Task

With the server running, submit the following request by opening the following URL in a browser:

http://localhost:9191/ese/services/python/Addition/submitJob?ADDEND_1=3&ADDEND_2=5

Hitting refresh or checking the job console will allow you to see the status of the job and the final result, which will be similar to the following:

{
    "jobId": 29,
    "jobStatus": "esriJobSucceeded",
    "jobStatusURL": "ese/jobs/29/status",
    "jobProgress": 100,
    "jobProgressMessage": "Done adding numbers.",
    "jobRoute": "default",
    "taskName": "Addition",
    "serviceName": "python",
    "jobErrorMessage": "",
    "inputs": {
        "ADDEND_1": 3,
        "ADDEND_2": 5
    },
    "results": [
        {
            "name": "SUM",
            "value": 8
        }
    ],
    "messages": [
        {
            "type": "esriJobMessageTypeInformative",
            "description": "Submission Time: Thu Apr 06 2017 15:25:13 GMT-0600 (Mountain Daylight Time)"
        },
        {
            "type": "esriJobMessageTypeInformative",
            "description": "Start Time: Thu Apr 06 2017 15:25:13 GMT-0600 (Mountain Daylight Time)"
        },
        {
            "type": "esriJobMessageTypeInformative",
            "description": "End Time: Thu Apr 06 2017 15:25:13 GMT-0600 (Mountain Daylight Time) (Elapsed Time: 0.156 seconds)"
        },
        {
            "sandbox_url": "ese/jobs/29",
            "sandbox_contents": [
                {
                    "filename": "server.log",
                    "size": 66,
                    "dateModified": "2017-04-06T21:25:13.586Z"
                }
            ]
        }
    ]
}

Default Values

To define default values for any of the input parameters, add a "default" property to the input parameter definition in the task file.

In the example above, the ADDEND_2 input parameter has a default value of 1. With the server running, submit the following request by opening the following URL in a browser. Note that ADDEND_2 is omitted from the input parameters:

http://localhost:9191/ese/services/python/Addition/submitJob?ADDEND_1=3

Hitting refresh or checking the job console will allow you to see the status of the job and the final result, which will be similar to the following:

{
    "jobId": 554,
    "jobStatus": "esriJobSucceeded",
    "jobStatusURL": "ese/jobs/554/status",
    "jobProgress": 100,
    "jobProgressMessage": "Done adding numbers.",
    "jobOptions": {
        "route": "default"
    },
    "taskName": "Addition",
    "serviceName": "python",
    "jobErrorMessage": "",
    "inputs": {
        "ADDEND_1": 3
    },
    "results": [
        {
            "name": "SUM",
            "value": 4
        }
    ],
    "messages": [
        {
            "type": "esriJobMessageTypeInformative",
            "description": "Submission Time: Fri Aug 03 2018 12:57:36 GMT-0600 (Mountain Daylight Time)"
        },
        {
            "type": "esriJobMessageTypeInformative",
            "description": "Start Time: Fri Aug 03 2018 12:57:36 GMT-0600 (Mountain Daylight Time)"
        },
        {
            "type": "esriJobMessageTypeInformative",
            "description": "End Time: Fri Aug 03 2018 12:57:36 GMT-0600 (Mountain Daylight Time) (Elapsed Time: 0.176 seconds)"
        },
        {
            "sandbox_url": "ese/jobs/554",
            "sandbox_contents": [
                {
                    "filename": "server.log",
                    "size": 66,
                    "dateModified": "2018-08-03T18:57:36.609Z"
                }
            ]
        }
    ]
}



© 2019 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us