JS API ImageryLayer 重分類后的圖例標(biāo)注

1、需求描述
ArcGIS JS API ImageryLayer有豐富的功能,對(duì)柵格數(shù)據(jù)進(jìn)行重分類是非常普遍的需求。RasterFunction柵格函數(shù)就允許執(zhí)行重分類。

      const remapRF = new RasterFunction({
        functionName: "Remap",
        functionArguments: {
          // pixel values of forest categories are 41, 42, and 43
          // according to the  raster attribute table.
          // The InputRanges property defines the ranges of intial pixel values to remap
          // Three ranges: [0, 41], [41, 44], and [44, 255] are defined to extract forest pixels.
          inputRanges: [0, 41, 41, 44, 44, 255],
          // non-forest pixels (0-41 and 44-255) are remapped to a value of 1,
          // forest pixels (41-44) are remapped to a value of 2.
          outputValues: [1, 2, 3],
          // $$(default) refers to the entire image service,
          // $2 refers to the second image of the image service
          raster: "$$"
        }
      });

重分類后可以對(duì)柵格進(jìn)行顏色重新渲染。

      const colorRF = new RasterFunction({
        functionName: "Colormap",
        functionArguments: {
          colormap: [
            // non-forest pixels (value of 1) are assigned
            // a yellowish color RGB = [253, 254, 152]
            [1, 0, 0, 255],
            // forest pixels (value of 2) are assigned
            // a greenish color RGB = [2, 102, 6]
            [2, 0, 255, 0],
            [3, 255, 0, 0]


          ],
          // Setting the previous raster function to the Raster
          // property of a new raster function allows you to chain functions
          raster: remapRF
        },
        outputPixelType: "U8"
      });

效果如下:


image.png

2、問題出現(xiàn),在圖例中只是顯示了該柵格的數(shù)值數(shù)據(jù)123,未能正常中文顯示為業(yè)務(wù)標(biāo)注。
解決辦法,增加渲染規(guī)則,添加colormap 標(biāo)注和值對(duì)應(yīng)。

      const layer = new ImageryLayer({
        url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
        // apply the most recent raster function to the chain
        rasterFunction: colorRF,
        renderer: new RasterColormapRenderer({
          colormapInfos: [
            {
              color: [ 0, 0, 255],
              value: 1,
              label: "藍(lán)",
            },
            {
              color: [ 0, 255, 0],
              value: 2,
              label: "綠",
            },
            {
              color: [255, 0, 0],
              value: 3,
              label: "紅",
            },
          ]
        }
        ),
        mosaicRule: mosaicRule,
         effect: "saturate(125%) contrast(150%)"
      });
image.png

3、完整代碼如下:

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
  <title>ImageryLayer - raster function | Sample | ArcGIS Maps SDK for JavaScript 4.32</title>

  <link rel="stylesheet"  />
  <script src="https://js.arcgis.com/4.32/"></script>

  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/ImageryLayer",
      "esri/layers/support/RasterFunction", "esri/widgets/Legend", "esri/renderers/RasterColormapRenderer",
      "esri/layers/support/MosaicRule"
    ], (Map, MapView, ImageryLayer, RasterFunction, Legend, RasterColormapRenderer, MosaicRule) => {
      /**************************************************************************
       * Create image layer with client defined rendering rules and mosaic rules
       *************************************************************************/

      // Define the way overlapping images are mosaicked together
      const mosaicRule = new MosaicRule({
        ascending: true,
        method: "center",
        operation: "last"
      });

      // Defines a Remap raster function. Remap reclassifies pixel
      // values to new values. In this case we want to separate
      // two landcover types: forested areas and non-forested areas

      const remapRF = new RasterFunction({
        functionName: "Remap",
        functionArguments: {
          // pixel values of forest categories are 41, 42, and 43
          // according to the  raster attribute table.
          // The InputRanges property defines the ranges of intial pixel values to remap
          // Three ranges: [0, 41], [41, 44], and [44, 255] are defined to extract forest pixels.
          inputRanges: [0, 41, 41, 44, 44, 255],
          // non-forest pixels (0-41 and 44-255) are remapped to a value of 1,
          // forest pixels (41-44) are remapped to a value of 2.
          outputValues: [1, 2, 3],
          // $$(default) refers to the entire image service,
          // $2 refers to the second image of the image service
          raster: "$$"
        }
      });

      // The Colormap raster function adds color to each pixel
      // based on its pixel value
      const colorRF = new RasterFunction({
        functionName: "Colormap",
        functionArguments: {
          colormap: [
            // non-forest pixels (value of 1) are assigned
            // a yellowish color RGB = [253, 254, 152]
            [1, 0, 0, 255],
            // forest pixels (value of 2) are assigned
            // a greenish color RGB = [2, 102, 6]
            [2, 0, 255, 0],
            [3, 255, 0, 0]


          ],
          // Setting the previous raster function to the Raster
          // property of a new raster function allows you to chain functions
          raster: remapRF
        },
        outputPixelType: "U8"
      });

      const layer = new ImageryLayer({
        url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer",
        // apply the most recent raster function to the chain
        rasterFunction: colorRF,
        renderer: new RasterColormapRenderer({
          colormapInfos: [
            {
              color: [ 0, 0, 255],
              value: 1,
              label: "藍(lán)",
            },
            {
              color: [ 0, 255, 0],
              value: 2,
              label: "綠",
            },
            {
              color: [255, 0, 0],
              value: 3,
              label: "紅",
            },
          ]
        }
        ),
        mosaicRule: mosaicRule,
         effect: "saturate(125%) contrast(150%)"
      });

      /*************************
       * Add image layer to map
       ************************/

      const map = new Map({
        basemap: "gray-vector",
        layers: [layer]
      });

      const view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-84.1522, 35.793],
        zoom: 8
      });

      const legend = new Legend({
        view: view,
        container: "legendDiv"
      });
      view.ui.add("infoDiv", "bottom-left");
    });
  </script>
</head>

<body>
  <div id="infoDiv" class="esri-widget">
    <div id="legendDiv"></div>
  </div>
  <div id="viewDiv">


  </div>
</body>

</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容