GeoServer批量發(fā)布tiff圖層

數(shù)據(jù)準(zhǔn)備

在本地電腦,準(zhǔn)備好一張tiff格式的地圖

導(dǎo)入依賴

在pom.xml文件中導(dǎo)入以下依賴

<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已經(jīng)存在了,ws :" + ws);
            }
            //判斷數(shù)據(jù)存儲(chǔ)(datastore)是否已經(jīng)存在,不存在則創(chuàng)建
            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文件創(chuàng)建狀態(tài)) : " + createStore);
                boolean publish = false;
                publish = manager.getPublisher().publishGeoTIFF(ws, store_name, new File(filePath));
                System.out.println("publish (TIFF文件發(fā)布狀態(tài)) : " + publish);
            } else {
                System.out.println("數(shù)據(jù)存儲(chǔ)已經(jīng)存在了,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,發(fā)現(xiàn)在Layer Preview中已經(jīng)出現(xiàn)了剛發(fā)布的圖層


image.png

打開圖層預(yù)覽,圖層已可以正常加載


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容