Skip to content

格式 Format

标准版提供 6 种数据格式解析器,支持 GeoJSON、KML、GPX、SHP、WKT、CSV 等常见地理数据格式的读写。所有类型继承自抽象基类 Format

Format(抽象基类)

所有格式解析器的公共基类。

ts
abstract class Format {
  readonly type: string;

  constructor(type: string);

  abstract read(data: string | object, options?: object): object;
  abstract write(features: object, options?: object): string | object;
}

属性

属性类型说明
typestring格式类型标识(只读)

方法

方法参数返回值说明
read(data, options?)data: string | object, options?: objectobject解析数据为内部格式(抽象方法)
write(features, options?)features: object, options?: objectstring | object将内部格式序列化为字符串/对象(抽象方法)

GeoJsonFormat

GeoJSON 格式解析器。

构造函数

ts
class GeoJsonFormat extends Format {
  readonly type = 'geojson';

  constructor();
}

方法

方法参数返回值说明
read(data)data: string | objectobject解析 GeoJSON(支持 JSON 字符串和对象)
write(features)features: objectobject返回原始 GeoJSON 对象

示例

ts
import { GeoJsonFormat } from '@gmap/standard';

const format = new GeoJsonFormat();

// 从字符串解析
const geojson = format.read('{"type":"FeatureCollection","features":[]}');

// 从对象解析
const geojson2 = format.read({
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      geometry: { type: 'Point', coordinates: [116.397, 39.908] },
      properties: { name: '天安门' },
    },
  ],
});

// 写入
const output = format.write(geojson2);

KmlFormat

KML 格式解析器。

构造函数

ts
class KmlFormat extends Format {
  readonly type = 'kml';

  constructor();
}

方法

方法参数返回值说明
read(data)data: stringobject解析 KML XML 字符串
write(features)features: objectstring输出 KML XML 字符串

示例

ts
import { KmlFormat } from '@gmap/standard';

const format = new KmlFormat();

// 解析 KML
const kmlString = `<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark>
      <name>天安门</name>
      <Point><coordinates>116.397,39.908,0</coordinates></Point>
    </Placemark>
  </Document>
</kml>`;

const data = format.read(kmlString);

// 写入 KML
const kml = format.write(data);
console.log(kml); // KML XML 字符串

GpxFormat

GPX 格式解析器,适用于 GPS 轨迹数据。

构造函数

ts
class GpxFormat extends Format {
  readonly type = 'gpx';

  constructor();
}

方法

方法参数返回值说明
read(data)data: stringobject解析 GPX XML 字符串
write(features)features: objectstring输出 GPX XML 字符串

示例

ts
import { GpxFormat } from '@gmap/standard';

const format = new GpxFormat();

const gpxString = `<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1">
  <trk>
    <name>骑行路线</name>
    <trkseg>
      <trkpt lat="39.908" lon="116.397"><ele>50</ele></trkpt>
      <trkpt lat="39.918" lon="116.407"><ele>55</ele></trkpt>
    </trkseg>
  </trk>
</gpx>`;

const data = format.read(gpxString);
const output = format.write(data);

ShpFormat

Shapefile 格式解析器。

构造函数

ts
class ShpFormat extends Format {
  readonly type = 'shp';

  constructor();
}

方法

方法参数返回值说明
read(data)data: string | objectobject解析 Shapefile 数据
write(features)features: objectobject输出 Shapefile 格式

示例

ts
import { ShpFormat } from '@gmap/standard';

const format = new ShpFormat();

// 读取
const data = format.read(shpData);

// 写入
const output = format.write(data);

WktFormat

WKT(Well-Known Text)格式解析器。

构造函数

ts
class WktFormat extends Format {
  readonly type = 'wkt';

  constructor();
}

方法

方法参数返回值说明
read(data)data: stringobject解析 WKT 字符串
write(features)features: objectstring输出 WKT 字符串

示例

ts
import { WktFormat } from '@gmap/standard';

const format = new WktFormat();

// 解析 WKT Point
const point = format.read('POINT (116.397 39.908)');

// 解析 WKT Polygon
const polygon = format.read(
  'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))'
);

// 解析 WKT LineString
const line = format.read(
  'LINESTRING (0 0, 1 1, 2 2, 3 3)'
);

// 写入 WKT
const wkt = format.write(point);
console.log(wkt); // 'POINT (116.397 39.908)'

支持的 WKT 类型

类型WKT 示例
PointPOINT (116.397 39.908)
MultiPointMULTIPOINT ((0 0), (1 1))
LineStringLINESTRING (0 0, 1 1, 2 2)
MultiLineStringMULTILINESTRING ((0 0, 1 1), (2 2, 3 3))
PolygonPOLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
MultiPolygonMULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)))
GeometryCollectionGEOMETRYCOLLECTION (POINT (0 0), LINESTRING (0 0, 1 1))

CsvFormat

CSV 格式解析器,适用于含坐标列的表格数据。

构造函数

ts
class CsvFormat extends Format {
  readonly type = 'csv';

  constructor();
}

方法

方法参数返回值说明
read(data)data: stringobject解析 CSV 字符串为 GeoJSON
write(features)features: objectstring输出 CSV 字符串

示例

ts
import { CsvFormat } from '@gmap/standard';

const format = new CsvFormat();

const csvString = `name,lng,lat,population
天安门,116.397,39.908,100000
故宫,116.403,39.916,0
鸟巢,116.396,39.993,80000`;

const data = format.read(csvString);
// 返回 FeatureCollection,每行变为一个 Point Feature

const csv = format.write(data);

格式对比

格式类型扩展名读取写入适用场景
GeoJsongeojson.geojson, .json支持支持Web 应用标准格式
KMLkml.kml支持支持Google Earth
GPXgpx.gpx支持支持GPS 轨迹
SHPshp.shp支持支持GIS 桌面软件
WKTwkt支持支持数据库/文本表示
CSVcsv.csv支持支持表格数据

综合示例

ts
import { GeoJsonFormat, KmlFormat, WktFormat, CsvFormat } from '@gmap/standard';

// 格式转换:CSV -> GeoJSON
const csv = new CsvFormat();
const geojson = new GeoJsonFormat();

const csvData = 'name,lng,lat\n站点A,116.4,39.9\n站点B,116.5,40.0';
const features = csv.read(csvData);
const geojsonOutput = geojson.write(features);

// 格式转换:KML -> GeoJSON
const kml = new KmlFormat();
const kmlData = kml.read(kmlString);
const converted = geojson.write(kmlData);

// 格式转换:WKT -> GeoJSON
const wkt = new WktFormat();
const wktData = wkt.read('POINT (116.397 39.908)');

四川省交通运输综合地理服务平台 地图开发框架