geojson-to-wkt 坐标格式转换

geojson-to-wkt 坐标格式转换

一个 简单、轻量、健壮 的 JavaScript / TypeScript 库,用于将 GeoJSON 数据转换为 WKT(Well-Known Text) 格式。

本库面向 真实 GIS / 数据库场景 设计,适用于将前端或服务端的 GeoJSON 数据输出为数据库可直接使用的 WKT。


特性

  • 🚀 轻量、无依赖
  • 🧠 明确的几何类型映射(非字符串拼接)
  • 🧩 支持 GeoJSON Geometry / Feature / FeatureCollection
  • 📐 支持 Z / M 坐标(XY / XYZ / XYM / XYZM)
  • 🗄 输出数据库标准 WKT
  • 🌲 Tree Shaking 友好,支持按需引入

安装

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

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

支持的类型映射

GeoJSON 类型 WKT 类型
Point POINT
MultiPoint MULTIPOINT
LineString LINESTRING
MultiLineString MULTILINESTRING
Polygon POLYGON
MultiPolygon MULTIPOLYGON
GeometryCollection GEOMETRYCOLLECTION
Feature 对应 Geometry
FeatureCollection GEOMETRYCOLLECTION

API

geometryToWKT(geometry: Geometry): string

GeoJSON Geometry 转换为 WKT 字符串

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

const geometry = {
  type: 'Point',
  coordinates: [120, 30]
};

geometryToWKT(geometry);
// "POINT (120 30)"

featureToWKT(feature: Feature): string

GeoJSON Feature 转换为 WKT

Feature 的 properties 会被忽略,仅处理 geometry

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

const feature = {
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [120, 30]
  },
  properties: {}
};

featureToWKT(feature);
// "POINT (120 30)"

featureCollectionToWKT(featureCollection: FeatureCollection): string

GeoJSON FeatureCollection 转换为 GEOMETRYCOLLECTION WKT

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

const collection = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: [120, 30]
      },
      properties: {}
    },
    {
      type: 'Feature',
      geometry: {
        type: 'LineString',
        coordinates: [[120, 30], [121, 31]]
      },
      properties: {}
    }
  ]
};

featureCollectionToWKT(collection);
// "GEOMETRYCOLLECTION (POINT (120 30), LINESTRING (120 30, 121 31))"

toWKT(input: GeoJSON): string

通用转换方法,自动识别输入类型

支持:

  • Geometry
  • Feature
  • FeatureCollection
js 复制代码
import { toWKT } from '@giszhc/geojson-to-wkt';

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

toWKT({
  type: 'Feature',
  geometry: {
    type: 'LineString',
    coordinates: [[120, 30], [121, 31]]
  }
});
// "LINESTRING (120 30, 121 31)"

Multi* 几何示例

MultiPoint

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

MultiLineString

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

MultiPolygon

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

GeometryCollection

js 复制代码
geometryToWKT({
  type: 'GeometryCollection',
  geometries: [
    {
      type: 'Point',
      coordinates: [10, 10]
    },
    {
      type: 'LineString',
      coordinates: [[20, 20], [30, 30]]
    }
  ]
});
GEOMETRYCOLLECTION (POINT (10 10), LINESTRING (20 20, 30 30))

Z / M 坐标支持

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

推荐测试用例

js 复制代码
const cases = [
  {
    type: 'Point',
    coordinates: [10, 10]
  },
  {
    type: 'MultiPoint',
    coordinates: [[10, 10], [20, 20]]
  },
  {
    type: 'LineString',
    coordinates: [[10, 10], [20, 20]]
  },
  {
    type: 'Polygon',
    coordinates: [[[0, 0], [10, 0], [10, 10], [0, 0]]]
  },
  {
    type: 'GeometryCollection',
    geometries: [
      { type: 'Point', coordinates: [1, 1] },
      { type: 'LineString', coordinates: [[0, 0], [1, 1]] }
    ]
  }
];

cases.forEach(geojson => {
  expect(() => toWKT(geojson as any)).not.toThrow();
});

注意事项

  • 输出格式遵循数据库标准:TYPE (x y, ...)
  • 不自动添加 SRID
  • 不修改原始坐标维度与顺序
  • Feature.properties 不参与转换

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

相关推荐
qq_406176142 小时前
深入浅出 Pinia:Vue3 时代的状态管理新选择
javascript·vue.js·ecmascript
德育处主任Pro3 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
叫我一声阿雷吧3 小时前
JS 入门通关手册(23):JS 异步编程:回调函数与异步本质
javascript·es6·前端面试·回调函数·回调地狱·js异步编程·异步本质
zayzy3 小时前
前端八股总结
开发语言·前端·javascript
今天减肥吗3 小时前
前端面试题
开发语言·前端·javascript
小J听不清4 小时前
CSS 外边距(margin)全解析:取值规则 + 实战用法
前端·javascript·css·html·css3
前端小超超5 小时前
Vue计算属性computed:可写与只读的区别
前端·javascript·vue.js
小J听不清6 小时前
CSS 边框(border)全解析:样式 / 宽度 / 颜色 / 方向取值
前端·javascript·css·html·css3
用户15815963743707 小时前
多 Agent 系统容错与恢复机制:OAuth 过期、Cron 级联失败的工程解法
javascript
m0_459252468 小时前
fastadmin动态渲染统计信息
开发语言·前端·javascript·php