geoserver源碼學習與擴展——自動發布shapefile圖層


geoserver通過工作空間Workspace-數據源DataStore-圖層Layer管理地理數據,這些信息都通過Catalog進行組織和管理,要完成自動發布只需要在Catalog中增加相應的信息即可。

 主要包括:1、添加數據源信息DataStore,使用默認工作空間;2、添加矢量要素信息FeatureTypeInfo,作為矢量數據源;3、添加圖層信息LayerInfo,可設置使用樣式,也可使用默認樣式;

 1        /**
 2        * publish shape file to layer
 3        * */
 4       private void publishShapeFile(File shpDir, String schemaName){        
 5           final CatalogBuilder catalogBuilder = new CatalogBuilder(catalog);
 6   
 7           //create FeatureSource
 8           String shapePath = shpDir.getAbsolutePath() + "/" + schemaName + ".shp";
 9           ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
10          ShapefileDataStore sds = null;
11          try {
12              URL shpUrl = new File(shapePath).toURI().toURL();
13              sds = (ShapefileDataStore)dataStoreFactory.createDataStore(shpUrl);
14              SimpleFeatureSource featureSource = sds.getFeatureSource();
15  
16              //check exist
17              DataStoreInfo dsInfo = catalog.getDataStoreByName(schemaName);
18              if(dsInfo == null){
19                  dsInfo = catalogBuilder.buildDataStore(schemaName);
20                  dsInfo.setType("Shapefile");
21                  Map<String, Serializable> connectionParams = new HashMap<String, Serializable>();
22                  connectionParams.put("charset", sds.getCharset().toString());
23                  connectionParams.put("filetype", "shapefile");
24                  connectionParams.put("create spatial index", true);
25                  connectionParams.put("memory mapped buffer", false);
26                  connectionParams.put("timezone", "PRC");
27                  connectionParams.put("enable spatial index", true);
28                  connectionParams.put("namespace", catalog.getDefaultNamespace().getURI());
29                  connectionParams.put("cache and reuse memory maps", true);
30                  connectionParams.put("fstype", "shape");
31                  connectionParams.put("url", shpUrl.toString());
32                  dsInfo.getConnectionParameters().putAll(connectionParams);
33                  catalog.save(dsInfo);
34              }
35              catalogBuilder.setStore(dsInfo);
36  
37              //check exist
38              FeatureTypeInfo ftInfo = catalog.getFeatureTypeByDataStore(dsInfo, featureSource.getName().getLocalPart());
39              if(ftInfo == null){
40                  ftInfo = catalogBuilder.buildFeatureType(featureSource);
41                  catalogBuilder.setupBounds(ftInfo, featureSource);
42                  catalog.add(ftInfo);
43              }
44  
45              //check exist
46              LayerInfo lInfo = catalog.getLayerByName(ftInfo.getName());
47              if(lInfo == null){
48                  lInfo = catalogBuilder.buildLayer(ftInfo);
49                  //set custom style “ammeter”
50                  StyleInfo styleInfo = catalog.getStyleByName("ammeter");
51                  if(styleInfo != null)
52                      lInfo.setDefaultStyle(styleInfo);
53  
54                  catalog.add(lInfo);
55              }
56  
57          } catch (Exception e) {
58              e.printStackTrace();
59          }finally{
60              sds.dispose();
61          }
62  
63      }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM