注释说明都添加在了代码里的注释~~自取不谢٩(˃̶͈̀௰˂̶͈́)و
package com.leadinsight.services.dataacquirement.utils.geom;
import com.leadinsight.services.dataacquirement.utils.sentinelhub.process.MultiPolygonList;
import com.leadinsight.services.dataacquirement.utils.sentinelhub.process.PolygonList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Description wkt转各种格式 + 各种格式转wkt
* @Author AQin1012.
* @CreateTime 2021/9/28 14:10
* @Version
**/
public class WKTUtil {
/**
* wkt转json里的geometry字符串
*
* @param wkt
* @return
*/
public static String wkt2geoJson(String wkt) {
if (null == wkt) {
System.out.println("null == wkt");
return null;
}
// wkt = wkt.replaceAll("\\s*", "");
// wkt = wkt.replaceAll(" ", "");
// System.out.println("wkt=" + wkt);
// "POLYGON((113.299 40.95,113.324 40.97,113.323 40.949,113.299 40.95))";
String json = "";
json = wkt.replace("MULTIPOLYGON", "").replace("POLYGON", "").replace("POINT", "");
json = json.replaceAll("\\(\\(\\(", "\\[\\[\\[\\[").replaceAll("\\)\\)\\)", "]]]]");
json = json.replaceAll("\\)\\);\\(\\(", "]]];\\[\\[\\[").replaceAll("\\(\\(", "\\[\\[\\[").replaceAll("\\)\\)", "]]]");
json = json.replaceAll("\\),\\(", "]];\\[\\[").replaceAll("\\(", "\\[\\[").replaceAll("\\)", "]]");
json = json.replaceAll(",","];\\[");
json = json.replaceAll(" ", ",");
json = json.replaceAll(";", ",");
// System.out.println("json.replace=" + json);
return json;
}
/**
* json里的geometry字符串转wkt [[[][]]]
*
* @param json
* @return
*/
public static String geoJson2Wkt(String json) {
if (null == json) {
System.out.println("null == json");
return null;
}
json = json.replaceAll("\\s*", "");
json = json.replaceAll(" ", "");
System.out.println("json=" + json);
String wkt = "";
wkt = json.replaceAll("\\[\\[\\[\\[", "(((").replaceAll("]]]]", ")))");
wkt = wkt.replaceAll("]]],\\[\\[\\[", "));((").replaceAll("\\[\\[\\[", "((").replaceAll("]]]", "))");
wkt = wkt.replaceAll("]],\\[\\[", ");(").replaceAll("\\[\\[", "(").replaceAll("]]", ")");
wkt = wkt.replaceAll("],\\[", ";").replaceAll("\\[", "").replaceAll("]", "");
wkt = wkt.replaceAll(",", " ");
wkt = wkt.replaceAll(";", ",");
System.out.println("wkt.replace=" + wkt);
if (wkt.contains("(((")) {
wkt = "MultiPolygon " + wkt;
} else if (wkt.contains("((")) {
wkt = "Polygon " + wkt;
} else {
wkt = "Point " + wkt;
}
// System.out.println("wkt=" + wkt);
return wkt;
}
public static String getStringByType(String json, String type) {
if (type.equals("MultiPolygon")) {
List<MultiPolygonList> multiPolygonLists = geoJson2ListOfMultiPolygonList(json);
return Arrays.toString(multiPolygonLists.toArray());
} else if (type.equals("Polygon")) {
List<PolygonList> polygonLists = geoJson2ListOfPolygonList(json);
return Arrays.toString(polygonLists.toArray());
} else {
return "不支持类型:" + type;
}
}
public static List<PolygonList> geoJson2ListOfPolygonList(String json) {
StringBuilder sb = new StringBuilder();
// 先判断有几个polygon
json = json.replaceAll("\\s*", "");
System.out.println("json.replaceAll = " + json);
List<PolygonList> lList = new ArrayList<>();
if (json.contains("]],[[")) {
// 一个有孔的polygon
String[] splitPoly = json.split("]],\\[\\[");
for (String sp : splitPoly) {
String[] splitCoord = sp.split("],");
List<Double[]> doublesList = new ArrayList<>();
PolygonList polygonList = new PolygonList();
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
polygonList.setPolygonList(doublesList);
}
lList.add(polygonList);
}
} else {
// 一个无孔的polygon
String[] splitCoord = json.split("],");
List<Double[]> doublesList = new ArrayList<>();
PolygonList polygonList = new PolygonList();
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
polygonList.setPolygonList(doublesList);
}
lList.add(polygonList);
}
return lList;
}
public static List<MultiPolygonList> geoJson2ListOfMultiPolygonList(String json) {
StringBuilder sb = new StringBuilder();
// 先判断有几个polygon
json = json.replaceAll("\\s*", "");
System.out.println("json.replaceAll = " + json);
List<MultiPolygonList> llList = new ArrayList<>();
if (json.contains("]]],[[[")) {
// 多个polygon
String[] splitMulti = json.split("]]],\\[\\[\\[");
for (String sm : splitMulti) {
List<PolygonList> lList = new ArrayList<>();
MultiPolygonList multiPolygonList = new MultiPolygonList();
// System.out.println("split = " + sm);
// 再判断当前polygon是否有孔
if (sm.contains("]],[[")) {
// 有孔
String[] splitPoly = sm.split("]],\\[\\[");
for (String sp : splitPoly) {
String[] splitCoord = sp.split("],");
List<Double[]> doublesList = new ArrayList<>();
PolygonList polygonList = new PolygonList();
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
polygonList.setPolygonList(doublesList);
}
lList.add(polygonList);
}
} else {
// 无孔
List<Double[]> doublesList = new ArrayList<>();
PolygonList polygonList = new PolygonList();
String[] splitCoord = sm.split("],\\[");
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
polygonList.setPolygonList(doublesList);
}
lList.add(polygonList);
}
multiPolygonList.setMultiPolygonList(lList);
llList.add(multiPolygonList);
}
} else {
System.out.println("类型与json不匹配!");
return null;
}
return llList;
}
/**
* JSON中的geometry转为Double格式的List
* 无需输出版本
*
* @param json
* @return
*/
public static List<List<List<Double[]>>> geoJson2ListOfDoubleList(String json) {
StringBuilder sb = new StringBuilder();
// 先判断有几个polygon
json = json.replaceAll("\\s*", "");
System.out.println("json.replaceAll = " + json);
List<List<List<Double[]>>> llList = new ArrayList<>();
if (json.contains("]]],[[[")) {
// 多个polygon
String[] splitMulti = json.split("]]],\\[\\[\\[");
for (String sm : splitMulti) {
List<List<Double[]>> lList = new ArrayList<>();
// System.out.println("split = " + sm);
// 再判断当前polygon是否有孔
if (sm.contains("]],[[")) {
// 有孔
String[] splitPoly = sm.split("]],\\[\\[");
for (String sp : splitPoly) {
String[] splitCoord = sp.split("],");
List<Double[]> doublesList = new ArrayList<>();
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
}
lList.add(doublesList);
}
} else {
// 无孔
List<Double[]> doublesList = new ArrayList<>();
String[] splitCoord = sm.split("],\\[");
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
}
lList.add(doublesList);
}
llList.add(lList);
}
} else if (json.contains("]],[[")) {
// 一个有孔的polygon
String[] splitPoly = json.split("]],\\[\\[");
List<List<Double[]>> lList = new ArrayList<>();
for (String sp : splitPoly) {
String[] splitCoord = sp.split("],");
List<Double[]> doublesList = new ArrayList<>();
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
}
lList.add(doublesList);
}
llList.add(lList);
} else {
// 一个无孔的polygon
String[] splitCoord = json.split("],");
List<List<Double[]>> lList = new ArrayList<>();
List<Double[]> doublesList = new ArrayList<>();
for (String coord : splitCoord) {
coord = coord.replaceAll("\\[", "").replaceAll("]", "");
String[] lonlat = coord.split(",");
Double lon = Double.parseDouble(lonlat[0]);
Double lat = Double.parseDouble(lonlat[1]);
Double[] ll = new Double[]{lon, lat};
doublesList.add(ll);
}
lList.add(doublesList);
llList.add(lList);
}
return llList;
}
}