-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad-symbolized-map.js
65 lines (58 loc) · 2.51 KB
/
ad-symbolized-map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* global L, adNdviData, dNdviSymbology */
'use strict'
/* Function to symbolize each Leaflet vector feature in the collection using a
predefined list of colors. */
const setupLegend = (feature, layer) => {
/* The NDVI values are represented on a scale of 0-1 while the legend we
have chosen for this example contains 255 whole-number values, so to compare
them we need to multiply each field's 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.
const [r, g, b] = adNdviSymbology[ndviVal];
/* Define a symbol definition object, 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 field.
layer.setStyle(symbology);
};
/* Function used to initialize a Leaflet map component with a polygon layer,
as described in detail in Tutorial 1 */
const setupMap = (fieldPolys, mbAccessToken) => {
const basemapUrl = 'http://api.tiles.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.png';
const map = L.map('map').setView([0, 0], 0);
L.tileLayer(`${basemapUrl}?access_token=${mbAccessToken}`).addTo(map);
fieldPolys.addTo(map);
map.fitBounds(fieldPolys.getBounds());
}
/ !!! PROGRAM BEGINS HERE !!! /
/* This tutorial builds on the basic data preparation and map setup process
as described in Tutorial 1.
This time, while defining the vector feature, we'll set a Mapbox option called
onEachFeature to apply a legend to the polygons based on their characteristics.
Because our data contains NDVI values for several dates, we will need to pick
which date to display. In this exampe, a constant defines the display date as
the 28th date in the index. */
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 call, and the function itself, are the only areas where we
diverge from the more detailed instructions in Tutorial 1. */
onEachFeature: setupLegend
});
const mbAccessToken = 'pk.eyJ1IjoiYXN0cm9kaWdpdGFsIiwiYSI6ImNVb1B0ZkEifQ.IrJoULY2VMSBNFqHLrFYew';
setupMap(fieldPolys, mbAccessToken);