leaft 动态配置渲染河流

动态配置渲染河流,颜色,宽度

版本1效果图:动态配置河流

 代码块

<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: Cookie
 * @Date: 2022-03-18 17:24:49
 * @LastEditors: Zhang
 * @LastEditTime: 2022-03-19 15:59:19
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin="" />
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script src="./river1.json"></script>

<style>
  .leaflet-marker-icon.leaflet-interactive {
    font-size: 20px;
  }
  .box {
    position: absolute;
    top: 50px;
    left: 50px;
    /* width: 500px;
    height: 100px; */
    z-index: 10000;
  }
  .my-div-icon {
    display: none;
  }
  .el-select {
    width: 140px;
  }
</style>
<div id="app">
  <div id="map" style="width: 100vw; height: 100vh"></div>
  <div class="box">
    <span>请配置渲染河流:</span>
    <el-select v-model="value">
      <el-option
        v-for="item in options.showRiverName"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <span>请配置河流宽度:</span>
    <el-select v-model="width">
      <el-option
        v-for="item in widthOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <span>请配置河流颜色:</span>
    <el-select v-model="color">
      <el-option
        v-for="item in colorOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
  </div>
</div>
</head>
<body>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
 <script>


var map,mylayerGroup,color ="#306bff";
new Vue({
  el: '#app',
  data: {
    value: '新凯河',
    width: 6,
    color: color,
    options: {
      showRiverName: [
      {value:'松花江',label: '松花江'},
      {value:'新凯河',label: '新凯河'},
      {value:'饮马河',label: '饮马河'},
      ]
    },
    widthOptions: [
      {value:'1',label: '1'},
      {value:'6',label: '6'},
      {value:'12',label: '12'},
    ],
    colorOptions: [
      {value:'#306bff',label: '蓝色'},
      {value:'red',label: '红色'},
    ]
  },
  mounted() {
    this.init()
  },
  methods: {
      init() {
        map = L.map('map').setView([30.3367385888597, 120.135198302847], 9);
        var wpUrl = 'http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0';
        L.tileLayer(wpUrl, {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);
        map.setView([43.88, 125.35])
        this.addRiver(this.value)
      },
      addRiver(val) {
        let optoins = {
            hidRiverName: []
        } 
        this.options.showRiverName.forEach(el => {
          if(el.label !== val) {
            optoins.hidRiverName.push(el.label)
          }
        });
        let width = this.width
        console.log(optoins.hidRiverName)
        let layer1  = L.geoJSON(river, {
              id: "RIVER_COLOR",
              style: function(geoJsonFeature) {
                // console.log(geoJsonFeature)
                let color = "#306bff"
                let color1 = "rgba(0,0,0,0)"
                let nameClass=geoJsonFeature.properties.DJ * 1>4?"fz24":"fz24"
                geoJsonFeature.properties.name = geoJsonFeature.properties.SSHL // 自定义name
                if (optoins.hidRiverName && optoins.hidRiverName.indexOf(geoJsonFeature.properties.SSHL) != -1) {
                  color = color1
                  addText(geoJsonFeature.properties)
                } else {
                  addText(geoJsonFeature.properties, nameClass)
                }
                // if (optoins.showRiverName) {
                //   if (optoins.showRiverName.indexOf(geoJsonFeature.properties.SSHL) != -1) {
                //     addText(geoJsonFeature.properties, nameClass)
                //     color = "#306bff"
                //   } else {
                //     color = "rgba(0,0,0,0)"
                //   }
                // }
                if (optoins.hidRiverLevel && optoins.hidRiverLevel.indexOf(geoJsonFeature.properties.DJ * 1) != -
                  1) {
                  color = "rgba(0,0,0,0)"
                }
                console.log(color)
                
                return {
                  fillColor: "#1100d6",
                  fillOpacity: 0.4,
                  // weight: getRiverWidth(geoJsonFeature.properties.HLDJ),
                  weight: width,
                  color: getColor(color)
                }
              }
        }).on("click", function(e) {
          console.log(e)
        })
        //  把这一组图层Layer放到同一个图层组layerGroup
        mylayerGroup = L.layerGroup([layer1])
        // 把图层组layerGroup添加到地图
        map.addLayer(mylayerGroup)
      },
      
  },
  watch: {
    value(newval,oldval) {
      mylayerGroup.clearLayers() // 删除图层
      this.addRiver(newval)
    },
    width(newval,oldval) {
      mylayerGroup.clearLayers()
      this.addRiver(this.value)
    },
    color() {
      mylayerGroup.clearLayers()
      color = this.color
      this.addRiver(this.value)
    }
  } 
})

function getColor(val) {
  if(val !=='rgba(0,0,0,0)') {
    // console.log(color)
    return color
  }else {
    return color1 = 'rgba(0,0,0,0)'
  }
}




function getRiverWidth(level) { //获取河流宽度
  let width = 12
  switch (level) {
    case "一级河流":
      width = 12
      break;
    case "二级河流":
      width = 10
      break;
    case "三级河流":
      width = 8
      break;
    case "四级河流":
      width = 6
      break;
    case "五级河流":
      width = 4
      break;
    case "六级河流":
      width = 4
      break;
    case "七级河流":
      width = 4
      break;
  }
  return width
}


function addText(data, className) { // 标记河流名字
  console.log(data)
  debugger
  
  let myIcon = L.divIcon({
    html: data.name,
    className: className ? className : 'my-div-icon',
    iconSize: 60,
  });
  let center = data.centroid ? data.centroid : data.center
  let marker = L.marker([center[1], center[0]], {
    icon: myIcon
  })
  map.addLayer(marker);
}

 </script>
</body>
</html>

river1.json数据:https://github.com/zhanghenxiao/leaflet.git

 版本2效果图:动态配置河流及添加省份渲染

 

<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: Cookie
 * @Date: 2022-03-18 17:24:49
 * @LastEditors: Zhang
 * @LastEditTime: 2022-03-19 15:59:19
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin="" />
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script src="./data.json"></script>
<script src="./river1.json"></script>

<style>
  .leaflet-marker-icon.leaflet-interactive {
    font-size: 20px;
  }
  .box {
    position: absolute;
    top: 50px;
    left: 50px;
    /* width: 500px;
    height: 100px; */
    z-index: 10000;
  }
  .my-div-icon {
    display: none;
  }
  .rgba{
    color: rgba(0, 0, 0, 0);
  }
  .black {
    color: black;
  }
  .el-select {
    width: 140px;
  }
</style>
<div id="app">
  <div id="map" style="width: 100vw; height: 100vh"></div>
  <div class="box">
    <span>请配置渲染河流:</span>
    <el-select v-model="value">
      <el-option
        v-for="item in options.showRiverName"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <span>请配置河流宽度:</span>
    <el-select v-model="width">
      <el-option
        v-for="item in widthOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <span>请配置河流颜色:</span>
    <el-select v-model="color">
      <el-option
        v-for="item in colorOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
  </div>
</div>
</head>
<body>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
 <script>


var map,mylayerGroup,mylayerGroup2,mylayerGroup21,mylayerGroup22,
mylayerGroup3,color ="#306bff",isZoom=true,isClick=false,layerMarkers=[];
let style = {
  fillColor: "#e5dada",
  fillOpacity:.4,
  weight: 1 ,
  color: "#306bff"
}  
let style2 = {
  fillColor: "yellow",
  fillOpacity: 0.4,
  color: 'yellow'
}
function highlightFeature(e) {
  if(mylayerGroup3) {
      mylayerGroup3.clearLayers()
  }
  let layer2 = L.geoJSON(e.target.feature, {
      style: function (feature) {
          return {
          fillColor: "yellow",
          fillOpacity: 0.4,
          color: 'yellow',
        };
      }
  }).on('mouseover',function(e){
  var popup = L.popup();
      popup.setLatLng([e.latlng.lat,e.latlng.lng])
          .setContent(`<span>name:</span> <span>${e.layer.feature.properties.name}</span>`)
          .openOn(map);
  })
  mylayerGroup3 = L.layerGroup([layer2])
  map.addLayer(mylayerGroup3)
  // console.log(e);
  if(e.target.feature.properties.name === '吉林省') {
    mylayerGroup21.clearLayers()
    layerMarkers.clearLayers()
    isZoom =false
    vue.addRiver(vue.value,'yellow')
  } 
}

function onEachFeature(feature, layer) {
    layer.on({
      mouseover: highlightFeature,
    });
}
let vue = new Vue({
  el: '#app',
  data: {
    value: '松花江',
    width: 12,
    color: color,
    options: {
      showRiverName: [
      {value:'松花江',label: '松花江'},
      {value:'新凯河',label: '新凯河'},
      {value:'饮马河',label: '饮马河'},
      ]
    },
    widthOptions: [
      {value:'1',label: '1'},
      {value:'6',label: '6'}, 
      {value:'12',label: '12'},
    ],
    colorOptions: [
      {value:'#306bff',label: '蓝色'},
      {value:'red',label: '红色'},
    ]
  },
  mounted() {
    this.init()
  },
  methods: {
      init() {
        map = L.map('map').setView([29.40268, 106.54041], 5);
        var wpUrl = 'http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0';
        L.tileLayer(wpUrl, {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);
        let geoJsonLayer = L.geoJson(data, {
          style,  
          onEachFeature: (feature, layer) => {//onEachFeature回调  关键方法
            if (onEachFeature) {
              onEachFeature(feature, layer); }
           }
        }).addTo(map)
        this.addRiver(this.value)
      },
      addRiver(val) {
          let width = this.width
          let layerName = val         
          let layerColor =  isClick? 'yellow': this.color
          let layer1  = L.geoJSON(river, {
                id: "RIVER_COLOR",
                onEachFeature: (feature, layer) => {//onEachFeature回调  关键方法
                this.riverOnEachFeature(feature, layer,layerName,width,layerColor);
                }
             
        })
         //  把这一组图层Layer放到同一个图层组layerGroup
        //  mylayerGroup = L.layerGroup([layer1])
        // // // 把图层组layerGroup添加到地图
        // map.addLayer(mylayerGroup)
        // console.log('getBounds');
        // console.log(layer1.getBounds()); 
        

      },

      riverOnEachFeature(feature, layer,layerName,width,color) {
        let nameClass=''
          if(feature.properties.SSHL !== layerName) {
            let layer1 = L.geoJSON(feature, {
              style: function (feature) {
                  return {
                  fillColor: "red",
                  fillOpacity: 0.4,
                  color: 'red',
                };
              }
            }).on("click", function(e) {
          console.log(e)
          let layer2 = L.geoJSON(e.layer.feature, {
              style: function (feature) {
                console.log('feature');
                console.log(feature);
                  return {
                  fillColor: "rgba(0,0,0,0)",
                  fillOpacity: 0.4,
                  color: 'rgba(0,0,0,0)',};
              }
          }).bindPopup(function (layer) {
            return  `<h1>${layer.feature.properties.SSHL}</h1>`
          })

        }).bindPopup(function (layer) {
            return  `<h1>${layer.feature.properties.SSHL}</h1>`
        }) // 不显示其他河流则不添加到图层就行
        // mylayerGroup2 = L.layerGroup([layer1])
        // map.addLayer(mylayerGroup2)   
        // console.log(11);
        
        }else {
          let layer1 = L.geoJSON(feature, {
              style: function (feature) {
                // console.log(feature);
                addText(feature.properties, nameClass='black')
                  return {
                  fillColor: color,
                  fillOpacity: 0.4,
                  weight: width,
                  color: color,
                };
              }
          }).on("click", function(e) {
          isClick = true
          let layer2 = L.geoJSON(e.layer.feature, {
              style: function (feature) {
                console.log('feature');
                console.log(feature);
                  return {
                    fillColor: "yellow",
                    fillOpacity: 0.4,
                    weight: 12,
                    color: 'yellow'};
              }
          }).bindPopup(function (layer) {
            return  `<h1>${layer.feature.properties.SSHL}</h1>`
          })
          mylayerGroup22 = L.layerGroup([layer2])
          map.addLayer(mylayerGroup22)
          // console.log(e)
        }).bindPopup(function (layer) {
            return  `<h1>${layer.feature.properties.SSHL}</h1>`
        })
        let bounds = layer1.getBounds() // 返回地图视图的经纬度边界
        console.log(bounds);
        if(isZoom){
          map.flyToBounds([
            [bounds._southWest.lat, bounds._southWest.lng],
            [bounds._northEast.lat, bounds._northEast.lng]
          ]) 
        }
        mylayerGroup21 = L.layerGroup([layer1])
        map.addLayer(mylayerGroup21)           
        }
        
      },
      

      
  },
  watch: {
    value(newval,oldval) {
      this.value = newval
      if(mylayerGroup21) {
         mylayerGroup21.clearLayers()
      }
      if(mylayerGroup22) {
         mylayerGroup22.clearLayers()
      }
      layerMarkers.clearLayers()
      isZoom = true
      isClick = false
      this.addRiver(this.value)
    },
    width(newval,oldval) {
      this.width = newval
      // mylayerGroup.clearLayers()
      if(mylayerGroup21) {
         mylayerGroup21.clearLayers()
      }
      layerMarkers.clearLayers()
      isZoom = true
      isClick = false
      this.addRiver(this.value)
    },
    color(newval,oldval) {
      this.color = newval
      // mylayerGroup.clearLayers()
      if(mylayerGroup21) {
         mylayerGroup21.clearLayers() // 删除图层
      }
      layerMarkers.clearLayers()
      color = this.color
      isZoom = true
      isClick = false
      this.addRiver(this.value)
    }
  } 
})

function getColor(val) {
  if(val !=='rgba(0,0,0,0)') {
    // console.log(color)
    return color
  }else {
    return color1 = 'rgba(0,0,0,0)'
  }
}


function addText(data, className) {
  let myIcon = L.divIcon({
    html: data.SSHL,
    className: className ? className : 'my-div-icon',
    iconSize: 60,
  });
  let center = data.centroid ? data.centroid : data.center
  let marker = L.marker([center[1], center[0]], {
    icon: myIcon
  })
  layerMarkers = L.layerGroup([marker])  //  设置marker 图层组,方便图层管理
  map.addLayer(layerMarkers);
  }
 </script>
</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PCL(Point Cloud Library)是一个开源的、跨平台的点云处理框架,它提供了大量的点云处理算法和工具,其中之一就是 Out-of-Core(OOC)模块。OOC模块可以在内存不足的情况下处理大规模点云数据,它将点云数据分成多个块(chunks)进行处理,每个块可以被存储到硬盘上,不需要全部加载到内存中。 Out-of-Core模块的源代码位于PCL库的pcl/outofcore目录下,它包含了多个类和文件,其中最核心的是OutofcoreOctree类。下面是OutofcoreOctree类的主要代码结构和功能解析: 1. 类定义: ``` template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeBaseNode; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeAbstractNode; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeDiskContainer; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeRamContainer; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeBase; template<typename PointT, typename LeafT> class OutofcoreOctreeBaseNode<OutofcoreOctreeBase<OutofcoreOctreeDiskContainer<OutofcoreOctreeBaseNode<OutofcoreOctreeDiskContainer<PointT>, PointT, LeafT>, PointT, LeafT> >, PointT, LeafT>; template<typename PointT, typename LeafT> class OutofcoreOctreeBaseNode<OutofcoreOctreeBase<OutofcoreOctreeRamContainer<OutofcoreOctreeBaseNode<OutofcoreOctreeRamContainer<PointT>, PointT, LeafT>, PointT, LeafT> >, PointT, LeafT>; template<typename PointT, typename LeafT> class OutofcoreOctreeBase<OutofcoreOctreeDiskContainer<OutofcoreOctreeBaseNode<OutofcoreOctreeDiskContainer<PointT>, PointT, LeafT>, PointT, LeafT> >; template<typename PointT, typename LeafT> class OutofcoreOctreeBase<OutofcoreOctreeRamContainer<OutofcoreOctreeBaseNode<OutofcoreOctreeRamContainer<PointT>, PointT, LeafT>, PointT, LeafT> >; template<typename PointT, typename LeafT> class OutofcoreOctreeDiskContainer<OutofcoreOctreeBaseNode<OutofcoreOctreeDiskContainer<PointT>, PointT, LeafT>, PointT, LeafT>; template<typename PointT, typename LeafT> class OutofcoreOctreeRamContainer<OutofcoreOctreeBaseNode<OutofcoreOctreeRamContainer<PointT>, PointT, LeafT>, PointT, LeafT>; template<typename PointT, typename LeafT> class OutofcoreOctreeDiskContainer<boost::shared_ptr<OutofcoreOctreeBaseNode<OutofcoreOctreeDiskContainer<PointT>, PointT, LeafT> >, PointT, LeafT>; template<typename PointT, typename LeafT> class OutofcoreOctreeRamContainer<boost::shared_ptr<OutofcoreOctreeBaseNode<OutofcoreOctreeRamContainer<PointT>, PointT, LeafT> >, PointT, LeafT>; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeBaseNode { }; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeAbstractNode { }; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeDiskContainer { }; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeRamContainer { }; template<typename Container, typename PointT, typename LeafT> class OutofcoreOctreeBase { }; ``` 2. OutofcoreOctreeBaseNode类: OutofcoreOctreeBaseNode是OutofcoreOctree模块的最基本的节点类型,它是一个模板类,需要指定点云数据容器类型(Container)、点云类型(PointT)和叶子节点类型(LeafT)。OutofcoreOctreeBaseNode类定义了节点的基本属性和行为,例如节点的索引、节点的深度、节点的父节点和子节点等。OutofcoreOctreeBaseNode还提供了一些方法,如添加点云数据、获取包围盒等。 3. OutofcoreOctreeAbstractNode类: OutofcoreOctreeAbstractNode类是OutofcoreOctreeBaseNode类的基类,它定义了纯虚函数,需要在子类中实现。OutofcoreOctreeAbstractNode类主要提供了节点的一些抽象方法,如获取节点的包围盒、获取节点的点云数据等。 4. OutofcoreOctreeDiskContainer类和OutofcoreOctreeRamContainer类: OutofcoreOctreeDiskContainer类和OutofcoreOctreeRamContainer类是点云数据容器类,它们分别用于处理硬盘存储和内存存储的点云数据。这两个类都继承自OutofcoreOctreeAbstractNode类,因此都需要实现OutofcoreOctreeAbstractNode类中的纯虚函数。OutofcoreOctreeDiskContainer类和OutofcoreOctreeRamContainer类都提供了一些方法,如添加点云数据、获取包围盒等。 5. OutofcoreOctreeBase类: OutofcoreOctreeBase类是OutofcoreOctree模块的主要类,它是一个模板类,需要指定点云数据容器类型(Container)、点云类型(PointT)和叶子节点类型(LeafT)。OutofcoreOctreeBase类封装了OutofcoreOctreeBaseNode类和OutofcoreOctreeAbstractNode类的一些方法,提供了一些高级的功能,如点云数据的读取、写入、压缩、解压等。OutofcoreOctreeBase类还提供了一些方法,如获取节点、获取包围盒等。 总体来说,Out-of-Core模块的源代码结构比较复杂,涉及到多个类和文件,但其核心代码就是OutofcoreOctree类。OutofcoreOctree类的实现非常精简和高效,它通过将点云数据分块存储到硬盘上,实现了对大规模点云数据的高效处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值