Python Scripting Extension
This page describes the GeoServer Python scripting extension.
The extension as well as this page are still a work in progress. |
Overview
Jython is the Java implementation of the Python language that allows Python code to execute on the JVM.
Installation
TODO: Update when added as GeoServer community module.
Scripts
Python scripts are stored in the GeoServer data directory under the python/scripts directory. There is nothing special about the scripts, they are written in plain old python.
Scripts can be executed by making http calls. Consider the following very simple script:
print 'hello world'
That is located at <GEOSERVER_DATA_DIR>/python/scripts/hello.py. This script is called with the following request:
GET http://localhost:8080/geoserver/python/scripts/hello.py
Arguments can be passed to scripts by specifying key value pairs in the query string of the request. These key value pairs end up as local variables in the script. Consider the following script:
print '%s' % greeting
That makes use of a local variable named greeting. To call this script:
GET http://localhost:8080/geoserver/python/scripts/hello.py?greeting=Howdy
Reusable Modules
As noted above the python/scripts directory is where scripts are located. The python/lib directory is used to store modules that are intended to be reused by multiple scripts.
Consider the following module:
class Greeting(object):
def __init__(self, msg):
self.msg = msg
def greet(self):
print '%s' % msg
Located in the file python/lib/greeting.py. Now consider the following script that makes use of the module:
from greeting import Greeting
g = Greeting('Hello')
g.greet()
GeoServer API
Undoubtedly scripts will want to interact with GeoServer in some way. The Python extension provides an api to make this easy. The geoserver module provides a number of classes that allow for access to GeoServer:
These classes are built on top of classes from the GeoScript library that provides Python bindings for GeoTools.
Python Compatibility
Currently the extension works against Jython 2.5.1 which implements all of Python 2.5.
Examples
Adding a GeoServer DataStore
from geoserver import Catalog cat = Catalog('topp') from geoscript.workspace import H2 db = H2('acme') db.create('widgets') cat.add(db, 'acme', description='Acme Corporation') st = cat['acme'] print st.layers()
Accessing a GeoServer Layer
from geoserver import Catalog
cat = Catalog('topp')
print cat.stores()
[]
st = cat['states_shapefile']
print st.layers()
[]
l = st['states']
print l.count()
49
print l.bounds()
..
Modifying a GeoServer Layer
from geoserver import Catalog
cat = Catalog('topp')
l = cat['states_shapefile']['states']
l.title = 'foo'
append(l.keywords, 'bar')
l.save()
..
Interactive Console
The extension includes a very basic interactive console available as a demo page.
The console works best in Firefox.


Use Cases
The following is the wish list based on feedback from teh community:
- add a new output format
- add a transaction listener
- add a dispatcher callback
- add a WPS process
- add a datastore
- a restful endpoint