介绍

geoserver-manager使用 Java 库通过 REST 与 GeoServer 通信。

只需要实例化GeoServerRESTReader即可从正在运行的GeoServer实例中读取数据

public GeoServerRESTReader(String restUrl, String username, String password);

GeoServerRESTPublisher 修改目录(发布或移除styles, featuretypes or coverages)。

public GeoServerRESTPublisher(String restURL, String username, String pw);

使用方法

初始化reader and publisher

        String RESTURL  = "http://localhost:8080/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        
        GeoServerRESTReader reader = new GeoServerRESTReader(RESTURL, RESTUSER, RESTPW);
        GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW);


创建工作区

        boolean created = publisher.createWorkspace("myWorkspace")

创建样式

        File sldFile = ...;
        boolean published = publisher.publishStyle(sldFile); 
        File sldFile = ...;
        boolean published = publisher.publishStyle(sldFile, "myStyle");

只读方法

  • public boolean existsStyle(String styleName)
    检查全局样式是否存在

  • public RESTStyle getStyle(String name)
    获取全局样式信息

  • public RESTStyleList getStyles()
    返回可迭代的样式列表

  • public String getSLD(String styleName)
    获取 SLD

这些方法具有对应的工作区方法:

  • public boolean existsStyle(String workspace, String styleName)

  • public RESTStyle getStyle(String workspace, String name)

  • public RESTStyleList getStyles(String workspace)

  • public String getSLD(String workspace, String styleName)

Insert, update, delete

可以使用不同的方法发布样式。
可以将 SLD 作为 String 或使用 File 提供;此外,可以为样式指定一个名称:如果未提供,它将从 SLD 中提取。 以下是方法列表:

  • public boolean publishStyle(String sldBody)

  • public boolean publishStyle(final String sldBody, final String name)

  • public boolean publishStyle(File sldFile)

  • public boolean publishStyle(File sldFile, String name)

对于更新,可以使用 String 或 File 的相同选项:

  • public boolean updateStyle(final String sldBody, final String name)

  • public boolean updateStyle(final File sldFile, final String name)

删除样式时,可以使用任一方法

  • public boolean removeStyle(String styleName)
    删除文件系统上的样式及其相关 SLD

  • public boolean removeStyle(String styleName, purge)
    在文件系统上删除样式及其相关的 SLD

所有这些方法都适用于全局样式。
对于它们中的每一个,都可以找到相应的作方法 在工作区中使用样式:

  • public boolean publishStyleInWorkspace(String workspace, String sldBody)

  • public boolean publishStyleInWorkspace(String workspace, String sldBody, String name)

  • public boolean publishStyleInWorkspace(String workspace, File sldFile)

  • public boolean publishStyleInWorkspace(String workspace, File sldFile, String name)

  • public boolean updateStyleInWorkspace(String workspace, String sldBody, String name)

  • public boolean updateStyleInWorkspace(String workspace, File sldFile, String name)

  • public boolean removeStyleInWorkspace(String workspace, String styleName, boolean purge)

  • public boolean removeStyleInWorkspace(String workspace, String styleName)

例子

创建样式

将从本地文件系统加载的 SLD 发送到 GeoServer。 样式名称将从提供的 SLD 中读取。

        File sldFile = ...;
        boolean published = publisher.publishStyle(sldFile); // Will take the name from SLD contents

如果要覆盖 SLD 中定义的名称,可以显式指定名称。

        File sldFile = ...;
        boolean published = publisher.publishStyle(sldFile, "myStyle");

查找样式

如果只有样式的名称,但不知道它是否在工作区内,则需要 搜索,因为 REST作仅允许在指定其工作区时选取样式。

import it.geosolutions.geoserver.rest.Util;
...
GeoServerRESTReader reader = ...
List<RESTStyle> styles = Util.searchStyles(reader, "myStyle");
for(RESTStyle style : styles) {
   String stylename = style.getName(); // it will be "myStyle" 
   String workspace = style.workspace();
   if( workspace == null) {
      // style is global
   } else {
      // style belongs to workspace
   }
}

检查资源是否存在

        boolean exists = reader.existsDatastore("myWorkspace", "myStore");

如果数据存储存在,则结果将为 true,否则将返回 false。相同的行为可以应用于其他资源:

Coverage stores

        boolean exists = reader.existsCoveragestore("myWorkspace", "myStore");

Coverages

        boolean exists = reader.existsCoverage("myWorkspace", "myStore", "myCoverage");

Feature Typse

        boolean exists = reader.existsFeatureType("myWorkspace", "myStore", "myFeatureType");

Granules (注意:仅对 ImageMosaic 等 StructuredCoverage 有用!!)

        boolean exists = reader.existsGranule("myWorkspace", "myStore", "myCoverage", "myGranuleId");

Layers

        boolean exists = reader.existsLayer("myWorkspace", "myLayer");

LayerGroups

        boolean exists = reader.existsLayerGroup("myWorkspace", "myLayerGroup");

Namespaces

        boolean exists = reader.existsNamespace("myNamespace");

Styles

        boolean exists = reader.existsStyle("myStyle");

Workspaces

        boolean exists = reader.existsWorkspace("myWorkspace");

从 Shapefile 创建图层

        File zipFile = ...;
        boolean published = publisher.publishShp("myWorkspace", "myStore", "cities", zipFile, "EPSG:4326", "default_point");

请注意,图层名称 (“cities”) 应与 .zip 文件中的 shapefile 相同。 在我们的例子中,我们在 zip 文件中有 “”。cities.shp

如果要从配置中完全删除此层,则必须删除该层和数据存储:

        boolean ftRemoved = publisher.unpublishFeatureType("myWorkspace", "myStore", "cities");
        // remove  datastore
        boolean dsRemoved = publisher.removeDatastore("myWorkspace", "myStore");

从tif创建图层

public boolean publishGeoTIFF(String workspace, String storeName, String coverageName, File geotiff, String srs, GSResourceEncoder.ProjectionPolicy policy, String defaultStyle, double[] bbox)
public boolean publishExternalGeoTIFF(String workspace, String storeName, File geotiff, String coverageName, String srs, GSResourceEncoder.ProjectionPolicy policy, String defaultStyle)

从数据库表创建层

        boolean ok = publisher.publishDBLayer("myWorkspace", "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon");
  • ''pg_kids'' 是已在 GeoServer 中配置的 PostGIS 数据存储

  • ''easia_gaul_0_aggr'' 是尚未在 GeoServer 中发布的表

geoserver 原生rest api

api 文档地址REST — GeoServer 2.27.x User Manual