openlayer加载远程shp文件

项目场景:

openlayer 请求加载远程.shp文件

安装引入shpjs

:需要用到shpjs 这个库去解析请求过来的文件
npm i shpjs

import shp from 'shpjs'; // shp转geoJson


解析方法:

用shp.parseShp方法解析buffer文件, 返回值promise类型

const loadSHPData = async url => {
  const response = await fetch(url);
  const buffer = await response.arrayBuffer();
  return shp.parseShp(buffer);
};

完整代码:

import { onMounted } from 'vue';
import { Vector as VectorLayer } from 'ol/layer';
import { Vector as VectorSource } from 'ol/source';
import { Style, Circle, Fill, Stroke } from 'ol/style';
// 创建图层
const polygonLayer = new VectorLayer({
  source: new VectorSource(),
  zIndex: 10,
  style: new Style({
    fill: new Fill({
      color: 'blue',
    }),
  }),
});

const initPolygonLayer = () => {
  const url = `你的http请求地址`;
  loadSHPData(url).then(res => {
    // 这里对coord进行转换
    const data = handleData(res); 
    // 一般返回的是geojson格式的,但是接口返回的是geometry数组 需要手动转为geojson格式
    const features = new GeoJSON().readFeatures({
      type: 'FeatureCollection',
      features: data.map(geometry => ({
        type: 'Feature',
        geometry,
      })),
    });
    polygonLayer.getSource().addFeatures(features);
    const extent = polygonLayer.getSource().getExtent();
    map.getView().fit(extent);
    map.addLayer(polygonLayer);
  });
};

// 处理数据 将32651转为4326
const handleData = res => {
  return res.map(i => {
    const arr = i.coordinates[0].map(j => {
      return transform(j, 'EPSG:32651', 'EPSG:4326');
    });
    return {
      type: i.type,
      coordinates: [arr],
    };
  });
};

onMounted(() => {
  initPolygonLayer();
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值