52°North-WPS-学习-发布处理服务-01
使用 52°North WPS-Extension-Skeleton开发自定义处理过程
WPS-Extension-Skeleton 项目的 GitHub
使用Maven
项目POM文件
|
编译源码
可选, 可以选择下载已经打包好的 war 包: https://github.com/52North/WPS/releases
开始之前,需要配置好 WPS source. 按此 说明 进行操作,并参考 https://github.com/52North/WPS。
即,安装配置好 git 和 maven之后,获取 WPS源码:git clone https://github.com/52North/WPS.git
然后导入 Eclipse 或 IDEA,等待maven 下载相关依赖。
然后 mvn clean install -P with-geotools -DSkipTests
其中,-DSkipTests
为跳过测试,可以去掉。
并且,WPS的pom中,若部分插件如果报错,可以注释掉
确保项目pom文件中WPS模块的版本与需要使用的WPS版本相匹配
扩展WPS-扩展抽象自描述算法
此部分内容说明如何在52°North WPS中使用一个java文件开发和部署一个自定义处理过程。此过程将会生成一个 ProcessDescription 文件。
注意,支持所选定的DataBinding 的所有Parsers/Generators的格式将会被添加到 ProcessDescription 文件中。如果你需要控制你的算法所支持的格式类型,你可能需要选择使用 AbstractAlgorithm,并参照此教程. AbstractSelfDescribingAlgorithm 对ProcessDescription的某些特定元素有所限制。例如,你不能指定处理(process)的标题和摘要。如果需要更多的控制,可以 继承 AbstractAnnotatedAlgorithm.
开发
完整代码 https://wiki.52north.org/pub/Geoprocessing/ExtendAbstractSelfDescribingAlgorithm/ConvexHullDemo.java
创建自定义处理文件
- 新建 package: 如
org.n52.wps.demo
- 新建Class
ConvexHullDemo
- 使新类继承
AbstractSelfDescribingAlgorithm
实现处理
继承 AbstractSelfDescribingAlgorithm
之后,需要实现5个方法
public List getInputIdentifiers()
此方法给出所有输入数据的标识符列表。因为我们实现的是ConvexHull 算法,因此只需要 几何要素类型的数据。定义标识符为“FEATURES”
|
public List getOutputIdentifiers()
此方法给出所有产生的输出数据的标识符列表。ConvexHull 算法生成多边形,因此命名数据标识符为“polygons”
|
public Class getInputDataType(String identifier)
此方法列出指定输入数据标识符的所有输入数据类型。此例中,输入数据为FEATURES,输出类必须是特定的绑定 binding。
由于我们想使用 JTS 中支持Geotools 特性的算法, 因而使用 GTVectorDataBinding 并返回:
|
public Class getOutputDataType(String identifier)
此方法列出给定输出数据标识符的所有输出数据类型。在本例中,输出数据为polygons,此外亦知 JTS输出GeoTools要素,因此
|
public Map<String, IData> run(Map<String, List<IData>> inputData)
此方法处理业务逻辑。
首先检查所有输入数据是否已准备:
if (inputData == null || !inputData.containsKey("FEATURES")) {throw new RuntimeException("Error while allocating input parameters");}List<IData> dataList = inputData.get("FEATURES");if (dataList == null || dataList.size() != 1) {throw new RuntimeException("Error while allocating input parameters");}然后获取Geotools 要素集合:
IData firstInputData = dataList.get(0);FeatureCollection featureCollection = ((GTVectorDataBinding) firstInputData).getPayload();迭代所有要素以获取所有的坐标信息,存储在coordinateList列表中
FeatureIterator iter = featureCollection.features();List<Coordinate> coordinateList = new ArrayList<Coordinate>();int counter = 0;while (iter.hasNext()) {SimpleFeature feature = (SimpleFeature) iter.next();if (feature.getDefaultGeometry() == null) {throw new NullPointerException("defaultGeometry is null in feature id: "+ feature.getID());}Geometry geom = (Geometry) feature.getDefaultGeometry();Coordinate[] coordinateArray = geom.getCoordinates();for(Coordinate coordinate : coordinateArray){coordinateList.add(coordinate);}}iter.close();下一步,将coordinateList转为数组,以作为JTS中函数的输入
Coordinate[] coordinateArray = new Coordinate[coordinateList.size()];for(int i = 0; i<coordinateList.size(); i++){coordinateArray[i] = coordinateList.get(i);}com.vividsolutions.jts.algorithm.ConvexHull convexHull = new com.vividsolutions.jts.algorithm.ConvexHull(coordinateArray, new GeometryFactory());Geometry geometry = convexHull.getConvexHull();同时使用生成的geometry新建Geotools要素
String uuid = UUID.randomUUID().toString();SimpleFeatureType featureType = GTHelper.createFeatureType(geometry, uuid, featureCollection.getSchema().getCoordinateReferenceSystem());GTHelper.createGML3SchemaForFeatureType(featureType);Feature feature = GTHelper.createFeature("0", geometry, featureType);然后新建要素集合,并将上面的结果添加到里面
SimpleFeatureCollection fOut = DefaultFeatureCollections.newCollection();fOut.add((SimpleFeature) feature);最后的步骤是创建一个标准的输出hashmap,以输出标识符为key,以一个IData对象(此例中为GTVectordataBinding)为value。
HashMap<String, IData> result = new HashMap<String, IData>();result.put("polygons", new GTVectorDataBinding(fOut));return result;
发布处理服务
1. 使用管理控制台发布处理服务
原文中包含图片说明
https://wiki.52north.org/Geoprocessing/DeployProcessViaAdminConsole
打开管理控制台
- 确认servlet容器(如tomcat)已经运行
- 浏览器中打开http://localhost:8080/wps/
- 点击52°North WPS Web Admin Console 或 直接 http://localhost:8080/wps/webAdmin/index.jsp
- 以用户名 wps 和密码 wps 登录
上传处理文件
单击Upload Process
在弹出的对话框中,在第一个文本框中输入开发的java处理程序的完整限定名, e.g.:
org.n52.wps.demo.AbstractAlgorithmExample
根据说明选择实现类 java 文件
若实现类继承自 AbstractAlgorithm,那么还需要指定ProcessDescription 文件的路径
点击 Send files.
源码文件将会在服务后台编译,并成为一个新的WPS服务.
特别注意:由于此示例中包含了Geotools的内容,因此 一定要额外下载官方提供的 geotools-package!例如 WPS 3.6.2 GeoTools package ,并将解压后得到的
WEB-INF
下的lib
和web.xml
覆盖掉 war 包中的 相应内容!不然的话,不管是上传源码进行发布,或是采用jar包形式发布,都会导致编译失败、部署失败!Could not load algorithm com.vtech.wps.ConvexHullDemo. ProcessDescription Not Valid.
同理,如果使用了其他的 jar,也应该加入到
WEB-INF
下的lib
中!
激活处理服务
切换到 Algorithm Repositories 选项卡
滚动到 LocalAlgorithmReporitory
点击算法仓库中最后一个算法下面的 加号
添加 key 为Algorithm ,值为处理类的全限定名,如
org.n52.wps.demo.AbstractAlgorithmExample
点击save
点击Save and Activate Configuration
新建的处理服务将会出现在http://localhost:8080/wps/WebProcessingService?Request=GetCapabilities&Service=WPS 列表中,例如
<wps:Process wps:processVersion="1.0.0"><ows:Identifier>com.vtech.wps.ConvexHullDemo</ows:Identifier><ows:Title>com.vtech.wps.ConvexHullDemo</ows:Title></wps:Process>
2. 使用打包的jar文件发布一个或多个服务
https://wiki.52north.org/Geoprocessing/DeployProcessesViaJarFile
将jar文件添加到WPS库
定位到 %PATH_TO_WPS_WEBAPP%/WEB-INF/lib
,然后将jar文件拷贝进去
在WPS中激活处理服务
若你将处理服务的实现类的全限定名添加到了org.n52.wps.server.IAlgorithm
文件中(/META-INF/services/org.n52.wps.server.IAlgorithm
,没有则新建一个), 那么不需要额外的操作,处理服务会自动添加到WPS中。
若需要使处理服务失效,需要disable ServiceLoaderAlgorithmRepository
或者直接从lib
中移除对应的jar文件。
若没有使用ServiceLoaderAlgorithmRepository,那么你需要手动添加处理服务到WPS配置:
- 登录到管理控制台http://localhost:8080/wps/webAdmin/index.jsp
- 按照上面的激活处理服务操作
执行发布的服务
- 使用内置的 XML 客户端
- 使用客户端 API
- 使用 ArcMap 客户端