美文网首页开源GeoServer
java发布shp数据(带style样式)

java发布shp数据(带style样式)

作者: WebGiser | 来源:发表于2019-06-09 18:36 被阅读3次

环境:IDEA java geoserver。
参考:github项目:geoserver-manager
功能:使用java语言通过Geoserver软件发布shp(zip格式)地图服务。

1、新建maven项目

image.png

2、pom.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>TestGeoserver_success</groupId>
    <artifactId>TestGeoserver_success</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>it.geosolutions</groupId>
            <artifactId>geoserver-manager</artifactId>
            <version>1.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.11</version>
        </dependency>

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>

3、PublishShp2.java代码

package geoserver;

import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.decoder.RESTDataStoreList;
import org.apache.commons.httpclient.NameValuePair;
import java.io.File;
import java.net.URI;
import java.util.List;

//发布带style样式的shp地图服务
public class PublishShp2 {
    public static void main(String[] args) throws Exception {
        final String geoserverUrl = "http://localhost:8080/geoserver";
        final String geoserverUsername = "admin";
        final String geoserverPassword = "geoserver";

        GeoServerRESTPublisher geoServerRESTPublisher = new GeoServerRESTPublisher(geoserverUrl,geoserverUsername,geoserverPassword);
        GeoServerRESTReader geoServerRESTReader = new GeoServerRESTReader(geoserverUrl,geoserverUsername,geoserverPassword);

        //发布zip格式的shp数据(直接把shp等相关数据压缩成zip格式,中间不要加文件夹)
        String workspace = "bbb";                   //工作空间
        String datastoreName = "resttestshp2";      //必须和zip压缩包的名称一致
        String layerName = "cities2";               //必须和压缩包里面shp的名称一致
        File zipFile = new File("G:\\test\\resttestshp2.zip");      //shp数据zip压缩包
        URI uri = zipFile.toURI();
        NameValuePair nameValuePair = new NameValuePair("charset","UTF-8");
        NameValuePair[] nameValuePairsArr = new NameValuePair[1];   //字符串等相关配置
        nameValuePairsArr[0] = nameValuePair;
        String srs = "EPSG:4326";                   //shp数据坐标系
        String style = "restteststyle";             //shp数据样式

        //workspace是否存在,不存在则新建。
        List<String> workspacesList = geoServerRESTReader.getWorkspaceNames();
        boolean wsNull = !workspacesList.contains(workspace);
        if(wsNull){
            geoServerRESTPublisher.createWorkspace(workspace);
        }

        //datastore是否存在,不存在则新建并发布数据。
        RESTDataStoreList datastoresList = geoServerRESTReader.getDatastores(workspace);
        List<String> datastoreNameList = datastoresList.getNames();
        boolean storeNull = !datastoreNameList.contains(datastoreName);
        if(storeNull){
            boolean result = geoServerRESTPublisher.publishShp(workspace,datastoreName, nameValuePairsArr,
                    layerName, GeoServerRESTPublisher.UploadMethod.FILE,uri,srs,style);
            System.out.println("数据发布是否成功:"+result);
        }else{
            System.out.println("数据已经发布过了,不能重复发布!");
        }
    }
}
image.png

相关文章