-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathndvi.html
More file actions
105 lines (102 loc) · 3.29 KB
/
ndvi.html
File metadata and controls
105 lines (102 loc) · 3.29 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css"/>
<style>
#map {
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
z-index: 1;
}
#info {
background: rgba(20, 20, 20, 0.85);
color: lightblue;
left: 15%;
padding: 15px;
position: absolute;
top: 0;
right: 15%;
z-index: 2;
}
#info a {
color: lightblue;
}
</style>
</head>
<body>
<div id="info">
You are viewing a map created with <a href="http://leafletjs.com/">LeafletJS</a> and <a href="https://github.com/geotiff/georaster-layer-for-leaflet">GeoRasterLayer for Leaflet</a>.
<br/>
This demo is pulling from 2 64MB Landsat 8 GeoTIFFs, but takes less than 1 MB to load through the magic of <a href="https://www.cogeo.org/">Cloud Optimized GeoTIFFs</a>.
</div>
<div id="map"></div>
<script src="https://unpkg.com/chroma-js"></script>
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<script src="https://unpkg.com/georaster"></script>
<script src="https://unpkg.com/proj4"></script>
<script src="https:///unpkg.com/georaster-layer-for-leaflet@latest"></script>
<script>
// initalize leaflet map
var map = L.map('map').setView([0, 0], 5);
// add OpenStreetMap basemap
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
/*
NDVI is (Near-Infrared - Red) / (Near-Infrared + Red)
"In Landsat 8, NDVI = (Band 5 – Band 4) / (Band 5 + Band 4)." (USGS)
*/
const baseURL = "https://landsat-pds.s3.amazonaws.com/c1/L8/024/030/LC08_L1TP_024030_20180723_20180731_01_T1/LC08_L1TP_024030_20180723_20180731_01_T1_B.TIF";
const band4URL = baseURL.replace('B.TIF', 'B4.TIF');
const band5URL = baseURL.replace('B.TIF', 'B5.TIF');
const scale = chroma.scale([
'#C7BB95',
'#FEFEE1',
'#6E9F62',
'#032816',
'black'
]).domain([
0,
.2,
.4,
.6,
.8
]);
Promise.all([
parseGeoraster(band4URL),
parseGeoraster(band5URL)
]).then(georasters => {
var pixelValuesToColorFn = values => {
const [ RED, NIR ] = values;
const dividend = NIR - RED;
const divisor = NIR + RED;
let result;
if (dividend === 0 && divisor === 0) {
// probably no reading here
return 'rgba(0,0,0,0)';
}
if (dividend === 0 || divisor === 0) {
result = 0;
} else {
result = dividend === 0 ? 0 : dividend / divisor;
}
if (result <= 0.1) return 'blue';
if (result >= 0.8) return 'black';
return scale(result).hex();
};
var layer = new GeoRasterLayer({
georasters,
pixelValuesToColorFn,
resolution: 64,
opacity: 0.5
});
layer.addTo(map);
map.fitBounds(layer.getBounds());
});
</script>
</body>
</html>