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

如何在Node.js中读取ShapeFile属性表?使用shapefile包仅获坐标

解决Shapefile属性读取问题

你当前的代码其实已经拿到了包含属性的完整记录,只是没明确提取properties字段。shapefile包读取的每条记录result.value包含两个关键部分:

  • geometry: 存储地理图形坐标
  • properties: 存储Shapefile的属性信息(对应dbf文件中的数据)

修改后的代码示例

可以直接在处理时明确获取属性,或者保留完整记录结构:

const shapefile = require("shapefile");

let MyData = [];
shapefile.open(addr)
  .then(source => source.read()
    .then(function log(result) {
      if (result.done) return;
      // 直接拆分出坐标和属性
      const { geometry, properties } = result.value;
      console.log("地理坐标:", geometry);
      console.log("属性信息:", properties);
      // 将完整数据存入数组,包含坐标与属性
      MyData.push({ geometry, properties });
      return source.read().then(log);
    }))
  .catch(error => console.error(error.stack))
  .then(() => {
    res.send(MyData);
  });

注意事项

  • 确保.dbf文件和.shp文件在同一目录下,shapefile包会自动读取同路径的dbf文件来获取属性,缺少dbf文件则无法拿到属性数据。
  • 如果属性包含中文等非UTF-8编码内容,可在shapefile.open时指定编码参数,比如shapefile.open(addr, { encoding: "gbk" })来适配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:15:09