npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@giszhc/wkt-to-geojson

v0.0.1

Published

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

Downloads

17

Readme

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
  • 🧪 提供完整测试用例示例

安装

pnpm install @giszhc/wkt-to-geojson

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 |


基础示例

import { wktToGeoJSON } from '@giszhc/wkt-to-geojson';

POINT

wktToGeoJSON('POINT (120 30)');
{
  "type": "Point",
  "coordinates": [120, 30]
}

LINESTRING

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

POLYGON

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

Multi* 几何示例

MULTIPOINT

wktToGeoJSON('MULTIPOINT (10 10, 20 20)');
{
  "type": "MultiPoint",
  "coordinates": [
    [10, 10],
    [20, 20]
  ]
}

MULTILINESTRING

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

MULTIPOLYGON

wktToGeoJSON(
  'MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)))'
);
{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [[0, 0], [10, 0], [10, 10], [0, 0]]
    ]
  ]
}

GeometryCollection(重点)

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

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 坐标支持

wktToGeoJSON('POINT (10 10 5)');
{
  "type": "Point",
  "coordinates": [10, 10, 5]
}
wktToGeoJSON('POINT (10 10 5 7)');
{
  "type": "Point",
  "coordinates": [10, 10, 5, 7]
}

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

以下 全部合法并等价

wktToGeoJSON(`POINT (10 10)`)

wktToGeoJSON(`POINT(10 10)`)

wktToGeoJSON(`POINT
(
  10
  10
)`);

wktToGeoJSON(`
POINT
(
  10
  10
)
`);

测试用例示例(推荐)


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(不自动修复拓扑错误)
  • 不对坐标维度做裁剪,保持原始精度