geoserver源码学习与扩展——自动发布shapefile图层

geoserver通过工作空间Workspace-数据源DataStore-图层Layer管理地理数据,这些信息都通过Catalog进行组织和管理,要完成自动发布只需要在Catalog中增加相应的信息即可。

 主要包括:1、添加数据源信息DataStore,使用默认工作空间;2、添加矢量要素信息FeatureTypeInfo,作为矢量数据源;3、添加图层信息LayerInfo,可设置使用样式,也可使用默认样式;

获取catalog的方法:this.catalog=(Catalog) GeoServerExtensions.bean("catalog");

/**
       * publish shape file to layer
       * */
      private void publishShapeFile(File shpDir, String schemaName){        
          final CatalogBuilder catalogBuilder = new CatalogBuilder(catalog);
  
          //create FeatureSource
          String shapePath = shpDir.getAbsolutePath() + "/" + schemaName + ".shp";
          ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
         ShapefileDataStore sds = null;
         try {
             URL shpUrl = new File(shapePath).toURI().toURL();
             sds = (ShapefileDataStore)dataStoreFactory.createDataStore(shpUrl);
             SimpleFeatureSource featureSource = sds.getFeatureSource();
 
             //check exist
             DataStoreInfo dsInfo = catalog.getDataStoreByName(schemaName);
             if(dsInfo == null){
                 dsInfo = catalogBuilder.buildDataStore(schemaName);
                 dsInfo.setType("Shapefile");
                 Map<String, Serializable> connectionParams = new HashMap<String, Serializable>();
                 connectionParams.put("charset", sds.getCharset().toString());
                 connectionParams.put("filetype", "shapefile");
                 connectionParams.put("create spatial index", true);
                 connectionParams.put("memory mapped buffer", false);
                 connectionParams.put("timezone", "PRC");
                 connectionParams.put("enable spatial index", true);
                 connectionParams.put("namespace", catalog.getDefaultNamespace().getURI());
                 connectionParams.put("cache and reuse memory maps", true);
                 connectionParams.put("fstype", "shape");
                 connectionParams.put("url", shpUrl.toString());
                 dsInfo.getConnectionParameters().putAll(connectionParams);
                 catalog.save(dsInfo);
             }
             catalogBuilder.setStore(dsInfo);
 
             //check exist
             FeatureTypeInfo ftInfo = catalog.getFeatureTypeByDataStore(dsInfo, featureSource.getName().getLocalPart());
             if(ftInfo == null){
                 ftInfo = catalogBuilder.buildFeatureType(featureSource);
                 catalogBuilder.setupBounds(ftInfo, featureSource);
                 catalog.add(ftInfo);
             }
 
             //check exist
             LayerInfo lInfo = catalog.getLayerByName(ftInfo.getName());
             if(lInfo == null){
                 lInfo = catalogBuilder.buildLayer(ftInfo);
                 //set custom style “ammeter”
                 StyleInfo styleInfo = catalog.getStyleByName("ammeter");
                 if(styleInfo != null)
                     lInfo.setDefaultStyle(styleInfo);
 
                 catalog.add(lInfo);
             }
 
         } catch (Exception e) {
             e.printStackTrace();
         }finally{
             sds.dispose();
         }
 
     }

来自  http://www.cnblogs.com/HandyLi/p/8616148.html

 

 

posted @ 2018-05-21 17:00  zhaoyanhao  阅读(355)  评论(0编辑  收藏  举报