Geojosn中的coordinates数组转换为WKT格式的字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Geojosn中的coordinates数组转换为WKT格式的字符串相关的知识,希望对你有一定的参考价值。
直接贴个类吧,也没有仔细的修改代码。
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 坐标转换为WKT
* @author luhaiyou
* @sate 2019-02-21 10:06
* @version 1.0
*
*/
public class CoordinateUtil {
public static String convert2Wkt(String type, String coordinates) {
//"Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon"
StringBuffer wkt = new StringBuffer();
JSONArray jsonArray = JSONObject.parseArray(coordinates);
type = type.toUpperCase();
switch (type) {
case "POINT":
//[100.0, 0.0]
wkt.append("POINT(").append(jsonArray.getString(0)).append(" ")
.append(jsonArray.getString(1)).append(")");
break;
case "MULTIPOINT":
//[
// [100.0, 0.0],
// [101.0, 1.0]
// ]
wkt.append("MULTIPOINT(");
for (int i = 0; i < jsonArray.size(); i++) {
JSONArray point = jsonArray.getJSONArray(i);
wkt.append(point.getString(0)).append(" ").append(point.get(1)).append(",");
}
wkt.deleteCharAt(wkt.length() - 1);
wkt.append(")");
break;
case "LINESTRING":
//和multipoint结构一样
//[
// [101.0, 0.0],
// [102.0, 1.0]
// ]
wkt.append("LINESTRING").append(wktLineString(jsonArray));
break;
case "MULTILINESTRING":
//[
// [
// [100.0, 0.0],
// [101.0, 1.0]
// ],
// [
// [102.0, 2.0],
// [103.0, 3.0]
// ]
// ]
wkt.append("MULTILINESTRING(");
for (int i = 0; i < jsonArray.size(); i++) {
wkt.append("").append(wktLineString(jsonArray.getJSONArray(i))).append("").append(",");
}
wkt.deleteCharAt(wkt.length() - 1);
wkt.append(")");
break;
case "POLYGON":
//POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))
//"coordinates": [
// [
// [-180.0, 10.0],
// [20.0, 90.0],
// [180.0, -5.0],
// [-30.0, -90.0]
// ]
// ]
wkt.append("POLYGON").append(wktPolygon(jsonArray));
break;
case "MULTIPOLYGON":
//解析为多个polygon
wkt.append("MULTIPOLYGON(");
for (int i = 0; i < jsonArray.size(); i++) {
wkt.append(wktPolygon(jsonArray.getJSONArray(i))).append(",");
}
wkt.deleteCharAt(wkt.length() - 1);
wkt.append(")");
break;
}
return wkt.toString();
}
private static String wktLineString(JSONArray jsonArray) {
StringBuffer wkt = new StringBuffer();
wkt.append("(");
for (int i = 0; i < jsonArray.size(); i++) {
JSONArray point = jsonArray.getJSONArray(i);
wkt.append(point.getString(0)).append(" ").append(point.get(1)).append(",");
}
wkt.deleteCharAt(wkt.length() - 1);
wkt.append(")");
return wkt.toString();
}
private static String wktPolygon(JSONArray jsonArray) {
StringBuffer wkt = new StringBuffer();
wkt.append("(");
for (int i = 0; i < jsonArray.size(); i++) {
//是一组组的LineString
wkt.append("(");
JSONArray lineString = jsonArray.getJSONArray(i);
for (int j = 0; j < lineString.size(); j++) {
JSONArray pointArray = lineString.getJSONArray(j);
wkt.append(pointArray.getString(0)).append(" ").append(pointArray.getString(1))
.append(",");
}
if (wkt.length() > 0) {
//删除最后一个逗号
wkt.deleteCharAt(wkt.length() - 1);
}
wkt.append("),");
}
if (wkt.length() > 0) {
wkt.deleteCharAt(wkt.length() - 1);
}
wkt.append(")");
return wkt.toString();
}
public static void main(String[] args) {
String coord = "[
"
+ " [
"
+ " [100.0, 0.0],
"
+ " [101.0, 1.0]
"
+ " ],
"
+ " [
"
+ " [102.0, 2.0],
"
+ " [103.0, 3.0]
"
+ " ]
"
+ " ]";
String wkt = convert2Wkt("MULTILINESTRING", coord);
System.out.println(wkt);
}
}
以上是关于Geojosn中的coordinates数组转换为WKT格式的字符串的主要内容,如果未能解决你的问题,请参考以下文章
使用 MVVM+Coordinators 模式的 UIViewController 转换
labelme_json_to_dataset不能转换关键点的问题(coordinate list must contain at least 2 coordinates)解决办法