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 🙏

© 2025 – Pkg Stats / Ryan Hefner

xgis-data2d

v0.0.3

Published

基于MapShaper 0.6.102版提取的矢量数据的导入导出工具

Downloads

13

Readme

xgis-data2d

xgis-data2d 矢量数据导入导出库

基于MapShaper 0.6.102版(https://mapshaper.org/) 开源项目抽取提炼,主要用于 2D矢量数据导入、导出。 支持Shapefile, GeoJSON, TopoJSON, KML and CSV formats are supported. Files can be zipped or gzipped.

(仅支持浏览器端使用)

更新说明

  • v0.0.3 解决导入kml文件时报“ Dynamic require of "@tmcw/togeojson" is not supported Import error”错误;
  • v0.0.2 为xgis-data2d增加导出saveZipFile,saveDataset,saveDatasetFromGeojson,getProj,transformDataset方法
    • saveDataset 保存数据集为多格式文件,默认以zip包导出
    • saveDatasetFromGeojson 导入geojson,并转为其他格式输出
    • getProj 定义投影对象
    • transformDataset 对数据集进行投影变换
    • saveZipFile 保存输出zip文件
  • v0.0.1 初步测试版本,支持2D矢量数据导入

使用说明

  • 安装库

    npm install xgis-data2d

  • 获取常用投影,或从https://epsg.gis.digsur.com查询EPSG来实例化投影

    import { getProj } from 'xgis-data2d';
    const p4326=await getProj('EPSG:4326');
    const p3857=await getProj('EPSG:3857');
    //自定义:ESPG:4521
    const p4521=await getProj('EPSG:4521',"+proj=tmerc +lat_0=0 +lon_0=99 +k=1 +x_0=33500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs");
    //或在线自动查询 ESPG:4521
    const p4521_2=await getProj('EPSG:4521');
  • 保存数据集,到文件,使用saveDataset

    /**

    • 保存数据集,到文件

    • @param {*} dataset 数据集

    • @param {*} type shapefile|geojson|topojson|json|dbf|csv|tsv|svg|kml

    • @param {*} isZip 是否压缩包

    • @param {} zipName 压缩包名称

    */

    function saveDataset(dataset, type, isZip = true, zipName = "test.zip")

​ 保存geojson

import { saveDataset} from 'xgis-data2d';
//保存geojson,单个文件
saveDataset(dataset,'geojson',false);
//以zip保存 geojson
saveDataset(dataset,'geojson',true);
//以zip保存 shp
saveDataset(dataset,'shapefile');
  • 拖拽导入文件,加载矢量图层
import {import2DFiles,internal} from 'xgis-data2d';
const {
  importContent,
  exportDatasetAsGeoJSON
} = internal;


async function dragFileHandler(fileList: FileList) {
  if (!fileList || fileList.length === 0)
    return;
  {
    const files = Array.from(fileList) 
    //导入文件为数据集
    const result = await import2DFiles(files);
    if (result && result.length > 0) {
      result.forEach(it => {
        console.log('00000000数据集:',it.dataset);
        //数据集转为geojson
        const content = exportDatasetAsGeoJSON(it.dataset, {})
        //********下面为openlayer加载矢量图层代码
        const data = new GeoJSON().readFeatures(content, { dataProjection: "EPSG:4326", featureProjection: "EPSG:3857" })
        const vectorSource = new VectorSource({
          features: data
        });
        const geojsonLayer = new VectorLayer({
          source: vectorSource,
        });
        if (geojsonLayer) {
          const xmap = Global.XMap as XMap;
          xmap.map.addLayer(geojsonLayer as any);
        }
      })
    }
  }
  • 导出geojson为ShapeFile压缩文件,使用方法:saveDataset

    import { import2DFiles, internal,saveDataset,saveDatasetFromGeojson,getProj,transformDataset } from 'xgis-data2d';
    import {get} from 'xframelib'
    const {
      importContent,
      exportDatasetAsGeoJSON,
      exportFileContent,
      zipAsync
    } = internal;
      
    async function handleExport() {
      const p = await get('SampleData/china.json');
      const dataset = importContent({ json: { filename: 'china.json', content: JSON.stringify(p.data) } })
      //https://epsg.gis.digsur.com/epsg/4326
      setDatasetCrsInfo(dataset, { prj: 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]' })
      
      saveDataset(dataset,'shapefile')
    }
  • openlayers矢量图层导出shapefile等格式(基于openlayers转投影)

    // format=shapefile|geojson|topojson|json|dbf|csv|tsv|svg

    import { internal,saveDataset,saveDatasetFromGeojson,getProj,transformDataset } from 'xgis-data2d';
    const {
      importGeoJSON
    } = internal
      
    let  geojsonLayer:VectorLayer;
    function handleExport2()
    {
       if(!geojsonLayer)
       {
        console.log('空图层!')
        return;
       }
      const features= geojsonLayer.getSource()?.getFeatures();
       //直接导出——经纬度4326
      const geoInfo= new GeoJSON().writeFeatures(features,{dataProjection:'EPSG:4326',featureProjection:'EPSG:3857'});
      const dataset=importGeoJSON(geoInfo,{});
       //直接导出——经纬度4326
       saveDatasetFromGeojson(geoInfo,'shapefile');
    }
  • openlayers矢量图层导出(transformDataset转投影)

import { internal,saveDataset,saveDatasetFromGeojson,getProj,transformDataset } from 'xgis-data2d';
const {
  importGeoJSON
} = internal

async function handleExport2()
{
   if(!geojsonLayer)
   {
    console.log('空图层!')
    return;
   }
   //默认为3857的
  const features= geojsonLayer.getSource()?.getFeatures();
  //dataset 为3857,先转投影再导出
  const geoInfo2= new GeoJSON().writeFeatures(features);
  
  const dataset2=importGeoJSON(geoInfo2,{});
  //转投影
  await transformDataset(dataset2,'EPSG:3857','EPSG:4326');
  if(dataset2)
  saveDataset(dataset2,'shapefile',true,'4326.zip');
}
  • Geojson保存为其他格式(先导入成dataset,再导出)

    /**
     * 将Geojson导入生成dataset,然后保存dataset为文件
     * @param {*} src string或json object
     * @param {*} type shapefile|geojson|topojson|json|dbf|csv|tsv|svg|kml
     * @param {*} isZip 是否压缩包
     * @param {*} zipName 压缩包名称,默认为test.zip
     */
    export function saveDatasetFromGeojson(
      src,
      type,
      isZip = true,
      zipName = "test.zip"
    )

    使用方式:

    import { saveDatasetFromGeojson} from 'xgis-data2d';
    import {get} from 'xframelib'
     const p = await get('SampleData/china.json');
    //将geojson转为shp
    saveDatasetFromGeojson(p.data,'shapefile');