使用java将shape文件发布到GeoServer

627 阅读1分钟

pom.xml中使用geoserver-manager

<dependency>
    <groupId>it.geosolutions</groupId>
    <artifactId>geoserver-manager</artifactId>
    <version>1.7.0</version>
</dependency>
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.GSLayerGroupEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 发布GeoServer服务.
 */

public class RenrenApplication {

   public static void main(String[] args) {
      //记录数组节点
      int pl = 0;
      //待发布数组
      List<String> publishList = new ArrayList<>();
      //记录发布名节点
      int pn = 0;
      for (int i = 0; i < args.length; i++) {
         if (args[i].equals("--publist")) pl = i;
         if (args[i].equals("--name")) pn = i;
      }
      String labelName = args[pn + 1];
      for (int i = pl + 1; i < args.length; i++) {
         publishList.add(args[i]);
      }

      pubShape(publishList, labelName);
   }

   static GeoServerRESTManager geoServerRESTManager = null;
   static GeoServerRESTReader restReader = null;
   static GeoServerRESTPublisher restPublisher = null;
   // 工作空间
   static String workSpace = "coalmap";

   private static void pubShape(List<String> publist, String labelName) {
      // 连接geoServer
      connectGeo();

      // shp读写和发布
      assert geoServerRESTManager != null;
      restReader = geoServerRESTManager.getReader();
      restPublisher = geoServerRESTManager.getPublisher();

      // 存在相应的工作区
      if (!restReader.existsWorkspace(workSpace)) {
         restPublisher.createWorkspace(workSpace);
      }
      ArrayList<String> layerList = new ArrayList<>();
      //添加数据存储
      for (int i = 0; i < publist.size(); i++) {
         System.out.println(publist.get(i));
         String[] arrayList = publist.get(i).split("\\");
         SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
         Date curDate = new Date(System.currentTimeMillis());
         String fireName = arrayList[arrayList.length - 1].replace(".shp", "");
         String dataSetName = labelName + "-" + fireName + "-" + formatter.format(curDate);
         dataSetName = dataSetName.replace("-","");
         //添加数据存储
         pubShapeStore(dataSetName, publist.get(i));
//       //发布图层
         publishLayer(dataSetName,fireName, fireName.contains("Annotation") ? false : true);
         layerList.add(workSpace + ":" + fireName);
      }
      //发布图层组
      publishShapeGroup(labelName,layerList);
   }

   /**
    * 连接GeoServer
    * @return
    */
   private static void connectGeo() {
      // geoServer信息
      String url = "http://192.168.124.82:8099/geoserver";
      String user = "admin";
      String password = "geoserver";

      try {
         geoServerRESTManager = new GeoServerRESTManager(new URL(url), user, password);
         System.out.println("GeoServer连接成功");
      } catch (Exception e) {
         System.out.println("远程连接GeoServer失败...");
         e.printStackTrace();
      }
   }

   /**
    * 发布数据存储
    * @param dataSetName 数据存储名称
    * @param filePath shape文件路径
    */
   private static void pubShapeStore(String dataSetName, String filePath) {
      if (!restReader.existsDatastore(workSpace, dataSetName)) {
         File file = new File(filePath);
         //创建shape文件存储
         try {
            //shp文件所在的位置
            String urlDataStorePath = file.getPath();
            // 数据存储需要的文件
            String shpFilePath = String.format("file:%s", urlDataStorePath);
            URL urlShapeFile = new URL(shpFilePath);
            // 创建数据集
            GSShapefileDatastoreEncoder datastoreEncoder = new GSShapefileDatastoreEncoder(dataSetName, urlShapeFile);
            datastoreEncoder.setCharset(Charset.forName("GBK"));
            geoServerRESTManager.getStoreManager().create(workSpace, datastoreEncoder);
            System.out.println("创建数据存储:" + dataSetName + "成功");
         } catch (MalformedURLException e) {
            e.printStackTrace();
         }
      }
   }

   /**
    *
    * @param dataSetName 图层名称
    * @param isCss 样式是否使用CSS
    */
   private static void publishLayer (String dataSetName, String layerName, Boolean isCss) {
      if (!restReader.existsLayer(workSpace, layerName)) {
         try {
            GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();
            gsFeatureTypeEncoder.setTitle(layerName);
            gsFeatureTypeEncoder.setName(layerName);
            gsFeatureTypeEncoder.setSRS(GeoServerRESTPublisher.DEFAULT_CRS);

            GSLayerEncoder gsLayerEncoder = new GSLayerEncoder();
            if (isCss) {
               gsLayerEncoder.setDefaultStyle("vicMap");
            } else {
               gsLayerEncoder.setDefaultStyle("maptext");
            }
            boolean layer = restPublisher.publishDBLayer(workSpace, dataSetName, gsFeatureTypeEncoder, gsLayerEncoder);
            System.out.println("发布图层 : " + ( workSpace + ":" + layerName + (layer ?" 成功" : " 失败")));
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
   }

   /**
    * 发布图层组
    * @param layerName 图层名
    * @param layerList 图层组内容
    */
   private static void publishShapeGroup(String layerName, ArrayList<String> layerList) {
      if (!restReader.existsLayerGroup(workSpace, layerName)) {
         GSLayerGroupEncoder gsLayerGroupEncoder=new GSLayerGroupEncoder();
         for (int i = 0; i < layerList.size(); i++) {
            gsLayerGroupEncoder.addLayer(layerList.get(i));
         }
         boolean layerGroup = restPublisher.createLayerGroup(workSpace,layerName,gsLayerGroupEncoder);
         System.out.println("发布图层组 : " + workSpace + ":" + layerName + (layerGroup ? " 成功" : " 失败"));
      }
   }
}

使用命令行调用jar包

java -jar publishShape-1.0.jar --name hbc --publist data\\Annotation.shp data\\Polyline.shp

--name后为发布的图层组名称 --publist后为待发布的shape文件路径列表,使用空格隔开