java发布shp数据

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

1、新建maven项目

image.png

2、pom.xml代码



    4.0.0

    TestGeoserver_success
    TestGeoserver_success
    1.0-SNAPSHOT

    
        
            it.geosolutions
            geoserver-manager
            1.7.0
        

        
            org.slf4j
            slf4j-log4j12
            1.5.11
        

        
            commons-httpclient
            commons-httpclient
            3.1
        

        
            commons-io
            commons-io
            2.0.1
        

        
            jdom
            jdom
            1.1
        
    

3、PublishShp.java代码

package geoserver;

import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.decoder.RESTDataStoreList;

import java.io.File;
import java.util.List;

public class PublishShp {

    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");
        String srs = "EPSG:4326";

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


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

你可能感兴趣的:(java发布shp数据)