主题
格式 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;
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
type | string | 格式类型标识(只读) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
read(data, options?) | data: string | object, options?: object | object | 解析数据为内部格式(抽象方法) |
write(features, options?) | features: object, options?: object | string | object | 将内部格式序列化为字符串/对象(抽象方法) |
GeoJsonFormat
GeoJSON 格式解析器。
构造函数
ts
class GeoJsonFormat extends Format {
readonly type = 'geojson';
constructor();
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
read(data) | data: string | object | object | 解析 GeoJSON(支持 JSON 字符串和对象) |
write(features) | features: object | object | 返回原始 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: string | object | 解析 KML XML 字符串 |
write(features) | features: object | string | 输出 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: string | object | 解析 GPX XML 字符串 |
write(features) | features: object | string | 输出 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 | object | object | 解析 Shapefile 数据 |
write(features) | features: object | object | 输出 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: string | object | 解析 WKT 字符串 |
write(features) | features: object | string | 输出 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 示例 |
|---|---|
| Point | POINT (116.397 39.908) |
| MultiPoint | MULTIPOINT ((0 0), (1 1)) |
| LineString | LINESTRING (0 0, 1 1, 2 2) |
| MultiLineString | MULTILINESTRING ((0 0, 1 1), (2 2, 3 3)) |
| Polygon | POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)) |
| MultiPolygon | MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0))) |
| GeometryCollection | GEOMETRYCOLLECTION (POINT (0 0), LINESTRING (0 0, 1 1)) |
CsvFormat
CSV 格式解析器,适用于含坐标列的表格数据。
构造函数
ts
class CsvFormat extends Format {
readonly type = 'csv';
constructor();
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
read(data) | data: string | object | 解析 CSV 字符串为 GeoJSON |
write(features) | features: object | string | 输出 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);格式对比
| 格式 | 类型 | 扩展名 | 读取 | 写入 | 适用场景 |
|---|---|---|---|---|---|
| GeoJson | geojson | .geojson, .json | 支持 | 支持 | Web 应用标准格式 |
| KML | kml | .kml | 支持 | 支持 | Google Earth |
| GPX | gpx | .gpx | 支持 | 支持 | GPS 轨迹 |
| SHP | shp | .shp | 支持 | 支持 | GIS 桌面软件 |
| WKT | wkt | — | 支持 | 支持 | 数据库/文本表示 |
| CSV | csv | .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)');