You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用mapbox/shp-write转换GeoJson LineString为ShapeFile的异常问题

解决mapbox/shp-write转换LineString要素时合并为单条线的问题

问题场景

使用mapbox/shp-write将包含多组LineString/MultiLineString要素的GeoJSON转换为ShapeFile时,所有线要素被合并成单条线,仅保留第一组要素的属性;但Point类型要素的转换完全正常。

可行解决方案

1. 升级至最新版shp-write

旧版本的mapbox/shp-write存在线要素批量处理的bug,先更新依赖到最新版本:

npm update @mapbox/shp-write

若为CDN引入,替换为对应最新版本的CDN链接即可。

2. 移除polyline自定义类型配置

尝试删除options.types中的polyline配置项,避免自定义类型逻辑导致要素被错误合并:

const options = {
  folder: "my_internal_shapes_folder",
  filename: "myFileName" + "-" + "Shp",
  outputType: "blob",
  compression: "DEFLATE",
  types: {
    point: "pointName",
    polygon: "polygonName"
  },
};

如果不需要自定义要素类型,直接移除整个types对象效果更直接:

const options = {
  folder: "my_internal_shapes_folder",
  filename: "myFileName" + "-" + "Shp",
  outputType: "blob",
  compression: "DEFLATE"
};

3. 关闭压缩选项排查

暂时关闭压缩配置,验证是否是压缩流程导致的异常:

const options = {
  folder: "my_internal_shapes_folder",
  filename: "myFileName" + "-" + "Shp",
  outputType: "blob",
  compression: null
};

4. 拆分要素单独处理(备选方案)

若以上方法均无效,可将FeatureCollection拆分为单个Feature分别导出,之后手动合并ShapeFile文件:

data.features.forEach((feature, index) => {
  const singleFeatureSet = {
    type: "FeatureCollection",
    features: [feature]
  };
  const singleOptions = {
    ...options,
    filename: `myFileName-${index}-Shp`
  };
  shpwrite.download(singleFeatureSet, singleOptions);
});

前置验证

确认你的GeoJSON格式完全合规:每个LineString的coordinates为二维数组,每个Feature的type和geometry.type定义正确,无语法错误(如逗号遗漏、括号不匹配)。

内容的提问来源于stack exchange,提问作者Ajaj Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:20:21