GeoServer系列-REST接口初识

GeoServer 提供了一系列接口可供开发者读写图层数据,java中也有工具包封装了这些rest接口

1,官网接口文档

https://docs.geoserver.org/stable/en/user/rest/index.html
在这里插入图片描述

web界面上的操作都可以找到接口实现,如创建存储空间,创建数据仓库,发布图层,查询图层等
举几个常见的接口例子如下:

1.1 发布shp图层

先从官网API文档上查看参数描述
https://docs.geoserver.org/latest/en/api/#1.0.0/datastores.yaml
在这里插入图片描述
使用postman先测试下
在这里插入图片描述

需要注意请求头需要加用户名密码的验证
在这里插入图片描述
接口验证信息使用 用户名:密码 加密得到

String client = "admin:geoserver";
client = Base64.getEncoder().encodeToString(client.getBytes());
System.out.println("Basic " + client);

在这里插入图片描述
== 经测试压缩包解压后要有shp文件而不是文件夹,不支持中文shp包,不支持中文的问题后续文章会从源码找问题并处理 ==

1.2 查询图层列表

https://docs.geoserver.org/latest/en/api/#1.0.0/layers.yaml
可以查询所有发布图层,也可以按指定工作空间查询
在这里插入图片描述
在这里插入图片描述
图层基本信息,包含样式详情和属性详情链接
在这里插入图片描述

1.3 查询图层特征属性信息

https://docs.geoserver.org/latest/en/api/#1.0.0/featuretypes.yaml
属性信息包含图层的所有信息,包含存储仓库,坐标系,边界坐标,属性列表,点线面类型等等
在这里插入图片描述

2,JAVA 中调用接口

2.1使用原生接口调用

chatGPT提供的一个朴实无华的demo,用上面发布shp文件举例

package example;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class GeoServerRESTAPIExample {
    public static void main(String[] args) throws IOException {
        String gsUrl = "http://127.0.0.1:8993/geoserver";
        String username = "admin";
        String password = "geoserver";
        String workspace = "wsName";
        String storeName = "shpstoreName";
        String shpFile = "C:\\Users\\CDLX\\Desktop\\EJDL\\EJDL.zip";

        // Encode username and password to Base64
        String auth = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));

        // Construct the REST API endpoint URL
        String endpointUrl = String.format("%s/rest/workspaces/%s/datastores/%s/file.shp?configure=all", gsUrl, workspace, storeName);

        // Read the Shapefile ZIP file
        File file = new File(shpFile);
        byte[] fileBytes = new byte[(int) file.length()];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(fileBytes);
        inputStream.close();

        // Construct the HTTP request
        HttpURLConnection conn = (HttpURLConnection) new URL(endpointUrl).openConnection();
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.setRequestProperty("Authorization", "Basic " + auth);
        conn.setRequestProperty("Content-type", "application/zip");
        conn.getOutputStream().write(fileBytes);

        // Execute the HTTP request and get the response
        int responseCode = conn.getResponseCode();
        String responseMessage = conn.getResponseMessage();

        // Print the response code and message
        System.out.printf("Response code: %d, message: %s%n", responseCode, responseMessage);
    }
}

2.2 工具类包调用

1,pom引入

		<dependency>
            <groupId>it.geosolutions</groupId>
            <artifactId>geoserver-manager</artifactId>
            <version>${geoserver.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
	。。。
 	<repositories>
        <repository>
            <id>GeoSolutions</id>
            <url>http://maven.geo-solutions.it/</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>OSGeo Release Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
            <snapshots><enabled>false</enabled></snapshots>
            <releases><enabled>true</enabled></releases>
        </repository>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Snapshot Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
            <snapshots><enabled>true</enabled></snapshots>
            <releases><enabled>false</enabled></releases>
        </repository>
    </repositories>

2, 工具包简单使用

geoserver创建连接信息

	String url = "http://localhost:8080/geoserver";
	String user= "admin";
	String password= "geoserver";//连接geoServer
	GeoServerRESTManager geoServerRESTManager = null;
	try{
		geoServerRESTManager = new GeoServerRESTManager(newURL(url), user, password);
		assert geoServerRESTManager != null;
		//读写相关操作的对象
		GeoServerRESTReader restReader=geoServerRESTManager.getReader();
		//发布相关操作的对象
		GeoServerRESTPublisher restPublisher= geoServerRESTManager.getPublisher();
	}catch(Exception e) {
		System.out.println("远程连接GeoServer失败...");
		e.printStackTrace();
	}

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
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 的权限。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

占星安啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值