通过Java代码自动发布Geoserver的地图服务WMS

本文介绍了如何使用Java代码自动化发布Geoserver的地图服务WMS。通过引入POM依赖、编写发布工具类和解压文件工具类,详细阐述了发布过程,并列出可能遇到的坐标系、图集报错和过滤属性等关键问题及其解决方案。
摘要由CSDN通过智能技术生成

GeoServer 顾名思义。是一个Server,它是开源的,允许用户查看和编辑地理数据的服务器,它可以比较容易的在用户之间迅速共享空间地理信息。利用Geoserver可以把数据作为maps/images来发布(利用WMS来实现)也可以直接发布实际的数据(利用WFS来实现),它同时也提供了修改,删除和新增的功能(利用WFS-T)。

为什么要使用GeoServer?相比较同类产品ArcServer不是一个开源GIS服务器,所有想在Web地图应用开发中使用ArcServer就需要付费,而且价格比较高(ref:GeoServer与ArcServer对比)。本文将完成用ArcGIS切片好的数据,通过代码自动发布Geoserver 中。如果不清楚手动发布流程和实现的效果,可以看我的另外一篇文章:GeoServer手动发布本地Shapefile地图

一、引入pom依赖

指定远程仓库

<repository>
      <id>GeoSolutions</id>
      <url>http://maven.geo-solutions.it/</url>
   </repository>

加入dependency 

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

二、编写发布工具类

常量类Constants

public class Constant {

    public static final int BYTE_BUFFER = 1024;

    public static final int BUFFER_MULTIPLE = 10;

    /**
     * 县图集类型
     */
    public class AtlasType {
        //点位图
        public static final String ATLAS_TYPE_1 = "1";
        //单元图
        public static final String ATLAS_TYPE_2 = "2";
        //土壤类型图
        public static final String ATLAS_TYPE_3 = "3";
        //评价结果表
        public static final String ATLAS_TYPE_4 = "4";
    }

    public class FileType{
        public static final int FILE_IMG = 1;
        public static final int FILE_ZIP = 2;
        public static final int FILE_VEDIO= 3;
        public static final int FILE_APK = 4;
        public static final int FIVE_OFFICE = 5;
        public static final String FILE_IMG_DIR= "/img/";
        public static final String FILE_ZIP_DIR= "/zip/";
        public static final String FILE_VEDIO_DIR= "/video/";
        public static final String FILE_APK_DIR= "/apk/";
        public static final String FIVE_OFFICE_DIR= "/office/";
    }


    public static String parseAtlasTypeToName(String type){
        String typeName="";
        switch (type){
            case AtlasType.ATLAS_TYPE_1: typeName="bitMap"; break;
            case AtlasType.ATLAS_TYPE_2: typeName="unitMap"; break;
            case AtlasType.ATLAS_TYPE_3: typeName="typeMap"; break;
            default:break;
        }
        return  typeName;
    }

    public static class FilePostFix{
        public static final String ZIP_FILE =".zip";

        public static final String [] IMAGES ={"jpg", "jpeg", "JPG", "JPEG", "gif", "GIF", "bmp", "BMP"};
        public static final String [] ZIP ={"ZIP","zip","rar","RAR"};
        public static final String [] VIDEO ={"mp4","MP4","mpg","mpe","mpa","m15","m1v", "mp2","rmvb"};
        public static final String [] APK ={"apk","exe"};
        public static final String [] OFFICE ={"xls","xlsx","docx","doc","ppt","pptx"};

    }

    public class Atlas {
        public static final int ATLAS_FIELD_STATUS_HIDE = 0;
        public static final int ATLAS_FIELD_STATUS_SHOW = 1;
    }

}

发布工具类

import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import org.apache.commons.httpclient.NameValuePair;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;

public class GeoServerUtil {
    public static final String RESTURL;

    public static final String RESTUSER;

    public static final String RESTPW;

    public static final String GS_VERSION;

    public static java.net.URL URL;

    public static GeoServerRESTManager manager;

    public static GeoServerRESTReader reader;

    public static GeoServerRESTPublisher publisher;

    private static ResourceBundle bundle = ResourceBundle.getBundle("constant");

   //初始化用户名密码赋值,发布图集时会进行身份认证
    static {
        RESTURL = getenv("gsmgr_resturl", "http://localhost:18080/geoserver/");
        RESTUSER = getenv("gsmgr_restuser","admin");
        RESTPW = getenv("gsmgr_restpw", "geoserver");
        GS_VERSION = getenv("gsmgr_version", "2.11.2");
        try {
            URL = new URL(RESTURL);
            manager = new GeoServerRESTManager(URL, RESTUSER, RESTPW);
            reader = manager.getReader();
            publisher = manager.getPublisher();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

   //获取环境信息
    private static String getenv(String envName, String envDefault) {
        String env = System.getenv(envName);
        String prop = System.getProperty(envName, env);
        return prop != null ? prop : envDefault;
    }

    public  static boolean  publishShpAndReloadStore(String workspace,String zipFilePath,String storeName,String layerName,String styleType,String coordinateSystem) throws  Exception{
        //坐标系,判断是否为空
        if(ComUtil.isEmpty(coordinateSystem)){
            coordinateSystem=GeoServerRESTPublisher.DEFAULT_CRS;
        }
        //存在相应的工作区
        if(!reader.existsWorkspace(workspace)){
            publisher.createWorkspace(workspace);
        }
        boolean published;
        if(Constant.AtlasStyleType.ATLAS_STYLE_TYPE_5.equals(styleType)){
             published = publisher.publishShp(workspace, storeName, layerName, new File(zipFilePath),coordinateSystem,
                                  new NameValuePair[]{new NameValuePair("charset", "GBK")});
        }else{
            //读取style文件
            String styleFile = bundle.getString("geoServer-dir")+"/"+styleType+".sld";
            File file  = new File(styleFile);
     
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>