Geoserver的rest接口使用(后台或者前端调实现自动发布服务)

Geoserver提供许多服务的发布,我们可以通过控制台进行手动发布,至于至于如何在程序中利用代码发布服务,这如何做?可以通过两种思路进行一通过后台通过请求rest接口进行发布或者前端通过ajax,另一种通过xml方式详细的请参考该篇文章简析GeoServer服务的内部文件组织以及GeoServer自动化服务发布工具的开发思路本文主要介绍地第一种方式(java语言)如何发布,开始进入正题。
效果图
1、postgis数据
在这里插入图片描述
2、shapefile数据
在这里插入图片描述
一、maven中构造
通过Java构造请求我们需要用到geoserver-manager类库(api文档
1、添加依赖

   <repository>
      <id>GeoSolutions</id>
      <url>http://maven.geo-solutions.it/</url>
   </repository>
  <dependency>
    <groupId>it.geosolutions</groupId>
    <artifactId>geoserver-manager</artifactId>
    <version>1.7.0</version>
  </dependency>

2、几个重要的类对象

  1. GeoServerRESTManager该对象是一个最大的管理者可以获取以下两个对象,创建数据存储
  2. GeoServerRESTPublisher,发布对象,用来发布各种数据和创建工作空间(主要用来创建对象)
  3. GeoServerRESTReader,获取数据存储、图层、样式、图层组等(主要用来获取信息)
    二、demo示例(发布postgis数据和shapfile数据)
package com.hpu.geoserver;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class GeoserverPostGIS {
public static void main(String[] args) throws IOException {
	//GeoServer的连接配置
    String url = "http://localhost:8080/geoserver" ;
    String username = "admin" ;
    String passwd = "geoserver" ;
    //GeoserverPublishPostGISData(url, username, passwd);
    GeoserverPublishShapefileData(url, username, passwd);
}
//发布postgis中的数据
public static void GeoserverPublishPostGISData(String url,String username,String passwd) throws IOException{
	
    //postgis连接配置
    String postgisHost = "localhost" ;
    int postgisPort = 5432 ;//端口号
    String postgisUser = "postgres" ;//用户名
    String postgisPassword = "postgres" ;//用户密码
    String postgisDatabase = "sqlView" ;//数据库名称

    String ws = "testNew" ;     //待创建和发布图层的工作区名称workspace
    String store_name = "testGeoserver" ; //待创建和发布图层的数据存储名称store
    String table_name = "roa_4m" ; // 数据库要发布的表名称,后面图层名称和表名保持一致
    //判断工作区(workspace)是否存在,不存在则创建
    URL u = new URL(url);
    GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
   
    GeoServerRESTPublisher publisher = manager.getPublisher() ;
    List<String> workspaces = manager.getReader().getWorkspaceNames();
    if(!workspaces.contains(ws)){
        boolean createws = publisher.createWorkspace(ws);
        System.out.println("create ws : " + createws);
    }else {
        System.out.println("workspace已经存在了,ws :" + ws);
    }


    //判断数据存储(datastore)是否已经存在,不存在则创建
    RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
    if(restStore == null){
        GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
        store.setHost(postgisHost);//设置url
        store.setPort(postgisPort);//设置端口
        store.setUser(postgisUser);// 数据库的用户名
        store.setPassword(postgisPassword);// 数据库的密码
        store.setDatabase(postgisDatabase);// 那个数据库;
        store.setSchema("public"); //当前先默认使用public这个schema
        store.setConnectionTimeout(20);// 超时设置
        //store.setName(schema);
        store.setMaxConnections(20); // 最大连接数
        store.setMinConnections(1);     // 最小连接数
        store.setExposePrimaryKeys(true);
        boolean createStore = manager.getStoreManager().create(ws, store);
        System.out.println("create store : " + createStore);
    } else {
        System.out.println("数据存储已经存在了,store:" + store_name);
    }

    //判断图层是否已经存在,不存在则创建并发布
    RESTLayer layer = manager.getReader()..getLayer(ws, table_name);
    if(layer == null){
        GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
        pds.setTitle(table_name);
        pds.setName(table_name);
        pds.setSRS("EPSG:4326");
        GSLayerEncoder layerEncoder = new GSLayerEncoder();
        
        boolean publish = manager.getPublisher().publishDBLayer(ws, store_name,  pds, layerEncoder);
        System.out.println("publish : " + publish);
    }else {
        System.out.println("表已经发布过了,table:" + table_name);
    }
	
}
//发布shapefile数据
public static void GeoserverPublishShapefileData(String url,String username,String passwd) throws IOException{
  
    String ws = "testshape" ;     //待创建和发布图层的工作区名称workspace
    String store_name = "testShapeStore" ; //待创建和发布图层的数据存储名称store
    String srs="EPSG:4326";
    //压缩文件的完整路径
    File zipFile=new File("D:/软件安装/geoserver/GeoServer 2.14.0/data_dir/data/shapefiles/states.zip");
    String layername="states";//图层名称
    //shp文件所在的位置
    String urlDatastore="file:data/shapefiles/states.shp";
	//判断工作区(workspace)是否存在,不存在则创建
    URL u = new URL(url);
    //获取管理对象
    GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
   //获取发布对象
    GeoServerRESTPublisher publisher = manager.getPublisher() ;
    //获取所有的工作空间名称
    List<String> workspaces = manager.getReader().getWorkspaceNames();
    //判断工作空间是否存在
    if(!workspaces.contains(ws)){
    	//创建一个新的存储空间
        boolean createws = publisher.createWorkspace(ws);
        System.out.println("create ws : " + createws);
    }else {
        System.out.println("workspace已经存在了,ws :" + ws);
    }

    //判断数据存储(datastore)是否已经存在,不存在则创建
    URL urlShapefile = new URL(urlDatastore);
    RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
    if(restStore == null){  
    	//创建shape文件存储
        GSShapefileDatastoreEncoder store = new GSShapefileDatastoreEncoder(store_name, urlShapefile);  
        boolean createStore = manager.getStoreManager().create(ws, store);
        System.out.println("create store : " + createStore);
    } else {
        System.out.println("数据存储已经存在了,store:" + store_name);
    }

    //判断图层是否已经存在,不存在则创建并发布
    RESTLayer layer = manager.getReader().getLayer(ws, layername);
    if(layer == null){
    	//发布图层
        boolean publish = manager.getPublisher().publishShp(ws, store_name, layername, zipFile, srs);
        System.out.println("publish : " + publish);
    }else {
    	
        System.out.println("表已经发布过了,table:" + store_name);
    }
	
}
}


三、踩的一些坑
1、首先类似于String store_name = "testGeoserver"写成String store_name = "testGeoserver "会报错在geoserver控制台中会报安全错误,空格被解析成分号(
2、在发布shapefile时候创建shapefile存储空间路径前面要加“file://”,最方便的方式如下图,直接新建一个数据源在浏览中找到shape文件所在路径位置,直接复制就好
在这里插入图片描述
3、这里的压缩文件只能为zip格式,不能是rar格式否则会报错
4、压缩文件的路径为全路径

  • 8
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 38
    评论
GeoServer 是一个开源的地理数据服务软件,它支持 RESTful API 以及其他协议,可以通过这些接口来管理和发布地理数据。GeoServer RESTful API 提供了基于 HTTP 的接口,可以通过 HTTP GET, POST, PUT, DELETE 等方法来管理 GeoServer 中的数据。 下面是一些常用的 GeoServer RESTful API 接口: 1. 获取 GeoServer 版本信息:http://localhost:8080/geoserver/rest/about/version.xml 2. 获取所有工作空间信息:http://localhost:8080/geoserver/rest/workspaces.xml 3. 获取指定工作空间中的所有数据存储信息:http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores.xml 4. 获取指定数据存储中的所有图层信息:http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes.xml 5. 创建新的工作空间:POST http://localhost:8080/geoserver/rest/workspaces.xml 6. 创建新的数据存储:POST http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores.xml 7. 创建新的图层:POST http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes.xml 8. 删除工作空间:DELETE http://localhost:8080/geoserver/rest/workspaces/{workspace}.xml 9. 删除数据存储:DELETE http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}.xml 10. 删除图层:DELETE http://localhost:8080/geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes/{featureType}.xml 以上是一些常用的 GeoServer RESTful API 接口,你可以根据自己的需要进行用。需要注意的是,在用这些接口之前,需要先进行身份验证,获取到访问 GeoServer 的权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值