geojson 与 wkt、kml、shpfile 互转

42 阅读1分钟

缘起

shpfile的读写库很凌乱,通常需要安装两个包,而且mapbox家的shpfile写库不支持中文写入(浏览器默认编码不支持gbkgb2312,他们也懒的改)所以我就从头写了一遍,读写全有,扩充wkt、kml读写支持。

links

NPM License NPM Version NPM Downloads NPM Last Update

待开发

  • kml 支持样式导出、导入
  • kml 支持feature属性导入,可以是嵌入的html
  • shpfile 强制crs
  • shpfile 强制编码

使用

安装

yarn add geojson-converter-kit

# 或使用npm
npm install geojson-converter-kit

wkt

import { WKT } from 'geojson-converter-kit';

const wktStr = WKT.stringify({
  type: "Point",
  coordinates: [100.0, 0.0],
});

const geometry = WKT.parse(wktStr);

kml

import { KML } from "geojson-converter-kit";

const kmlStr = KML.stringify([
  {
    type: "Feature",
    properties: {},
    geometry: {
      type: "Point",
      coordinates: [100.0, 0.0],
    },
  },
]);

const features = KML.parse(kmlStr);

shpfile

import { SHPFILE } from "geojson-converter-kit";

const zipBlob = await SHPFILE.writeToZip([
  {
    type: "Feature",
    properties: {},
    geometry: {
      type: "Point",
      coordinates: [100.0, 0.0],
    },
  },
]);

const featureMaps = await SHPFILE.readFromZip(zipBlob);