zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    史上最容易听错的歌词
    笑话
    商贸通转入EXCEL中的账查不能自动进行合计
    XP登陆后自动注销!
    在Windows系统上如何安装虚拟网卡
    全球最佳造句奖
    一个电脑白痴和黑客的对话
    光棍与非光棍的N条区别
    『转』组合数快速算法!!!
    中国剩余定理
  • 原文地址:https://www.cnblogs.com/zhaoyanhaoBlog/p/9068114.html
Copyright © 2011-2022 走看看