目录
1. shape
1.1代码
一些变量的解释请看代码下面的截图
package com.yinghui.soft.geotools;
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 ShapeUtils {
private static final String DATASTORE_NAME = "your_datastore"; //名称
public static void main(String[] args) throws IOException {
File shapefile = new File("D:/shape/a04.zip"); //文件地址
uploadShapefileToGeoserver(shapefile);
}
public static void uploadShapefileToGeoserver(File shapefile) throws IOException {
// Create a base64 encoded authentication token
String auth = Code.GEOSERVER_USERNAME.getMsg() + ":" + Code.GEOSERVER_PASSWORD.getMsg();
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);
// Create Workspace if it doesn't exist
//createWorkspaceIfNotExists();
// Construct Geoserver resource URL
String geoserverUrl = Code.GEOSERVER_ENDPOINT.getMsg() + "/rest/workspaces/" + Code.WORKSPACE_NAME.getMsg() + "/datastores/" + DATASTORE_NAME + "/file.shp";
// Create HTTP connection
URL url = new URL(geoserverUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Authorization", authHeader);
connection.setRequestProperty("Content-type", "application/zip");
// Read shapefile and write to the Geoserver resource
try (InputStream inputStream = new FileInputStream(shapefile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
connection.getOutputStream().write(buffer, 0, bytesRead);
}
}
// Get the response code
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
// Close the connection
connection.disconnect();
}
}
package com.yinghui.soft.geotools;
public enum Code {
GEOSERVER_USERNAME("GEOSERVER_USERNAME","admin"), //geoserver用户名
GEOSERVER_PASSWORD("GEOSERVER_PASSWORD","geoserver"), //geoserver密码
GEOSERVER_ENDPOINT("GEOSERVER_PASSWORD","http://localhost:8080/geoserver_tzhb"), //geoserver地址
WORKSPACE_NAME("WORKSPACE_NAME","ak_tzhb"), //geoserver工作空间
GEOSERVER_URL("GEOSERVER_URL","http://localhost:8080/geoserver_tzhb"); //geoserver地址
private String code;
private String msg;
Code(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
1.2截图
2. tif
发tif有两种方式,常用第二种
第二种可以发大文件tif
第二种可以发大文件tif
第二种可以发大文件tif
2.1 第一种
package com.yinghui.soft.geotools;
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 TifUtils {
private static final String STORE_NAME = "jie"; //名称
public static void main(String[] args) throws IOException {
File tifFile = new File("D:/shape/L15/通州区.tif");
uploadTifToGeoserver(tifFile);
}
public static void uploadTifToGeoserver(File tifFile) throws IOException {
// Create a base64 encoded authentication token
String auth = Code.GEOSERVER_USERNAME.getMsg() + ":" + Code.GEOSERVER_PASSWORD.getMsg();
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);
// Construct Geoserver resource URL
String geoserverUrl = Code.GEOSERVER_ENDPOINT.getMsg() + "/rest/workspaces/" + Code.WORKSPACE_NAME.getMsg() + "/coveragestores/" + STORE_NAME + "/file.geotiff";
// Create HTTP connection
URL url = new URL(geoserverUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Authorization", authHeader);
connection.setRequestProperty("Content-type", "image/tiff");
// Read tif file and write to the Geoserver resource
try (InputStream inputStream = new FileInputStream(tifFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
connection.getOutputStream().write(buffer, 0, bytesRead);
}
}
// Get the response code
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
// Close the connection
connection.disconnect();
}
}
2.2 第二种
调用了geoserver中添加图层的接口
package com.yinghui.soft.geotools;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class TifUtils10 {
public static void main(String[] args) {
String workspace = "ak_tzhb"; //工作区
String coverageStoreName = "cheng_gong"; //存储仓库名称
String fileURL = "file://D:\\shape\\tif\\L18\\tong.tif"; //tif地址
String layer="zhi"; //图层名称
String naticeName="tong"; //原图层名称
String title="zhi"; //标题名称
try {
// 创建数据存储
String createCoverageStoreRequest = Code.GEOSERVER_URL.getMsg() + "/rest/workspaces/" + Code.WORKSPACE_NAME.getMsg() + "/coveragestores";
String createCoverageStorePayload = "<coverageStore><name>" + coverageStoreName + "</name><type>GeoTIFF</type><enabled>true</enabled><workspace>" + workspace + "</workspace><url>" + fileURL + "</url></coverageStore>";
sendPostRequest(createCoverageStoreRequest, Code.GEOSERVER_USERNAME.getMsg(), Code.GEOSERVER_PASSWORD.getMsg(), createCoverageStorePayload);
// 发布图层
String publishLayerRequest = Code.GEOSERVER_URL.getMsg() + "/rest/workspaces/" + Code.WORKSPACE_NAME.getMsg() + "/coveragestores/" + coverageStoreName + "/coverages";
String publishLayerPayload = "<coverage>\n" +
" <name>"+layer+"</name>\n" +
" <nativeName>"+naticeName+"</nativeName>\n" +
" <enabled>true</enabled>\n" +
" <title>"+title+"</title>\n"+
"</coverage>";
sendPostRequest(publishLayerRequest, Code.GEOSERVER_USERNAME.getMsg(), Code.GEOSERVER_PASSWORD.getMsg(), publishLayerPayload);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void sendPostRequest(String requestURL, String username, String password, String payload) throws Exception {
URL url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// 设置HTTP Basic认证(使用Base64编码)
String authString = username + ":" + password;
String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
// 设置请求内容类型
connection.setRequestProperty("Content-Type", "application/xml");
// 发送请求
connection.getOutputStream().write(payload.getBytes());
// 获取响应结果
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 输出响应结果
System.out.println(response.toString());
connection.disconnect();
}
}
2.3 code
package com.yinghui.soft.geotools;
public enum Code {
GEOSERVER_USERNAME("GEOSERVER_USERNAME","admin"), //geoserver用户名
GEOSERVER_PASSWORD("GEOSERVER_PASSWORD","geoserver"), //geoserver密码
GEOSERVER_ENDPOINT("GEOSERVER_PASSWORD","http://localhost:8080/geoserver_tzhb"), //geoserver地址
WORKSPACE_NAME("WORKSPACE_NAME","ak_tzhb"), //geoserver工作空间
GEOSERVER_URL("GEOSERVER_URL","http://localhost:8080/geoserver_tzhb"); //geoserver地址
private String code;
private String msg;
Code(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}