GeoServer批量发布tiff图层

数据准备

在本地电脑,准备好一张tiff格式的地图

导入依赖

在pom.xml文件中导入以下依赖

<dependency>
            <groupId>it.geosolutions</groupId>
            <artifactId>geoserver-manager</artifactId>
            <version>1.7.0</version>
</dependency>

代码示例

package com.example.demo.geoserver;

import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.encoder.datastore.GSGeoTIFFDatastoreEncoder;
import java.io.File;
import java.net.URL;
import java.util.List;

/**
 * @author yangkun
 * @createTime 2020年11月23日 10:42:00
 */
public class GeoServerDemo {

    public static String url = "http://192.168.1.190:9000/geoserver";
    public static String username = "admin";
    public static String passwd = "geoserver";
    public static String ws = "demo";
    public static String store_name = "t1";

    public static void publishLayer(String filePath){
        try{
            URL u = new URL(url);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
            GeoServerRESTPublisher publisher = manager.getPublisher();
            List<String> workspaces = manager.getReader().getWorkspaceNames();
            if (!workspaces.contains(ws)) {
                boolean createws = publisher.createWorkspace(ws);
                System.out.println("create ws : " + createws);
            } else {
                System.out.println("workspace已经存在了,ws :" + ws);
            }
            //判断数据存储(datastore)是否已经存在,不存在则创建
            RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
            if (restStore == null) {
                GSGeoTIFFDatastoreEncoder gsGeoTIFFDatastoreEncoder = new GSGeoTIFFDatastoreEncoder(store_name);
                gsGeoTIFFDatastoreEncoder.setWorkspaceName("mxleimm");
                gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + filePath));
                boolean createStore = manager.getStoreManager().create(ws, gsGeoTIFFDatastoreEncoder);
                System.out.println("create store (TIFF文件创建状态) : " + createStore);
                boolean publish = false;
                publish = manager.getPublisher().publishGeoTIFF(ws, store_name, new File(filePath));
                System.out.println("publish (TIFF文件发布状态) : " + publish);
            } else {
                System.out.println("数据存储已经存在了,store:" + store_name);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String filePath = "D:\\geoserver-2.16.4-bin\\data_dir\\data\\zs\\t1.tif";
        publishLayer(filePath);
    }

}

效果展示

打开geoserver,发现在Layer Preview中已经出现了刚发布的图层


image.png

打开图层预览,图层已可以正常加载


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容