Python¶
脚本挂钩¶
应用程序¶
在python中,app hook基于 WSGI which provides a common interface for Python web application development. This is not a comprehensive introduction to WSGI, that can be found here ,但应用程序脚本必须提供名为 app 这需要一个包含环境信息的字典和一个启动响应的函数。
def app(environ, start_response):
# do stuff here
函数必须存在于名为 main.py 在一个命名的 应用程序目录 . 应用程序目录位于 scripts/apps 数据目录根目录下的目录:
GEOSERVER_DATA_DIR/
...
scripts/
apps/
app1/
main.py
...
app2/
main.py
...
应用程序可以通过web从路径访问 /script/apps/{{app}} 在哪里? {{app}} 是应用程序的名称。所有以该路径开头的请求都被调度到 app 中的函数 main.py .
Hello World示例¶
在本例中,构建了一个简单的“Hello World”应用程序。第一步是为名为 hello ::
cd $GEOSERVER_DATA_DIR/scripts/apps
mkdir hello
下一步是创建 main.py 文件::
cd hello
touch main.py
下一个 app 函数被创建并删除:
def app(environ, start_response):
pass
在应用程序功能中,将发生以下情况:
报告HTTP状态代码200
声明响应的内容类型,在本例中为“text/plain”
生成响应,在本例中是字符串“hello world”
步骤1和2通过调用 start_response 功能:
start_response('200 OK', [('Content-Type', 'text/plain')])
步骤3通过返回字符串内容数组来实现:
return ['Hello World']
最终完成版本:
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World!']
注解
WSGi允许生成响应而不是返回数组的其他方法。特别是,它支持返回响应内容的生成器。有关更多详细信息,请参阅wsgi文档。
焊接工艺规程¶
在Python中,wps/process接口与其他语言非常相似,只是有一些不同。进程是用一个名为 run 上面装饰着 geoserver.wps.process 装饰者:
from geoserver.wps import process
@process(...)
def run(...):
# do something
函数位于 scripts/wps 数据目录根目录下的目录。WPS过程需要元数据来向外部世界描述自己,包括:
A name 确定流程
短发 标题 描述过程
有选择地更长 描述 更详细地描述了这个过程
一本字典 输入 描述进程作为输入接受的参数
一本字典 输出 将流程生成的结果描述为输出
在 Python 中 name 隐式派生自包含进程函数的文件名。其余的元数据作为参数传递给 process 装饰工。这个 title 和 description 是简单的字符串:
@process(title='Short Process Title',
description='Longer and more detailed process description')
def run():
pass
这个 inputs 元数据是一个字典,由与进程输入名称匹配的字符串键入。字典的值是元组,其中第一个元素是输入的类型,第二个值是输入的描述。字典的键必须与进程函数本身声明的键匹配:
@process(
...
inputs={'arg1': (<arg1 type>, 'Arg1 description'),
'arg2': (<arg2 type>, 'Arg2 description')}
)
def run(arg1, arg2):
pass
或者,输入元组还可以承载第三个参数,即承载更多输入元数据的字典。当前支持以下元数据:
min:输入的最小出现次数,如果输入是可选的,则为0。
max:输入的最大出现次数,如果超过一次,进程将收到一个列表。
域:输入可以接收的值列表,这些值将在wps describeProcess输出中公布。
例如:
@process(
inputs={'geom': (Geometry, 'The geometry to buffer'),
'distance':(float, 'The buffer distance'),
'capStyle': (str, 'The style of buffer endings',
{'min': 0, 'domain' : ('round', 'flat', 'square')}),
'quadrantSegments': (int, 'Number of segments' , {'min': 0})}
最后,指定给 run 功能参数将在功能文档中显示为参数默认值:
@process(...)
def run(a, b, c='MyDefaultValue')
这个 outputs 元数据与输入字典的结构相同,只是它描述了进程的输出参数:
@process(
...
outputs={'result1': (<result1 type>, 'Result1 description'),
'result2': (<result2 type>, 'Result2 description')}
)
def run(arg1, arg2):
pass
进程必须生成并返回与 outputs 论据。对于返回单个值的进程,这是隐式确定的,但返回多个值的进程必须通过返回返回值的字典来显式:
@process(
...
outputs={'result1': (<result1 type>, 'Result1 description'),
'result2': (<result2 type>, 'Result2 description')}
)
def run(arg1, arg2):
# do something
return {
'result1': ...,
'result2': ...
}
缓冲区示例¶
In this example a simple buffer process is created. First step is to create a file named
buffer.py in the scripts/wps directory:
cd $GEOSERVER_DATA_DIR/scripts/wps
touch buffer.py
下一个 run 函数被创建并删除。函数将接受两个参数:
要缓冲的几何对象
用作缓冲值/距离的浮点值
def run(geom, distance):
pass
若要获取函数,必须首先用 process 装饰者:
from geoserver.wps import process
@process(title='Buffer', description='Buffers a geometry')
def run(geom, distance):
pass
接下来,必须描述过程输入和输出:
from geoscript.geom import Geometry
@process(
...,
inputs={ 'geom': (Geometry, 'The geometry to buffer'),
'distance': (float,'The buffer distance')},
outputs={'result': (Geometry, 'The buffered geometry')}
)
def run(geom, distance):
pass
最后编写缓冲区例程,只需调用 buffer 几何参数的方法:
@process(...)
def run(geom, distance):
return geom.buffer(distance)
在这种情况下,由于进程只返回一个参数,因此可以直接返回,而不必将其包装在字典中。
最终完成版本:
from geoserver.wps import process
from geoscript.geom import Geometry
@process(
title='Buffer',
description='Buffers a geometry',
inputs={'geom': (Geometry, 'The geometry to buffer'),
'distance':(float,'The buffer distance')},
outputs={'result': (Geometry, 'The buffered geometry')}
)
def run(geom, distance):
return geom.buffer(distance);
地理脚本py¶
如前所述 previously GeoScript为各种语言的GeoTools提供脚本api。当然,GeoServer Python扩展支持GeoScript Python。在上面的缓冲区示例中,显示了导入GeoScript类的示例。
GeoScript Python api有文档记录 here .