学习记录2-java实现geoserver接口的图层发布和删除、修改标题

承接学习记录1

发布:
把your_localhost_port、your_workspaces、your_datastores的值替换为有效值就可以了



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class ReleaseUtil {
	public static void main(String[] args) {
		try {
			String url = "http://your_localhost_port/geoserver/rest/workspaces/your_workspaces/datastores/your_datastores/featuretypes";
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为PUT
			con.setRequestMethod("POST");

			// 设置请求头部信息
			con.setRequestProperty("Content-Type", "application/xml");
			con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 设置请求体数据
			String data = "<featureType>\n" +
				"    <name>zhjzzy_jc_sqzy</name>\n" +
				"    <nativeName>zhjzzy_jc_sqzy</nativeName>\n" +
				"</featureType>";
			con.setDoOutput(true);
			con.getOutputStream().write(data.getBytes("UTF-8"));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// 打印响应结果
			System.out.println("Response Code: " + responseCode);
			System.out.println("Response Body: " + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

效果:

修改图层的标题名称同样:



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class StatusUtil
{
	public static void main(String[] args) {
		try {
			String url = "http://your_localhost_port/geoserver/rest/workspaces/your_workspaces/datastores/your_datastores/featuretypes/zhjzzy_jc_sqzy";
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为PUT
			con.setRequestMethod("PUT");

			// 设置请求头部信息
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 设置请求体数据
			String data = "{\n" +
				" \"featureType\": {" +
				" \"title\": \"测试一下zhjzzy_jc_sqzy\"\n" +
				"}\n}";
			con.setDoOutput(true);
			con.getOutputStream().write(data.getBytes("UTF-8"));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// 打印响应结果
			System.out.println("Response Code: " + responseCode);
			System.out.println("Response Body: " + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

效果:

测试成功,那完全可以把相同的部分提出来,编为工具类:

发布图层的工具类:

已知标题的标签为"title",故在发布图层时如果需要直接命名图层标题,以xml形式发送"<title>你要取的标题</title>"即可。

以此类推,其他图层属性也可直接指定,如果没有指定就为默认。



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

/**
 * 发布图层
 */
public class Util {

	/**
	 * 发布图层工具
	 * @param localhostPort 端口号
	 * @param workspaces 工作空间
	 * @param dataStores 存储空间
	 * @param name 图层特征类型名称
	 * @param nativeName 图层特征类型本地名称
	 */
	public static void geoReleaseTool(String localhostPort, String workspaces, String dataStores, String name, String nativeName)
	{
		try {
			String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/datastores/"+dataStores+"/featuretypes";
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为POST
			con.setRequestMethod("POST");

			// 设置请求头部信息
			con.setRequestProperty("Content-Type", "application/xml");
			con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 设置请求体数据
			String data1 = "<featureType>\n" +
				"    <name>"+name+"</name>\n" +
				"    <nativeName>"+nativeName+"</nativeName>\n" +
				"</featureType>";
			con.setDoOutput(true);
			con.getOutputStream().write(data1.getBytes("UTF-8"));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// 打印响应结果
			System.out.println("Response Code: " + responseCode);
			System.out.println("Response Body: " + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

修改标题的:



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

/**
 * 修改图层标题
 */
public class NameUtil {

	/**
	 * 修改图层标题工具
	 * @param localhostPort 端口号
	 * @param workspaces 工作空间
	 * @param dataStores 储存空间
	 * @param name	图层特征类型名称
	 * @param afterName 修改后的标题
	 */
	public static void geoModifyNameTool(String localhostPort, String workspaces, String dataStores, String name, String afterName)
	{
		try {
			String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/datastores/"+dataStores+"/featuretypes/"+name;
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为PUT
			con.setRequestMethod("PUT");

			// 设置请求头部信息
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 设置请求体数据
			String data = "{\n" +
				" \"featureType\": {" +
				" \"title\": \""+afterName+"\"\n" +
				"}\n}";
			con.setDoOutput(true);
			con.getOutputStream().write(data.getBytes("UTF-8"));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// 打印响应结果
			System.out.println("Response Code: " + responseCode);
			System.out.println("Response Body: " + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

测试结果:

补充:以上的工具只能适用于shp图层的发布,因为其他形式的图层请求的接口和请求体都不同,下面会举类Giff形式的图层发布和存储空间的创建。

简单封了一个常量池:

package com.albedo.java.modules.util.http;

public interface Constant {

	String ADMIN = "admin";

	String GEOSERVER = "geoserver";

	String HTTP = "http://";

	String SLASH_GEOSERVER = "/geoserver";

	String SLASH_REST = "/rest";

	String SLASH_WORKSPACE = "/workspaces";

	String SLASH_DATASTORES = "/datastores";

	String SLASH_LAYERS = "/layers";

	String SLASH_COVERAGESTORES = "/coveragestores";

	String COVERAGES = "/coverages";

	//HTTP-POST请求
	String HTTP_POST = "POST";

	//HTTP-GET请求
	String HTTP_GET = "GET";

	//HTTP-PUT请求
	String HTTP_PUT = "PUT";

	//HTTP-DELETE请求
	String HTTP_DELETE = "DELETE";

	//请求头部字段-Content-Type
	String CONTENT_TYPE = "Content-Type";

	//请求响应中的实体主体为JSON
	String APPLICATION_JSON = "application/json";

	String APPLICATION_SLASH = "application/";

	//请求响应中的实体主体为XML
	String APPLICATION_XML = "application/XML";

	//请求头部字段-Authorization
	String AUTHORIZATION = "Authorization";

	//响应字符类型-GBK
	String GBK = "GBK";

	//响应状态码
	String RESPONSE_CODE = "Response Code: ";

	//响应体
	String RESPONSE_BODY = "Response Body: ";

	//基本身份验证
	String BASIC = "Basic ";

	//斜杠
	String SLASH = "/";

	String CMD = "cmd";

	String CMD_DOT_EXE = "cmd.exe";

	String SLASH_FEATURETYPES = "/featuretypes";

	String MINUS = "-";

	String MINUS_I = "-I";

	String MINUS_S = "-s";

	String SHP2PGSQL = "shp2pgsql";

	String F_T_T_S = "4326";

	String EPSG_4490 = "EPSG:4490";

	String DOT_JSON = ".json";

	String DOT_HTML = ".html";
}

下面的代码涉及到常量的我就不改了,大家对比上面的代码意会即可

giff---储存空间

/**
	 * 创建Tif的储存仓库
	 * @param localhostPort 端口号
	 * @param workspaceName 工作空间
	 * @param dataStoreName 储存空间名
	 * @param path tiff文件路径
	 */
	public static void createTifDataStores(String localhostPort, String workspaceName, String path, String dataStoreName)
	{
		try {
			String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaceName+"/coveragestores/datastores";
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为POST
			con.setRequestMethod(Constant.HTTP_POST);

			// 设置请求头部信息
			con.setRequestProperty(Constant.CONTENT_TYPE, Constant.APPLICATION_JSON);
			con.setRequestProperty(Constant.AUTHORIZATION, Constant.BASIC + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 设置请求体数据
			String data1 = "{\n" +
				"  \"coverageStore\": {\n" +
				"    \"name\": \""+dataStoreName+"\",\n" +
				"    \"type\": \"GeoTIFF\",\n" +
				"    \"enabled\": true,\n" +
				"    \"url\": \"file:"+path+"\",\n" +
				"    \"workspace\": {\n" +
				"      \"name\": \""+workspaceName+"\"\n" +
				"    }\n" +
				"  }\n" +
				"}";
			con.setDoOutput(true);
			con.getOutputStream().write(data1.getBytes(Constants.UTF_8));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// 打印响应结果
			System.out.println(Constant.RESPONSE_CODE + responseCode);
			System.out.println(Constant.RESPONSE_BODY + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

giff--图层发布

/**
	 * POST 发布图层工具
	 * @param localhostPort 端口号
	 * @param workspaces 工作空间
	 * @param dataStores 存储空间
	 * @param name 图层特征类型名称
	 * @param title 图层标题
	 */
	public static void geoTiffReleaseTool(String localhostPort, String workspaces, String dataStores, String name, String title)
	{
		try {
			String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/coveragestores/"+dataStores+"/coverages";
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为POST
			con.setRequestMethod(Constant.HTTP_POST);

			// 设置请求头部信息
			con.setRequestProperty(Constant.CONTENT_TYPE, Constant.APPLICATION_JSON);
			con.setRequestProperty(Constant.AUTHORIZATION, Constant.BASIC + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 设置请求体数据
			String data1 = "{\n" +
				"  \"coverage\": {\n" +
				"    \"name\": \""+name+"\",\n" +
				"    \"title\": \""+title+"\",\n" +
				"    \"abstract\": \""+name+"\",\n" +
				"    \"enabled\": true,\n" +
				"\"srs\": \""+Constant.EPSG_4490+"\""+
				"  }\n" +
				"}";
			con.setDoOutput(true);
			con.getOutputStream().write(data1.getBytes(Constants.UTF_8));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// 打印响应结果
			System.out.println(Constant.RESPONSE_CODE + responseCode);
			System.out.println(Constant.RESPONSE_BODY + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

同理,能用此种方式实现更多的接口功能,例如删除:

/**
	 * DELETE 删除已发布的图层
	 * @param localhostPort 端口号
	 * @param workspaces 工作空间
	 * @param dataStores 储存仓库
	 * @param layerName 图层特征名
	 */
	public static void geoDeleteTool(String localhostPort, String workspaces, String dataStores, String layerName)
	{
		try {
			String url = "http://"+localhostPort+"/geoserver/rest/layers/"+layerName;
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为DELETE
			con.setRequestMethod("DELETE");

			// 设置请求头部信息
			con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			deleteTool(localhostPort, workspaces, dataStores, layerName);

			// 打印响应结果
			System.out.println("Response Code: " + responseCode);
			System.out.println("Response Body: " + response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除工具,只有执行这一步才能再次发布曾经发布过的图层名
	 * @param localhostPort 端口号
	 * @param workspaces 工作空间
	 * @param dataStores 储存仓库
	 * @param layerName 图层特征名
	 */
	public static void deleteTool(String localhostPort, String workspaces, String dataStores, String layerName)
	{
		try {
			String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/datastores/"+dataStores+"/featuretypes/"+layerName+".html";
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();

			// 设置请求方法为DELETE
			con.setRequestMethod("DELETE");

			// 设置请求头部信息
			con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));

			// 发送请求并获取响应
			int responseCode = con.getResponseCode();
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

再修改一下就可以配合前端传输的数据使用了捏,更多的接口功能不再赘述。

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用 geoserver-manager 发布图层并进行 CQL 过滤的步骤如下: 1. 首先,需要在 pom.xml 文件中添加 geoserver-manager 的依赖: ```xml <dependency> <groupId>org.geoserver</groupId> <artifactId>gs-manager</artifactId> <version>2.16.1</version> </dependency> ``` 2. 创建一个 GeoserverRESTManager 对象,并设置 Geoserver 的基本信息:URL、用户名和密码。 ```java GeoserverRESTManager manager = new GeoserverRESTManager("http://localhost:8080/geoserver", "admin", "geoserver"); ``` 3. 获取一个 GeoserverWorkspace 对象,用于发布图层。 ```java GeoserverWorkspace workspace = manager.getWorkspace("workspace_name"); ``` 4. 创建一个 GeoserverDataStore 对象,用于发布数据源和图层。在创建数据源时,可以设置 CQL 过滤器。 ```java // 创建 PostGIS 数据源 PostGISDataStoreEncoder encoder = new PostGISDataStoreEncoder(); encoder.setHost("localhost"); encoder.setPort(5432); encoder.setDatabase("database_name"); encoder.setUser("user_name"); encoder.setPassword("password"); encoder.setSchema("public"); encoder.setExposePrimaryKeys(true); encoder.setLooseBbox(true); encoder.setEstimatedExtents(true); encoder.setValidateConnections(true); encoder.setMaxConnections(10); encoder.setMinConnections(1); encoder.setConnectionTimeout(20); encoder.setMaxPreparedStatements(20); PostGISDataStore dataStore = workspace.createDatastore("datastore_name", encoder); // 设置 CQL 过滤器 String cqlFilter = "property_name='property_value'"; dataStore.setDefaultCQLFilter(cqlFilter); ``` 5. 创建一个 GeoserverFeatureType 对象,并设置图层的基本信息:名称、数据源、几何类型等。 ```java GeoserverFeatureType featureType = dataStore.createFeatureType("layer_name", "the_geom", "EPSG:4326"); ``` 6. 发布图层。 ```java dataStore.publishFeatureType(featureType); ``` 完成上述步骤后,就可以成功发布一个带有 CQL 过滤器的图层了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值