-
Notifications
You must be signed in to change notification settings - Fork 0
/
planetscope_batch_NDVI_of_digital_number.py
81 lines (56 loc) · 2.11 KB
/
planetscope_batch_NDVI_of_digital_number.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed May 31 18:14:00 2017
https://developers.planet.com/tutorials/calculate-ndvi/
@author: YARON
"""
#del all DN_udm by hand do seacrh insde the dir and del
import rasterio
import numpy
from xml.dom import minidom
"D:\\TESTPATCH\NDVI\\"
import os
rootdir = "D:\\TESTPATCH\\"
#rootdir = r'D:\TESTPATCH'
d=rootdir
dir_list =filter(lambda x: os.path.isdir(os.path.join(d, x)), os.listdir(d))
coeffs = {}
#files = os.listdir(os.curdir)
for dir0 in dir_list:
s = os.chdir(rootdir+dir0)
#get file in working dir
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for name_file in files:
if name_file[-3:] == "tif":
print name_file
A = name_file[:-3]
xml_file_name = name_file[:-4] + "_metadata.xml"
with rasterio.open(name_file) as src:
band_red = src.read(2)
with rasterio.open(name_file) as src:
band_nir = src.read(3)
xmldoc = minidom.parse(xml_file_name)
nodes = xmldoc.getElementsByTagName("ps:bandSpecificMetadata")
# XML parser refers to bands by numbers 1-4
coeffs = {}
for node in nodes:
bn = node.getElementsByTagName("ps:bandNumber")[0].firstChild.data
#if bn in ['1', '2', '3', '4']:#
if bn in ['1', '2', '3', '4']:#
print bn
i = int(bn)
value = node.getElementsByTagName("ps:reflectanceCoefficient")[0].firstChild.data
coeffs[i] = float(value)
# Multiply by corresponding coefficients
band_red = band_red * coeffs[3]
band_nir = band_nir * coeffs[4]
# Allow division by zero
numpy.seterr(divide='ignore', invalid='ignore')
# Calculate NDVI
ndvi = (band_nir.astype(float) - band_red.astype(float)) / (band_nir + band_red)
# Set spatial characteristics of the output object to mirror the input
kwargs = src.meta
kwargs.update(dtype=rasterio.float32,count = 1)
# Create the file
with rasterio.open("D:\\TESTPATCH\\"+"NDVI"+A+"tif", 'w', **kwargs) as dst:
dst.write_band(1, ndvi.astype(rasterio.float32))