geojson生成图片_ JS【聪明人都知道的】

本文介绍了如何使用JavaScript库如geojson2svg将GeoJSON数据转换为SVG路径,并通过示例展示了如何根据给定的GeoJSON和自定义选项生成SVG地图,包括坐标转换、SVG元素创建和布局调整。
摘要由CSDN通过智能技术生成

效果如下
如图

// 安装的依赖
npm install geojson2svg
npm install reproject
npm install proj4

dom 如下
在这里插入图片描述
js 如下

/**
 * @param {*} geojson = {"type": "FeatureCollection","crs": {
    "type": "name","properties": {"name": "EPSG:4326"}},"features": [ {"type": "Feature","geometry": 
    {"type": "Polygon","coordinates": [
      [[108.153213989268,41.08685720641945],[109.32264273102045,24.027757043029396],[121.53887198705156,26.069893222620774],[118.50246904763671,40.027401130129945],[108.153213989268,41.08685720641945]]]}}],"properties":{"type": "building","height":3 ,"id": "fide53sk_-0"}}
 * @param {*} options   {
          viewportSize: {width:78,height:56}, // svg 大小
          mapExtent:  { // 墨卡托投影坐标 包围盒
            left: 11039560.707265345, // minx
            right: 13529645.3393041,    
            bottom: 2756790.790458125,
            top: 5025161.534985688 },// maxy
          attributes: {
            'style': 'stroke:#006600; fill: #F0F8FF;stroke-width:0.5px;',
            'vector-effect':'non-scaling-stroke'
          },
          explode: false
        }
 */
function drawGeoJSON (geojson, options) {
	// 坐标转换成EPSG:3857
    var geojson3857 = reproject.reproject(JSON.parse(geojson),'EPSG:4326','EPSG:3857',proj4.defs);
    // var svgMap = document.getElementById(domId);
    var convertor = geojson2svg(options);
    var svgElements = convertor.convert(geojson3857);
     
    let arrPath = []
    svgElements.forEach(function(svgStr) {
     arrPath.push(svgStr)
        // var svg = parseSVG(svgStr);
        // svgMap.appendChild(svg);
    });
    return arr
}
// 生成svg
function parseSVG(s, id) {
    // var svgMap = document.getElementById(domId);
    var div= document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
    div.innerHTML= '<svg xmlns="http://www.w3.org/2000/svg" style="border: 1px solid">'+s+'</svg>';
    var frag= document.createDocumentFragment();
    while (div.firstChild.firstChild)
        frag.appendChild(div.firstChild.firstChild);
    return frag;
}```

// ** 具体使用 
//content就是你的geojson
let num = Math.floor(Math.random() * 25)  % colors.length
let colors = ['#705ca2','#32a9af','#3cc177','#ec985d','#71a05d','#81b7ec']
let color = colors[num]
let options = {
    viewportSize: {width:70, height:50}, // svg 大小
    mapExtent:  item.mapExtent,
    attributes: {
      'style': 'stroke:#ffffff; fill: '+color+'; stroke-width:2px;',
      'vector-effect':'non-scaling-stroke'
    },
    explode: false
}
let pathArr = drawGeoJSON(content, options)
let that = this

pathArr.forEach((element,index) => {
	// 给map + index 生成svg
  that.parseSVG(element,'map' + index)
  // 获取path 大小
  let svgMap = document.getElementById('map' + index).children[0].getBBox()
  let width = svgMap.width > 78 ? 78 : svgMap.width
  let height = svgMap.height > 56 ? 56 : svgMap.height
  
  // document.getElementById('map' + index).style.width = width
  // document.getElementById('map' + index).style.height = height
  let top = ((56 - height) / 2)  - 15 > 0 ? ((56 - height) / 2)  - 10 : 0
  let left = ((78 - width) / 2) - 10  > 0 ? ((78 - width) / 2) - 10 : 0
  
  // 为了使生成的svg 在div 居中
  document.getElementById('map' + index).style.top = (56 - height) / 2 
  document.getElementById('map' + index).style.left = (78 - width) / 2
});

参考链接如下:
geojson2svg 案例
npm geojson2svg npm官方说明
如何用 JS 获取 SVG 内 path 元素中的图形的实际位置及尺寸?

参考源:
geojson大杀器
Node.js 笔记二:入门及GeoNode.js GIS相关库

方案2:geojsonTosvg
geojsonTosvg 案例 (这个挺好的_后续优化可能会选择它)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值