使用geoserver-manager發(fā)布服務(wù)

一 下載 geoserver-manager

  1. 在<repositorys>標(biāo)簽中添加倉(cāng)庫(kù)地址,用于下載包到本地倉(cāng)庫(kù)
 <repository>
      <id>GeoSolutions</id>
      <url>http://maven.geo-solutions.it/</url>
 </repository>
  1. 在<dependencies>引入依賴包
 <dependency>
    <groupId>it.geosolutions</groupId>
    <artifactId>geoserver-manager</artifactId>
    <version>1.7.0</version>
  </dependency>

二 連接postGIS發(fā)布服務(wù)

/**
 *  geoserver 工具
 */
public class GeoServerUtils {
    final String geoserverUrl = "http://localhost:8081/geoserver";
    final String geoserverUsername = "admin";
    final String geoserverPassword = "geoserver";
    GeoServerRESTPublisher publisher= null;
    GeoServerRESTReader reader= null;
    GeoServerRESTManager manager = null;
    {
        try {
            publisher= new GeoServerRESTPublisher(geoserverUrl, geoserverUsername, geoserverPassword);
            reader= new GeoServerRESTReader(geoserverUrl, geoserverUsername, geoserverPassword);
            manager = new GeoServerRESTManager(new URL(geoserverUrl), geoserverUsername, geoserverPassword);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //創(chuàng)建工作空間
    public boolean createWorkspace(String workspaceName){
        try {
            if (!reader.existsWorkspace(workspaceName)){ //判斷是否存在工作空間,如果不存在則創(chuàng)建
                publisher.createWorkspace(workspaceName);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    //創(chuàng)建樣式
    public boolean createLayerStyle(String workspaceName,String sldFilePath){
        //設(shè)置樣式
        boolean existsStyle = reader.existsStyle("devPolygon1"); //判斷該樣式是否存在,如果不存在則創(chuàng)建
        String styleName = null;
        if (!existsStyle){
            //String sldFilePath = "D:\\data222\\style\\devPolygon333.xml";
            File sldFile = new File(sldFilePath);
            String[] split = sldFile.getName().split(".xml");
            styleName = split[0];
            boolean createStyle = publisher.publishStyleInWorkspace(workspaceName,sldFile,styleName);
            if (createStyle){
                System.out.println("創(chuàng)建成功");
            }else {
                System.out.println("創(chuàng)建失敗");
            }
            return createStyle;
        }else {
            System.out.println("樣式已存在");
            return false;
        }
    }
    //創(chuàng)建數(shù)據(jù)源
    public boolean createPostGISDataStore(String workspaceName,String dataStoreName){
        try {
            GSPostGISDatastoreEncoder postgis = new GSPostGISDatastoreEncoder(dataStoreName);
            postgis.setHost("127.0.0.1");
            postgis.setPort(5432);
            postgis.setUser("postgres");
            postgis.setPassword("root0110");
            postgis.setDatabase("common"); //數(shù)據(jù)庫(kù)名稱
            postgis.setExposePrimaryKeys(true);
            boolean result = manager.getStoreManager().create(workspaceName, postgis);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 發(fā)布圖層
     * @param workspaceName 工作空間名稱
     * @param dataStoreName 數(shù)據(jù)源名稱
     * @param styleName 樣式名稱
     * @param tableName 數(shù)據(jù)源中表名稱
     * @param layerName 發(fā)布圖層名稱
     * @param layerAlias 發(fā)布圖層別名
     * @param crsCode 發(fā)布圖層坐標(biāo)系代碼
     * @return
     * @throws ErrorException
     */
    public boolean publisherLayer(String workspaceName,String dataStoreName,String styleName, String tableName,String layerName, String layerAlias, int crsCode) throws ErrorException {
        if (!reader.existsWorkspace(workspaceName)){
            throw new ErrorException(String.format("工作區(qū):%s 不存在", workspaceName));
        }
        if (!reader.existsStyle(workspaceName,styleName)){
            throw new ErrorException(String.format("在工作區(qū): %s 中, %s 服務(wù)樣式不存在"+ workspaceName,styleName));
        }
        if (!reader.existsDatastore(workspaceName,dataStoreName)){
            throw new ErrorException(String.format("在工作區(qū): %s 中, %s 數(shù)據(jù)源不存在"+ workspaceName,dataStoreName));
        }

        //圖層設(shè)置
        GSFeatureTypeEncoder gsFeatureTypeEncoder = new GSFeatureTypeEncoder();
        gsFeatureTypeEncoder.setNativeName(tableName); //表名
        gsFeatureTypeEncoder.setName(layerName); // 圖層名稱
        gsFeatureTypeEncoder.setTitle(layerAlias);// 圖層別名
        gsFeatureTypeEncoder.setSRS("EPSG:" + crsCode); //坐標(biāo)系

        GSLayerEncoder styleEncoder = new GSLayerEncoder(); //設(shè)置樣式
        styleEncoder.setDefaultStyle(workspaceName,styleName);

        boolean publishDBLayerResult = publisher.publishDBLayer(workspaceName, dataStoreName, gsFeatureTypeEncoder, styleEncoder);
        return publishDBLayerResult;
    }

}

注:補(bǔ)充樣式\color{red}{(SLD)},要服務(wù)OGC標(biāo)準(zhǔn)。

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0"
                       xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
                       xmlns="http://www.opengis.net/sld"
                       xmlns:ogc="http://www.opengis.net/ogc"
                       xmlns:xlink="http://www.w3.org/1999/xlink"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <NamedLayer>
        <Name>testStyleName</Name>
        <UserStyle>
            <Title>testStyleName</Title>
            <Abstract>testStyleName style</Abstract>
            <FeatureTypeStyle>
                <!--配置該樣式對(duì)應(yīng)的屬性-->
                <Rule>
                    <!--該樣式針對(duì)的是面-->
                    <Name>Polygon</Name>
                    <Title>Red Polygon</Title>
                    <ogc:Filter>
                        <ogc:And>
                            <ogc:PropertyIsEqualTo>
                                <ogc:Function name="dimension">
                                    <ogc:Function name="geometry"/>
                                </ogc:Function>
                                <ogc:Literal>2</ogc:Literal>
                            </ogc:PropertyIsEqualTo>
                            <ogc:PropertyIsEqualTo>
                                <!--過(guò)濾的屬性字段-->
                                <ogc:PropertyName>address</ogc:PropertyName>
                                <!--屬性字段對(duì)應(yīng)的值-->
                                <ogc:Literal>BeiJing</ogc:Literal>
                            </ogc:PropertyIsEqualTo>
                        </ogc:And>
                    </ogc:Filter>
                    <!--標(biāo)注樣式設(shè)置-->
                    <PolygonSymbolizer>
                        <Fill>
                            <CssParameter name="fill">#0000ff</CssParameter>
                            <CssParameter name="fill-opacity">0</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#0000ff</CssParameter>
                            <CssParameter name="stroke-width">1</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>
                    <TextSymbolizer>
                        <Label>
                            <!--標(biāo)注顯示的屬性字段-->
                            <ogc:PropertyName>XZQNAME</ogc:PropertyName>
                        </Label>
                        <Font>
                            <CssParameter name="font-family">Times new roman</CssParameter>
                            <CssParameter name="font-size">10</CssParameter>
                            <CssParameter name="font-style">normal</CssParameter>
                        </Font>
                        <LabelPlacement>
                            <PointPlacement>
                                <AnchorPoint>
                                    <AnchorPointX>0.5</AnchorPointX>
                                    <AnchorPointY>0.5</AnchorPointY>
                                </AnchorPoint>
                            </PointPlacement>
                        </LabelPlacement>
                        <Fill>
                            <CssParameter name="fill">#0000ff</CssParameter>
                        </Fill>
                    </TextSymbolizer>
                </Rule>
                <VendorOption name="ruleEvaluation">first</VendorOption>
            </FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容