wkt-to-geojson 坐标格式转换

wkt-to-geojson 坐标格式转换

一个 简单、轻量、健壮 的 JavaScript / TypeScript 库,用于 WKT 转 GeoJSON

本库面向 真实数据库 / GIS 场景 设计,重点解决:

  • ✅ 数据库标准 WKT(如 POINT (10 10)
  • ✅ 紧凑格式(如 POINT(10 10)
  • ✅ 任意空格 / 换行 / 制表符
  • ✅ Multi* 几何
  • ✅ GeometryCollection
  • ✅ Z / ZM 坐标
  • ✅ 双向一致性(WKT ↔ GeoJSON)

特性

  • 🚀 轻量、无依赖
  • 🧠 语法级解析(非简单正则拼接)
  • 🗄 兼容数据库常见输出(PostGIS / MySQL / Oracle)
  • 🧩 支持复杂嵌套 GeometryCollection
  • 🧪 提供完整测试用例示例

安装

bash 复制代码
pnpm install @giszhc/wkt-to-geojson

bash 复制代码
npm install @giszhc/wkt-to-geojson

API

wktToGeoJSON(wkt: string): Geometry

WKT 字符串 转换为 GeoJSON Geometry 对象

支持的 WKT 类型

WKT 类型 GeoJSON 类型
POINT Point
MULTIPOINT MultiPoint
LINESTRING LineString
MULTILINESTRING MultiLineString
POLYGON Polygon
MULTIPOLYGON MultiPolygon
GEOMETRYCOLLECTION GeometryCollection

基础示例

js 复制代码
import { wktToGeoJSON } from '@giszhc/wkt-to-geojson';

POINT

js 复制代码
wktToGeoJSON('POINT (120 30)');
{
  "type": "Point",
  "coordinates": [120, 30]
}

LINESTRING

js 复制代码
wktToGeoJSON('LINESTRING (120 30, 121 31)');
{
  "type": "LineString",
  "coordinates": [
    [120, 30],
    [121, 31]
  ]
}

POLYGON

js 复制代码
wktToGeoJSON('POLYGON ((120 30, 121 31, 122 32, 120 30))');
{
  "type": "Polygon",
  "coordinates": [
    [
      [120, 30],
      [121, 31],
      [122, 32],
      [120, 30]
    ]
  ]
}

Multi* 几何示例

MULTIPOINT

js 复制代码
wktToGeoJSON('MULTIPOINT (10 10, 20 20)');
{
  "type": "MultiPoint",
  "coordinates": [
    [10, 10],
    [20, 20]
  ]
}

MULTILINESTRING

js 复制代码
wktToGeoJSON(
  'MULTILINESTRING ((10 10, 20 20), (30 30, 40 40))'
);
{
  "type": "MultiLineString",
  "coordinates": [
    [[10, 10], [20, 20]],
    [[30, 30], [40, 40]]
  ]
}

MULTIPOLYGON

js 复制代码
wktToGeoJSON(
  'MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)))'
);
{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [[0, 0], [10, 0], [10, 10], [0, 0]]
    ]
  ]
}

GeometryCollection(重点)

GeometryCollection 是 WKT 中最复杂、最容易解析出错的类型,本库已完整支持。

js 复制代码
wktToGeoJSON(`
GEOMETRYCOLLECTION (
  POINT (10 10),
  LINESTRING (20 20, 30 30),
  POLYGON ((0 0, 10 0, 10 10, 0 0))
)
`);
{
  "type": "GeometryCollection",
  "geometries": [
    { "type": "Point", "coordinates": [10, 10] },
    {
      "type": "LineString",
      "coordinates": [[20, 20], [30, 30]]
    },
    {
      "type": "Polygon",
      "coordinates": [[[0, 0], [10, 0], [10, 10], [0, 0]]]
    }
  ]
}

Z / ZM 坐标支持

js 复制代码
wktToGeoJSON('POINT (10 10 5)');
{
  "type": "Point",
  "coordinates": [10, 10, 5]
}
wktToGeoJSON('POINT (10 10 5 7)');
{
  "type": "Point",
  "coordinates": [10, 10, 5, 7]
}

空格 / 换行 / 紧凑格式兼容性

以下 全部合法并等价

js 复制代码
wktToGeoJSON(`POINT (10 10)`)

wktToGeoJSON(`POINT(10 10)`)

wktToGeoJSON(`POINT
(
  10
  10
)`);

wktToGeoJSON(`
POINT
(
  10
  10
)
`);

测试用例示例(推荐)

js 复制代码
console.log(JSON.stringify(wktToGeoJSON('POINT (10 10)')));
console.log(JSON.stringify(wktToGeoJSON('POINT (10 10 5)')));
console.log(JSON.stringify(wktToGeoJSON('POINT (10 10 5 7)')));

console.log(JSON.stringify(wktToGeoJSON('MULTIPOINT (10 10, 20 20, 30 30)')));
console.log(JSON.stringify(wktToGeoJSON('MULTIPOINT ((10 10), (20 20), (30 30))')));
console.log(JSON.stringify(wktToGeoJSON('MULTIPOINT (10 10 1, 20 20 2)')));

console.log(JSON.stringify(wktToGeoJSON('LINESTRING (10 10, 20 20, 30 10)')));
console.log(JSON.stringify(wktToGeoJSON('LINESTRING (10 10 0, 20 20 5, 30 10 2)')));

console.log(JSON.stringify(wktToGeoJSON('MULTILINESTRING ((10 10, 20 20), (30 30, 40 40, 50 30)')));
console.log(JSON.stringify(wktToGeoJSON('MULTILINESTRING ((10 10 1, 20 20 2), (30 30 3, 40 40 4))')));

console.log(JSON.stringify(wktToGeoJSON('POLYGON ((0 0, 10 0, 10 10, 0 0))')));
console.log(JSON.stringify(wktToGeoJSON('POLYGON ((0 0, 20 0, 20 20, 0 0), (5 5, 5 10, 10 10, 5 5))')));
console.log(JSON.stringify(wktToGeoJSON('POLYGON ((0 0 0, 10 0 0, 10 10 5, 0 0 0))')));

console.log(JSON.stringify(wktToGeoJSON('MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)), ((20 20, 30 20, 30 30, 20 20)))')));
console.log(JSON.stringify(wktToGeoJSON('MULTIPOLYGON (((0 0 0, 20 0 0, 20 20 5, 0 0 0), (5 5 1, 5 10 1, 10 10 1, 5 5 1)), ((30 30 2, 40 30 2, 40 40 2, 30 30 2)))')));

console.log(JSON.stringify(wktToGeoJSON('GEOMETRYCOLLECTION (POINT (10 10),LINESTRING (20 20, 30 30),POLYGON ((0 0, 10 0, 10 10, 0 0)))')));
console.log(JSON.stringify(wktToGeoJSON('GEOMETRYCOLLECTION (MULTIPOINT (10 10 1, 20 20 2), MULTILINESTRING ((0 0, 5 5), (10 10, 15 15)), MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0))))')));

注意事项

  • 输入必须是合法 WKT(不自动修复拓扑错误)
  • 不对坐标维度做裁剪,保持原始精度

完结,撒花✿✿ヽ(°▽°)ノ✿

相关推荐
玩大数据的龙威1 天前
农经权二轮延包—各种地块示意图
python·arcgis
雯0609~1 天前
hiprint:实现项目部署与打印3-vue版本-独立出模板设计与模板打印页面
前端·vue.js·arcgis
玩大数据的龙威1 天前
农经权二轮延包—一键生成界址点界址线(ArcGIS插件)
arcgis
leaguecn3 天前
ArcGIS授权管理器断网后自动停止
网络·arcgis·授权
咔咔一顿操作3 天前
轻量无依赖!autoviwe 页面自适应组件实战:从安装到源码深度解析
javascript·arcgis·npm·css3·html5
杨超越luckly4 天前
从传统 GIS 向智能/自动化脚本演进:地铁接驳公交识别的 ArcGIS 与 Python 双路径实践
开发语言·arcgis·php·交互·数据可视化
枝上棉蛮5 天前
2026年GIS软件精选:五款工具的专业性与实用性解析
arcgis·gis·qgis·超图·gisbox·地图数据处理·gis工具
激动的兔子6 天前
Arcgis二次开发--评价单元综合限制级别判断矩阵工具
线性代数·arcgis·矩阵
yngsqq6 天前
arcgis 制作图例、视图页面设置
arcgis
规划酱7 天前
Arcgis中pip安装ezdxf部分GIS有pyparsing安装失败的情况处理
python·arcgis·pip·规划酱