Giter VIP home page Giter VIP logo

example-ndvi-vector-symbology's Introduction

Symbolized Astro Digital NDVI Vector Map

Demonstration of this code

Overview

This tutorial describes extracting vector data from the return of Astro Digital's NDVI values API, and using it with the Leaflet Javascript framework to render a symbolized map representing NDVI intensity. Be sure to check out the Basic Astro Digital NDVI vector map setup tutorial, which describes the basics of map initialization in more detail. Other topics in this series include:

Summary

Begin by creating a FeatureCollection from the results attribute of Astro Digital's NDVI values API response, as described in the basic tutorial. This time, we will include a call to Leaflet's onEachFeature method in the the construction of the vector feature, which will allow us to evaluate the values of each polygon and apply a legend accordingly. Because the example data contains NDVI values for a range of dates, we will also need to pick a specific date to symbolize by referencing its index.

// Choose date index to use.
const dateIndex = 27;
let fieldPolys = L.geoJson({
  'type': 'FeatureCollection',
  'features': adNdviData.results.map((field) => {
    const id = field.id;
    field = field.value;
    field.properties.id = id;
    return field;
  })
}, {
  // This function is the only new item not in the introductory tutorial
  onEachFeature: setupLegend
});

The setupLegend function called by onEachFeature will be written to map the NDVI values of each feature in the collection to the colors defined in our external legend file, as shown below:

const setupLegend = (feature, layer) => {
  // The NDVI values are represented on a scale of 0-1 while the legend we
  // have developed for this example contains 255 whole-number values. To compare
  // them, we will need to multiply each feature's NDVI value by 255.
  const ndviVal = Math.floor(
    feature.properties.ndvi_values[dateIndex].value * 255);
  // Find the RGB legend properties which match the active field's NDVI value, and
  // map them to variables called r, g, and b.
  const [r, g, b] = adNdviSymbology[ndviVal];
  // Define a symbol definition object in Leaflet syntax, where the polygon's
  // fill matches the RGB values listed in the legend file, and its outline
  // (color) attribute's RGB values are boosted slightly higher to add contrast.
  const symbology = {
    color: `rgb(${r + 50},${g + 50},${b + 50})`,
    weight: 2,
    opacity: 1,
    fillOpacity: 0.8,
    fillColor: `rgb(${r},${g},${b})`
  };
  // Apply the symbol definition to each feature.
  layer.setStyle(symbology);
};

With the features now appropriately symbolized, we are able to initialize the map using the same code as described in the basic tutorial.

Check out the source code for more details!

Legend Definition

There are several ways a legend could be generated, including creating a function for dynamic gradient generation or logic to assign a more limited palette of colors based on binning of the NDVI values. In this example, we will define a legend in an external file, where intensity categories numbering 0 to 256 are associated with red, green, and blue values in a Javascript object. In addition to importing the NDVI values external data, we will also import the NDVI values response. The example program itself is located in the ad-symbolized-map.js file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.