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

proj4ts

v0.0.22

Published

专门用于 CGCS2000 坐标转换

Readme

Proj4TS

专门用于 CGCS2000 (China Geodetic Coordinate System 2000) 坐标转换的 TypeScript 库,同时支持 WGS84、火星坐标系(GCJ-02)和 Web Mercator 坐标系。

功能特性

  • 支持 CGCS2000 坐标系的正向和反向转换
  • 支持 3 度带和 6 度带投影
  • 支持带号和不带带号的坐标表示
  • 支持基于 WKID 的坐标系转换
  • 支持 WGS84 (EPSG:4326) 和 Web Mercator (EPSG:3857) 坐标系
  • 支持火星坐标系(GCJ-02)与 CGCS2000/WGS84 之间的转换
  • 高精度计算,满足工程测量需求

安装

npm install proj4ts

快速开始

import proj4, { Proj4 } from 'proj4ts';

// 经纬度坐标 [lng, lat]
const coordinate: [number, number] = [116.391, 39.907];

// 转换为 3 度带投影坐标 (带带号)
const projected = proj4(coordinate, 4490, 4525); // 4490=CGCS2000地理坐标, 4525=3度带带号39
console.log(projected); // [39500000.000, 4410000.000]

// 投影坐标转回地理坐标
const geographic = proj4([projected[0], projected[1]], 4525, 4490);
console.log(geographic); // [116.391, 39.907]

// WGS84 转 Web Mercator
const mercator = proj4([116.391, 39.907], 4326, 3857);
console.log(mercator); // [12978141.575, 4855865.414]

// Web Mercator 转 WGS84
const wgs84 = proj4(mercator, 3857, 4326);
console.log(wgs84); // [116.391, 39.907]

// CGCS2000 转火星坐标 (GCJ-02)
const gcj02 = proj4.cgcs2000ToGcj02([116.391, 39.907]);
console.log(gcj02); // [116.398, 39.915]

// 火星坐标 (GCJ-02) 转 CGCS2000
const cgcs2000 = proj4.gcj02ToCgcs2000([116.398, 39.915]);
console.log(cgcs2000); // [116.391, 39.907]

使用方法

1. 基本坐标转换

import { Proj4 } from 'proj4ts';

// 地理坐标转投影坐标
const result = Proj4.forward(116.391, 39.907, '3deg', true);
console.log(result); // [39500000.000, 4410000.000]

// 投影坐标转地理坐标 (注意需要提供带号)
const zone = Proj4.calculateZone(116.391, '3deg');
const geographic = Proj4.inverse(result[0], result[1], zone, '3deg', true);
console.log(geographic); // [116.391, 39.907]

// WGS84 经纬度转 Web Mercator
const mercator = Proj4.lngLatToWebMercator(116.391, 39.907);
console.log(mercator); // [12978141.575, 4855865.414]

// Web Mercator 转 WGS84 经纬度
const wgs84 = Proj4.webMercatorToLngLat(12978141.575, 4855865.414);
console.log(wgs84); // [116.391, 39.907]

2. 火星坐标转换(GCJ-02)

import proj4 from 'proj4ts';

// CGCS2000/WGS84 转 GCJ-02(火星坐标)
const gcj02Coord = proj4.cgcs2000ToGcj02([116.391, 39.907]);
console.log(gcj02Coord); // [116.398, 39.915]

// GCJ-02(火星坐标)转 CGCS2000/WGS84
const originalCoord = proj4.gcj02ToCgcs2000([116.398, 39.915]);
console.log(originalCoord); // [116.391, 39.907]

3. 带号计算

// 计算 3 度带带号
const zone3deg = Proj4.calculateZone(116.391, '3deg');
console.log(zone3deg); // 39

// 计算 6 度带带号
const zone6deg = Proj4.calculateZone(116.391, '6deg');
console.log(zone6deg); // 20

4. WKID 坐标系转换

import proj4 from 'proj4ts';

// 使用 WKID 进行坐标转换
const beijing: [number, number] = [116.391, 39.907];

// 转换为 3 度带带号 39 (WKID: 4525)
const projected = proj4(beijing, 4490, 4525);

// 转换回地理坐标
const geo = proj4([projected[0], projected[1]], 4525, 4490);

// WGS84 (4326) 转 Web Mercator (3857)
const mercator = proj4(beijing, 4326, 3857);

// Web Mercator (3857) 转 WGS84 (4326)
const wgs84 = proj4(mercator, 3857, 4326);

支持的 WKID

地理坐标系

  • 4326: WGS 84 Geographic Coordinate System
  • 4490: CGCS2000 Geographic Coordinate System

投影坐标系

Web Mercator

  • 3857: WGS 84 / Pseudo-Mercator

3 度带带带号 (WKID: 4513-4533)

  • 4513-4533: 对应 3 度带 25-45 带

3 度带不带带号 (WKID: 4534-4554)

  • 4534-4554: 对应 3 度带 25-45 带

6 度带带带号 (WKID: 4491-4501)

  • 4491-4501: 对应 6 度带 1-11 带

6 度带不带带号 (WKID: 4502-4512)

  • 4502-4512: 对应 6 度带 13-23 带

API 参考

Proj4.forward(lng, lat, zoneType, withZone)

将地理坐标转换为投影坐标

参数:

  • lng: 经度
  • lat: 纬度
  • zoneType: 分带类型 ('3deg' | '6deg'),默认为 '3deg'
  • withZone: 是否带带号,默认为 false

返回:

[number, number] // [x, y]

Proj4.inverse(x, y, zone, zoneType, withZone)

将投影坐标转换为地理坐标

参数:

  • x: X 坐标
  • y: Y 坐标
  • zone: 带号
  • zoneType: 分带类型 ('3deg' | '6deg'),默认为 '3deg'
  • withZone: 是否带带号,默认为 false

返回:

[number, number] // [lng, lat]

Proj4.calculateZone(lng, zoneType)

计算指定经度所属的带号

参数:

返回: 带号 (number)

Proj4.lngLatToWebMercator(lng, lat)

将 WGS84 经纬度坐标转换为 Web Mercator 投影坐标

参数:

返回:

[number, number] // [x, y] Web Mercator 坐标

Proj4.webMercatorToLngLat(x, y)

将 Web Mercator 投影坐标转换为 WGS84 经纬度坐标

参数:

  • x: X 坐标
  • y: Y 坐标

返回:

[number, number] // [lng, lat] 经纬度坐标

proj4.cgcs2000ToGcj02(coords)

将 CGCS2000/WGS84 坐标转换为 GCJ-02(火星坐标)坐标

参数:

  • coords: 经纬度坐标数组 [lng, lat]

返回: 转换后的 GCJ-02 坐标数组 [lng, lat]

proj4.gcj02ToCgcs2000(coords)

将 GCJ-02(火星坐标)坐标转换为 CGCS2000/WGS84 坐标

参数:

  • coords: GCJ-02 经纬度坐标数组 [lng, lat]

返回: 转换后的 CGCS2000/WGS84 坐标数组 [lng, lat]

proj4(coords, fromWkid, toWkid)

使用 WKID 进行坐标转换

参数:

  • coords: 坐标数组 ([lng, lat] 或 [x, y])
  • fromWkid: 源坐标系 WKID
  • toWkid: 目标坐标系 WKID

返回: 转换后的坐标数组

示例

import proj4, { Proj4 } from 'proj4ts';

// 示例坐标: 北京天安门 [lng, lat]
const beijing: [number, number] = [116.3977, 39.9088];

// 1. 转换为 3 度带投影坐标 (不带带号)
const projected1 = Proj4.forward(beijing[0], beijing[1], '3deg', false);
console.log(projected1);
// 输出: [500000.000, 4415000.000]

// 2. 转换为 3 度带投影坐标 (带带号)
const projected2 = Proj4.forward(beijing[0], beijing[1], '3deg', true);
console.log(projected2);
// 输出: [39500000.000, 4415000.000]

// 3. 使用 WKID 转换
const projected3 = proj4(beijing, 4490, 4525); // CGCS2000 -> 3度带39带带号
console.log(projected3);

// 4. 转换回地理坐标
const geo = proj4([projected3[0], projected3[1]], 4525, 4490);
console.log(geo);
// 输出: [116.3977, 39.9088]

// 5. WGS84 转 Web Mercator
const mercator = proj4(beijing, 4326, 3857);
console.log(mercator);
// 输出: [12978141.575, 4855865.414]

// 6. Web Mercator 转 WGS84
const wgs84 = proj4(mercator, 3857, 4326);
console.log(wgs84);
// 输出: [116.3977, 39.9088]

// 7. CGCS2000 转火星坐标 (GCJ-02)
const gcj02 = proj4.cgcs2000ToGcj02(beijing);
console.log(gcj02);
// 输出: [116.404, 39.915] (略有偏移)

// 8. 火星坐标 (GCJ-02) 转 CGCS2000
const original = proj4.gcj02ToCgcs2000(gcj02);
console.log(original);
// 输出: [116.3977, 39.9088] (恢复原始坐标)

火星坐标系说明

火星坐标系(GCJ-02)是中国国家测绘局制定的坐标加密标准,也被称为"火星坐标"或"国测局坐标"。在中国大陆地区使用的地图服务(如高德地图、腾讯地图等)通常采用此坐标系。

本库提供了 CGCS2000/WGS84 与 GCJ-02 坐标系之间的相互转换功能:

  • proj4.cgcs2000ToGcj02() - 将 CGCS2000/WGS84 坐标转换为 GCJ-02 坐标
  • proj4.gcj02ToCgcs2000() - 将 GCJ-02 坐标转换为 CGCS2000/WGS84 坐标

注意:火星坐标转换仅在中国范围内有效,范围外的坐标将不会进行转换。

精度说明

本库基于高斯-克吕格投影算法实现,坐标转换精度可达毫米级,满足大多数工程测量需求。

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request 来改进这个库。