脚本挂钩

此页描述所有可用的脚本挂钩。此页上列出的每个钩子都由所有支持的语言扩展实现。但是,根据语言的不同,用于编写脚本的接口和API可能有所不同。继续阅读了解更多详细信息。

应用

“app”钩子提供了一种方法来贡献打算通过http运行的脚本。应用程序对应于 scripts/apps 目录。例如::

GEOSERVER_DATA_DIR/
  ...
  scripts/
    apps/
      hello/

应用程序目录必须包含 main 包含应用程序“入口点”的文件。每次通过http请求调用应用程序时,都会执行此主文件。

主文件的包含因语言而异。所有语言的默认值只是主文件包含一个名为“run”的函数,它接受两个参数,即http请求和响应。例如,在Beanshell中:

import org.restlet.data.*;

run(request,response) {
  response.setEntity("Hello World!", MediaType.TEXT_PLAIN);
}

正如上面所解释的,这个api可以根据语言的不同而有所不同。例如,在Python中,我们定义了 WSGI 为Python web开发提供标准接口的规范。与上述脚本等效的Python脚本是:

def app(environ, start_response):
  start_response('200 OK', [('Content-Type', 'text/plain')])
  return ['Hello World!']

对于JavaScript应用程序钩子,脚本需要导出 app 符合的函数 JSGI 规范(v0.3)。javascript中的等效“hello world”应用程序如下所示(在 /scripts/apps/hello/main.js ):

exports.app = function(request) {
  return {
    status: 200,
    headers: {"Content-Type": "text/plain"},
    body: ["Hello World"]
  }
};

应用程序可在路径上访问http /script/apps/{{app}} 在哪里? {{app}} 是应用程序的名称。例如,假设本地地理服务器,应用程序的url为:

http://localhost:8080/geoserver/script/apps/hello

警告

由于存在安全风险,如果未更改默认管理员密码,则无法访问路径。

Web处理服务

wps钩子提供了一种方法,可以提供一种方法来贡献可作为wps进程运行的脚本。使用标准WPS协议调用该进程,与现有的已知进程相同。

所有进程都位于 scripts/wps 目录。每个进程都位于为该进程命名的文件中。例如::

GEOSERVER_DATA_DIR/
  ...
  scripts/
    wps/
      buffer.bsh

进程将使用扩展名作为命名空间前缀,文件名作为进程名,例如,上面的进程将显示为 bsh:buffer . 也可以将脚本放在 script/wps ,在这种情况下,目录名将用作进程命名空间,例如:

GEOSERVER_DATA_DIR/
  ...
  scripts/
    wps/
      foo/
        buffer.bsh

将进程公开为 foo:buffer .

流程脚本必须定义两件事:

  1. 流程元数据:标题、描述、输入和输出

  2. 过程例程本身

语言的默认设置是将元数据定义为脚本中的全局变量,将流程例程定义为名为“run”的函数。例如,在groovy中:

import org.locationtech.jts.geom.Geometry

title = 'Buffer'
description = 'Buffers a geometry'

inputs = [
  geom: [name: 'geom', title: 'The geometry to buffer', type: Geometry.class],
  distance: [name: 'distance', title: 'The buffer distance', type: Double.class]
]

outputs = [
  result: [name: 'result', title: 'The buffered geometry',  type: Geometry.class]
]

def run(input) {
  return [result: input.geom.buffer(input.distance)]
}

在python中,api略有不同,并使用了python修饰符:

from geoserver.wps import process
from org.locationtech.jts.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);

在JavaScript中,脚本导出 process 对象(请参见 GeoScript JS API docs 以作为WPS过程公开。下面是一个简单缓冲进程的示例(保存在 scripts/wps/buffer.js ):

var Process = require("geoscript/process").Process;

exports.process = new Process({
  title: "JavaScript Buffer Process",
  description: "Process that buffers a geometry.",
  inputs: {
    geom: {
      type: "Geometry",
      title: "Input Geometry",
      description: "The target geometry."
    },
    distance: {
      type: "Double",
      title: "Buffer Distance",
      description: "The distance by which to buffer the geometry."
    }
  },
  outputs: {
    result: {
      type: "Geometry",
      title: "Result",
      description: "The buffered geometry."
    }
  },
  run: function(inputs) {
    return {result: inputs.geom.buffer(inputs.distance)};
  }
});

一旦实现,就可以使用标准的WPS协议调用流程。例如,假设一个本地地理服务器,执行该进程的URL为:

http://localhost:8080/geoserver/wps
  ?service=WPS
  &version=1.0.0
  &request=Execute
  &identifier=XX:buffer
  &datainputs=geom=POINT(0 0)@mimetype=application/wkt;distance=10

(取代基 XX:buffer 脚本名后面跟着扩展名。例如。 py:buffer 对于Python或 js:buffer 对于JavaScript。)

过滤器功能

Filter函数钩子提供了一种创建新Filter函数的方法。例如,可以在WFS/WMS筛选或SLD表达式中使用这些函数,有关筛选函数的详细信息,请参见 过滤器功能 . GeoServer已经提供了许多内置函数,有关完整的列表,请参见 过滤函数引用 .

所有创建的函数都位于 scripts/function 目录。对于创建新函数,请使用 编写Web用户界面脚本 或直接将函数文件放入 scripts/function 例如,创建名为 camelcase 使用python语言创建文件 scripts/function/camelcase.py .

函数文件的包含因语言而异。所有语言的默认值只是函数文件包含一个名为“run”的函数。例如,在python中:

def run(value, args):
  return ''.join(x for x in args[0].title() if not x.isspace())

过滤器函数名等于函数文件名,例如,如果 scripts/function/camelcase.py 文件,然后可以在SLD中使用,如下所示:

...
<TextSymbolizer>
       <Label>
          <ogc:Function name="camelcase">
            <ogc:PropertyName>STATE_NAME</ogc:PropertyName>
          </ogc:Function>
       </Label>
  ...
</TextSymbolizer>
...

WFS事务

WFS事务挂钩提供了一种可以拦截WFS事务的方法。例如,它可以用于添加验证或基于其他属性填充某些属性。

所有创建的WFS事务挂钩都位于 scripts/wfs/tx 目录。对于创建新函数,请使用 编写Web用户界面脚本 或直接将文件放入 scripts/wfs/tx 目录。文件名在WFS事务挂钩中并不重要。

要拦截事务,应该声明一个特定于事务阶段的名称的方法,例如,在更新使用之前操作数据 preUpdate . python中可用的方法有:

from geoserver.wfs import tx

def before(req, context):
  context['before'] = True

def preInsert(inserted, req, context):
  context['preInsert'] = True

def postInsert(inserted, req, context):
  context['postInsert'] = True

def preUpdate(updated, props, req, context):
  context['preUpdate'] = True

def postUpdate(updated, props, req, context):
  context['postUpdate'] = True

def preDelete(deleted, req, context):
  context['preDelete'] = True

def postDelete(deleted, req, context):
  context['postDelete'] = True

def preCommit(req, context):
  context['preCommit'] = True

def postCommit(req, res, context):
  context['postCommit'] = True

def abort(req, res, context):
  context['abort'] = True

例如,要禁止在python中删除功能,请创建脚本:

from org.geoserver.wfs import WFSException

def preDelete(deleted, req, context):
  raise WFSException("It is not allowed to delete Features in this layer!")