|
|
@@ -77,9 +77,98 @@ function geojsonToWkt(geojson) {
|
|
|
|
|
|
throw new Error('不支持的几何类型: ' + geom.type);
|
|
|
}
|
|
|
+/**
|
|
|
+ * WKT 转 GeoJSON Geometry,和 geojsonToWkt 成对使用
|
|
|
+ * @param {string} wktStr WKT字符串
|
|
|
+ * @returns {GeoJSON.Geometry} 几何对象
|
|
|
+ */
|
|
|
+function wktToGeoJson(wktStr) {
|
|
|
+ const s = wktStr.trim();
|
|
|
+
|
|
|
+ // 匹配类型,提取括号内内容
|
|
|
+ const pointReg = /^POINT\s*\(\s*([\d\.\-]+)\s+([\d\.\-]+)\s*\)$/i;
|
|
|
+ const lineReg = /^LINESTRING\s*\(\s*(.+?)\s*\)$/i;
|
|
|
+ const polygonReg = /^POLYGON\s*\(\s*(.+?)\s*\)$/i;
|
|
|
+ const multiPointReg = /^MULTIPOINT\s*\(\s*(.+?)\s*\)$/i;
|
|
|
+ const multiLineReg = /^MULTILINESTRING\s*\(\s*(.+?)\s*\)$/i;
|
|
|
+ const multiPolygonReg = /^MULTIPOLYGON\s*\(\s*(.+?)\s*\)$/i;
|
|
|
+ const geoCollReg = /^GEOMETRYCOLLECTION\s*\(\s*(.+?)\s*\)$/i;
|
|
|
+
|
|
|
+ // 辅助:把 "x y,x y" => [[x1,y1],[x2,y2]]
|
|
|
+ function parseCoordList(str) {
|
|
|
+ return str.split(',').map(p => {
|
|
|
+ const [x, y] = p.trim().split(/\s+/).map(Number);
|
|
|
+ return [x, y];
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 辅助:解析环集合 " (x y,...),(x y,...) "
|
|
|
+ function parseRingList(str) {
|
|
|
+ return str.match(/\(([^)]+)\)/g).map(ringStr => {
|
|
|
+ const inner = ringStr.replace(/[()]/g, '').trim();
|
|
|
+ return parseCoordList(inner);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Point
|
|
|
+ let m = s.match(pointReg);
|
|
|
+ if (m) {
|
|
|
+ const [, x, y] = m;
|
|
|
+ return { type: 'Point', coordinates: [Number(x), Number(y)] };
|
|
|
+ }
|
|
|
+
|
|
|
+ // LineString
|
|
|
+ m = s.match(lineReg);
|
|
|
+ if (m) {
|
|
|
+ return { type: 'LineString', coordinates: parseCoordList(m[1]) };
|
|
|
+ }
|
|
|
+
|
|
|
+ // Polygon
|
|
|
+ m = s.match(polygonReg);
|
|
|
+ if (m) {
|
|
|
+ return { type: 'Polygon', coordinates: parseRingList(m[1]) };
|
|
|
+ }
|
|
|
+
|
|
|
+ // MultiPoint
|
|
|
+ m = s.match(multiPointReg);
|
|
|
+ if (m) {
|
|
|
+ return { type: 'MultiPoint', coordinates: parseCoordList(m[1]) };
|
|
|
+ }
|
|
|
+
|
|
|
+ // MultiLineString
|
|
|
+ m = s.match(multiLineReg);
|
|
|
+ if (m) {
|
|
|
+ const lines = parseRingList(m[1]);
|
|
|
+ return { type: 'MultiLineString', coordinates: lines };
|
|
|
+ }
|
|
|
+
|
|
|
+ // MultiPolygon
|
|
|
+ m = s.match(multiPolygonReg);
|
|
|
+ if (m) {
|
|
|
+ const polyStrs = m[1].match(/\(\([^)]+\)\)/g);
|
|
|
+ const polygons = polyStrs.map(pStr => {
|
|
|
+ const inner = pStr.replace(/^\(|\)$/g, '');
|
|
|
+ return parseRingList(inner);
|
|
|
+ });
|
|
|
+ return { type: 'MultiPolygon', coordinates: polygons };
|
|
|
+ }
|
|
|
+
|
|
|
+ // GeometryCollection
|
|
|
+ m = s.match(geoCollReg);
|
|
|
+ if (m) {
|
|
|
+ // 简单分割,只处理简单WKT,不处理嵌套括号复杂场景
|
|
|
+ const geoParts = m[1].split(/(?<=\))\s*,\s*(?=[A-Z])/i);
|
|
|
+ const geometries = geoParts.map(part => wktToGeoJson(part.trim()));
|
|
|
+ return { type: 'GeometryCollection', geometries };
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new Error(`不支持的WKT类型:${s}`);
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
export default {
|
|
|
terminalType,
|
|
|
isChinese,
|
|
|
geojsonToWkt,
|
|
|
+ wktToGeoJson,
|
|
|
}
|