<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 定义变量
const jsonPoint = [39.69, 120.39];
const jsonLine = [
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
];
const jsonMultiLine = [
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
],
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
]
];
const jsonPolygon = [
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
],
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
]
];
const jsonMultiPolygon = [
[
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
],
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
],
],
[
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
],
[
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39],
[39.69, 120.39]
]
]
];
const wktPoint = "point(120.39 39.69)";
const wktLine = "linestring(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69)";
const wktMultiLine =
"multiLinestring((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69))";
const wktPolygon =
"polygon((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69))";
const wktMultiPolygon =
"multiPolygon(((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69)),((120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69),(120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69,120.39 39.69)))";
/**
* wkt转经纬度坐标
* @param wkt wkt数据
*/
function wktToGeometry(wkt) {
if (!wkt) {
return;
}
let type = wkt.match(/[a-zA-Z]+/g);
if (!type) {
return null;
}
let coordinates = wkt.replace(type, '');
coordinates = coordinates.replaceAll('(', '[');
coordinates = coordinates.replaceAll(')', ']');
coordinates = coordinates.replaceAll(' ', ',');
function coordinatesFormat(coordinates) {
const arr = [];
if (!isArr) {
return;
}
for (let i = 0; i < coordinates.length; i++) {
if (isArr(coordinates[i])) {
coordinates[i] = coordinatesFormat(coordinates[i]);
} else {
if (i % 2 === 0) {
arr.push([coordinates[i + 1], coordinates[i]]);
}
if (i === coordinates.length - 1) {
coordinates = arr;
}
}
}
return coordinates;
}
function getType(type) {
let newType = type.toLowerCase();
if (newType.includes('point')) {
return 'point';
}
if (newType.includes('line')) {
return 'line';
}
if (newType.includes('polygon')) {
return 'polygon';
}
return newType;
}
function isArr(arr) {
return Array.isArray(arr);
}
let lngLats = coordinatesFormat(JSON.parse(coordinates));
const newType = getType(type[0]);
// 点特殊处理
if (newType === 'point') {
lngLats = lngLats[0];
}
return {
type: newType,
lngLats
};
}
/**
* 经纬度坐标转wkt
* @param geometry 要素空间信息
*/
function geometryToWkt(geometry) {
let {
type,
coordinates
} = geometry;
let coordinateWkt = '';
let typeMap = {
point: getPointWkt,
linestring: getLineWkt,
multiLinestring: getMultiLineWkt,
polygon: getPolygonWkt,
multiPolygon: getMultiPolygonWkt
};
// 根据类型调取方法
if (typeMap[type]) {
// 点特殊处理
if (type === 'point') {
coordinateWkt = `(${typeMap[type](coordinates)})`;
} else {
coordinateWkt = typeMap[type](coordinates);
}
};
// 点
function getPointWkt(coordinates) {
if (isArr(coordinates)) {
return `${coordinates[1]} ${coordinates[0]}`;
}
}
// 线
function getLineWkt(coordinates) {
let str = '';
if (isArr(coordinates)) {
coordinates.forEach(coordinate => {
const symbol = str ? ',' : '';
str += `${symbol}${getPointWkt(coordinate)}`;
});
}
return `(${str})`;
}
// 多线
function getMultiLineWkt(coordinates) {
let str = '';
if (isArr(coordinates)) {
coordinates.forEach(coordinate => {
const symbol = str ? ',' : '';
str += `${symbol}${getLineWkt(coordinate)}`;
});
}
return `(${str})`;
}
// 面(处理方式同多线)
function getPolygonWkt(coordinates) {
return getMultiLineWkt(coordinates);
}
// 多面
function getMultiPolygonWkt(coordinates) {
let str = '';
if (isArr(coordinates)) {
coordinates.forEach(coordinate => {
const symbol = str ? ',' : '';
str += `${symbol}${getPolygonWkt(coordinate)}`;
});
}
return `(${str})`;
}
// 判断是否为数组
function isArr(arr) {
return Array.isArray(arr);
}
return `${type}${coordinateWkt}`;
}
console.log(geometryToWkt({
type: 'point',
coordinates: jsonPoint
}));
console.log(geometryToWkt({
type: 'linestring',
coordinates: jsonLine
}));
console.log(geometryToWkt({
type: 'multiLinestring',
coordinates: jsonMultiLine
}));
console.log(geometryToWkt({
type: 'polygon',
coordinates: jsonPolygon
}));
console.log(geometryToWkt({
type: 'multiPolygon',
coordinates: jsonMultiPolygon
}));
console.log('---------------');
console.log(wktToGeometry(wktPoint));
console.log(wktToGeometry(wktLine));
console.log(wktToGeometry(wktMultiLine));
console.log(wktToGeometry(wktPolygon));
console.log(wktToGeometry(wktMultiPolygon));
</script>
</body>
</html>
geometry 与 wkt 互转
最新推荐文章于 2024-05-21 18:09:01 发布