springboot操作geoserver

前言

使用Java进行编写程序用来对geoserver进行操控


一、springboot操作geoserver

环境:idea+springboot+jdk1.8

        
        <dependency>
            <groupId>nl.pdokgroupId>
            <artifactId>geoserver-managerartifactId>
            <version>1.7.0-pdok2version>
        dependency>

二、使用步骤

1.添加style

代码如下(示例):

 public boolean addStyle(Long id, String filePath) {
        // 获取当前用户默认的geoserver配置
        GeoserverConfigDTO config = authService.getUserGeoserverConfig(id);
        String layerName = filePath.substring(filePath.lastIndexOf('/') + 1);
        String styleName = layerName.substring(0, layerName.lastIndexOf("."));
        String url = GeoServerUrlUtil.getProtocol() + config.getDomainName() + "/geoserver";

        try {
            GeoServerRESTManager manager = null;
            try {
                manager = new GeoServerRESTManager(new URL(url), config.getUsername(), config.getPassword());
                logger.info("连接geoserver服务器成功");
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("geoserver服务器连接失败");
                return false;
            }
            boolean boo = manager.getReader().existsWorkspace(config.getWorkspace());
            if (!boo) {
                boolean b = manager.getPublisher().createWorkspace(config.getWorkspace());
                if (!b) {
                    logger.info("工作区创建失败");
                    return false;
                }
            }
            GeoServerRESTStyleManager styleManager = manager.getStyleManager();
            boolean b = styleManager.existsStyle(config.getWorkspace(), styleName);
            if (!b) {
                File sldFile = new File(filePath);
                boolean b1 = styleManager.publishStyleInWorkspace(config.getWorkspace(), sldFile, styleName);
                if (!b1) {
                    logger.info("新增样式失败");
                    return false;
                }
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

2.添加shp

代码如下(示例):

public boolean addShp(Long id, String filePath) {
        // 获取当前用户默认的geoserver配置
        GeoserverConfigDTO config = authService.getUserGeoserverConfig(id);
        String layerName = filePath.substring(filePath.lastIndexOf('/') + 1);
        String shpName = layerName.substring(0, layerName.lastIndexOf("."));
        String url = GeoServerUrlUtil.getProtocol() + config.getDomainName() + "/geoserver";
        NameValuePair nameValuePair = new NameValuePair("charset", "GBK");
        NameValuePair[] nameValuePairsArr = new NameValuePair[1];   
        nameValuePairsArr[0] = nameValuePair;
        String srs = "EPSG:4326";                   
        try {
            //  1、获取geoserver连接对象
            GeoServerRESTManager manager = null;
            try {
                manager = new GeoServerRESTManager(new URL(url), config.getUsername(), config.getPassword());
                logger.info("连接geoserver服务器成功");
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("geoserver服务器连接失败");
                return false;
            }
            File shpFile = new File(filePath);

            RESTDataStoreList datastoresList = manager.getReader().getDatastores(config.getWorkspace());
            List<String> datastoreNameList = datastoresList.getNames();
            boolean storeNull = !datastoreNameList.contains(shpName);
            if (storeNull) {
                GeoServerRESTPublisher publisher = manager.getPublisher();
                publisher.publishShp(config.getWorkspace(), shpName, nameValuePairsArr, shpName, GeoServerRESTPublisher.UploadMethod.FILE, shpFile.toURI(), srs, shpName);
                String geoserverUrl = GeoServerUrlUtil.getProtocol() + config.getDomainName() + "/geoserver/" + config.getWorkspace() + "/wms/";
                String layName = config.getWorkspace() + ":" + shpName;
                insertLay(geoserverUrl, layName);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

3.添加tiff

代码如下(示例):

    public boolean addTiff(Long id, String filePath) {
        // 获取当前用户默认的geoserver配置
        GeoserverConfigDTO config = authService.getUserGeoserverConfig(id);
        String layerName = filePath.substring(filePath.lastIndexOf('/') + 1);
        String tifName = layerName.substring(0, layerName.lastIndexOf("."));
        String url = GeoServerUrlUtil.getProtocol() + config.getDomainName() + "/geoserver";
        try {
            //  1、获取geoserver连接对象
            GeoServerRESTManager manager = null;
            try {
                manager = new GeoServerRESTManager(new URL(url), config.getUsername(), config.getPassword());
                logger.info("连接geoserver服务器成功");
            } catch (Exception e) {
                e.printStackTrace();
               logger.info("geoserver服务器连接失败");
                return false;
            }
            File shpFile = new File(filePath);

            RESTDataStore restStore = manager.getReader().getDatastore(config.getWorkspace(), tifName);
            if (restStore == null) {
                GSGeoTIFFDatastoreEncoder gsGeoTIFFDatastoreEncoder = new GSGeoTIFFDatastoreEncoder(tifName);
                gsGeoTIFFDatastoreEncoder.setWorkspaceName(config.getWorkspace());
                gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + filePath));
                boolean createStore = manager.getStoreManager().create(config.getWorkspace(), gsGeoTIFFDatastoreEncoder);
                boolean publish = false;
                publish = manager.getPublisher().publishGeoTIFF(config.getWorkspace(), tifName, shpFile);
                logger.info("publish (TIFF文件发布状态) : " + publish);
                String geoserverUrl = GeoServerUrlUtil.getProtocol() + config.getDomainName() + "/geoserver/" + config.getWorkspace() + "/wms/";
                String layName = config.getWorkspace() + ":" + tifName;
                insertLay(geoserverUrl, layName);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

4.将新增的geoserver图层存入数据库

    public void insertLay(String url, String layName) {
        LayerPO layerPO = new LayerPO();
        layerPO.setLayerName(layName);
        layerPO.setLayerUrl(url);
        layerPO.setRoute(100000);
        queryLayerService.insertLayer(layerPO);
    }

总结

从数据库中获取geoserver的参数。

你可能感兴趣的:(Java,geoserver,java,spring,boot)