Ik ben bezig met het schrijven van een Python-module om de NDVI (Normalized Difference Vegetation Index) te berekenen op basis van de volgende post: Using Python to calculate NDVI with multiband imagery<\/A>. Volgens de USGS achtergrondgegevens over het Landsat 8 Product worden de tegels geleverd als Digitial Numbers 16-bit unsigned integer formaat. De huidige voorbeelden (tutorials en referentiemateriaal) die ik heb gevonden om NDVI te berekenen gebruiken Landsat 5 + 7, welke zijn opgeslagen als 8-bit unsigned integer formaat. Kan ik nog steeds dezelfde formule gebruiken om NDVI te berekenen voor Landsat 8 dat nu is opgeslagen als 16-bit unsigned integer formaat? De formule die ik gebruik is te vinden in: Making Spatial Decisions Using GIS and Remote Sensing: A Workbook<\/A> <\/P><\/P>NDVI = (IR-R)\/(IR+R)<\/P><\/P><\/P>ArcGIS: USGS Landsat 8 GeoTIFF (Bands 4 + 5)<\/P><\/P><\/P>ArcGIS: schaalbereik van Landsat 8 Bands 4 + 5<\/P><\/P><\/P>USGS: Gebruik van het USGS Landsat 8 Product<\/P><\/P><\/P>USGS EarthExplorer: Landsat 8 Dataset Selectie<\/P><\/P><\/P>USGS EarthExplorer: Landsat 8 Downloadopties<\/P><\/BODY><\/HTML>
<\/P>
NDVI = (IR-R)\/(IR+R)<\/P>
ArcGIS: USGS Landsat 8 GeoTIFF (Bands 4 + 5)<\/P>
ArcGIS: schaalbereik van Landsat 8 Bands 4 + 5<\/P>
USGS: Gebruik van het USGS Landsat 8 Product<\/P>
USGS EarthExplorer: Landsat 8 Dataset Selectie<\/P>
USGS EarthExplorer: Landsat 8 Downloadopties<\/P><\/BODY><\/HTML>
Peter, if you are checking the script for whether it is working, run it manually and compare results.
Your only other alternative is to generate 'images' that have a known patterns of values from which you could calculate NDVI. This can be done in numpy
ir <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">64</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">128</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">128</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">192</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">255</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">255</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">128</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># I have left 0's out for now</SPAN> r <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>swapaxes<SPAN class="punctuation token">(</SPAN>ir<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> r array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">128</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">255</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="number token">64</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">192</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">128</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">128</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">255</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> np<SPAN class="punctuation token">.</SPAN>divide<SPAN class="punctuation token">(</SPAN>ir <SPAN class="operator token">-</SPAN> r<SPAN class="punctuation token">,</SPAN> ir <SPAN class="operator token">+</SPAN> r<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># or whatever</SPAN> array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">0.33333333</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">0.33159269</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0.33333333</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.33159269</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0.33159269</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">0.33159269</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Obviously, the only catch is to use masked arrays to mask 0 values to prevent division by zero errors. (hope I got stuff right... scale the input ranges to suit)
not sure I follow... 16 bit isn't 0-255 https://en.wikipedia.org/wiki/16-bit 8-bit is https://en.wikipedia.org/wiki/8-bit
Hi Dan
I found the following on ResearchGate:
Based on the following I presume that I should be able to calculate the NDVI using same formula, its just that the Landsat 8 is being stored in 16-bit and not 8-bit.
Peter
NDVI = (IR-R)/(IR+R)
Hi Cody
Thanks for the response. I'd like to determine the NDVI for the dry and wet season for my study area and then determine the difference between the two. I'm currently still using ArcGIS 10.3.1, unfortunately no ArcGIS Pro. The link that I provided in my initial post is a blog post by ESRI Australia: Using Python to calculate NDVI with multiband imagery, that has a Python script for determing the NDVI. If you are able to assist in the approach I need to following once I've determined the NDVI for say (i.e. 2017 Feb (Wet) and 2017 Oct (Dry)) ,how I can show the difference in NDVI between the wet and dry season. I'd like to be able to code the following in Python as its part of a larger water project that I'm busy with.
a difference is sa.Minus
I completed writing my Python module to automatically generate NDVI rasters from Landsat 8 tiles (Downloaded from USGS EarthExplorer) with TOA Reflectance and Sun Angle correction.
NDVI: With TOA Reflectance and Sun Angle correction
NDVI: Value range (between 1 and -1)
<SPAN class="string token">''' Created on 23 Sep 2017 Create NDVI Rasters with TOA Reflectance and Sun Angle correction @author: PeterW '''</SPAN> <SPAN class="comment token"># import site-packages and modules</SPAN> <SPAN class="keyword token">import</SPAN> re <SPAN class="keyword token">import</SPAN> argparse <SPAN class="keyword token">from</SPAN> pathlib <SPAN class="keyword token">import</SPAN> Path <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">list_landsat_tiles</SPAN><SPAN class="punctuation token">(</SPAN>landsat_dir<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Create a list of Landsat 8 tiles bands 4 & 5. """</SPAN> <SPAN class="comment token"># Determine how to prevent nested loops - Big 0 Notation</SPAN> landsat_tiles <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> ndvi_bands <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'_B4.TIF'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'_B5.TIF'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'_MTL.txt'</SPAN><SPAN class="punctuation token">]</SPAN> p <SPAN class="operator token">=</SPAN> Path<SPAN class="punctuation token">(</SPAN>landsat_dir<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> directory <SPAN class="keyword token">in</SPAN> p<SPAN class="punctuation token">.</SPAN>iterdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># add error handling to validate its subdirectories</SPAN> <SPAN class="keyword token">for</SPAN> band <SPAN class="keyword token">in</SPAN> ndvi_bands<SPAN class="punctuation token">:</SPAN> match <SPAN class="operator token">=</SPAN> <SPAN class="string token">'*{0}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>band<SPAN class="punctuation token">)</SPAN> landsat_tile <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">.</SPAN>glob<SPAN class="punctuation token">(</SPAN>match<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>next<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> landsat_name <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>directory<SPAN class="punctuation token">.</SPAN>stem<SPAN class="punctuation token">)</SPAN> landsat_key <SPAN class="operator token">=</SPAN> re<SPAN class="punctuation token">.</SPAN>findall<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'_(\d{6})_\d{8}_(\d{8})'</SPAN><SPAN class="punctuation token">,</SPAN> landsat_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> landsat_tiles<SPAN class="punctuation token">.</SPAN>setdefault<SPAN class="punctuation token">(</SPAN>landsat_key<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>landsat_tile<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> landsat_tiles <SPAN class="keyword token">def</SPAN> <SPAN class="token function">remove_zero_values</SPAN><SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Convert zero cell values to NoData. """</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> k<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> landsat_tiles<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> red_band <SPAN class="operator token">=</SPAN> SetNull<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'value=0'</SPAN><SPAN class="punctuation token">)</SPAN> NIR_band <SPAN class="operator token">=</SPAN> SetNull<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'value=0'</SPAN><SPAN class="punctuation token">)</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> red_band v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> NIR_band arcpy<SPAN class="punctuation token">.</SPAN>CheckInExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">extract_reflectance_coefficients</SPAN><SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Extract the reflectance coefficients from metadata txt file. """</SPAN> <SPAN class="keyword token">for</SPAN> k<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> landsat_tiles<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> mlt<SPAN class="punctuation token">:</SPAN> lines <SPAN class="operator token">=</SPAN> mlt<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>splitlines<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> reflect_mult <SPAN class="operator token">=</SPAN> float<SPAN class="punctuation token">(</SPAN>lines<SPAN class="punctuation token">[</SPAN><SPAN class="number token">187</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'='</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> reflect_mult reflect_add <SPAN class="operator token">=</SPAN> float<SPAN class="punctuation token">(</SPAN>lines<SPAN class="punctuation token">[</SPAN><SPAN class="number token">196</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'='</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> v<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>reflect_add<SPAN class="punctuation token">)</SPAN> sun_elev <SPAN class="operator token">=</SPAN> float<SPAN class="punctuation token">(</SPAN>lines<SPAN class="punctuation token">[</SPAN><SPAN class="number token">76</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'='</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> v<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>sun_elev<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">toa_reflectance_correction</SPAN><SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Correct landsat 8 bands 4 & 5 for TOA reflectance """</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> k<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> landsat_tiles<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> reflect4 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">*</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> reflect4 reflect5 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">*</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> reflect5 arcpy<SPAN class="punctuation token">.</SPAN>CheckInExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">sun_angle_correction</SPAN><SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Correct Landsat 8 bands 4 & 5 for sun angle """</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> k<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> landsat_tiles<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> sun4 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">/</SPAN><SPAN class="punctuation token">(</SPAN>Sin<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> sun4 sun5 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">/</SPAN><SPAN class="punctuation token">(</SPAN>Sin<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> sun5 arcpy<SPAN class="punctuation token">.</SPAN>CheckInExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">calculate_ndvi</SPAN><SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">,</SPAN> output_dir<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Generate NDVI from preprocessed landsat 8 bands 4 & 5 """</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> f<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> landsat_tiles<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> NDVI_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">'_'</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Processing {0}.tif NDVI'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>NDVI_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> Num <SPAN class="operator token">=</SPAN> Float<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> Denom <SPAN class="operator token">=</SPAN> Float<SPAN class="punctuation token">(</SPAN>v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> v<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> NDVI_raster <SPAN class="operator token">=</SPAN> Divide<SPAN class="punctuation token">(</SPAN>Num<SPAN class="punctuation token">,</SPAN> Denom<SPAN class="punctuation token">)</SPAN> NDVI_output <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{0}\\{1}.tif'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>output_dir<SPAN class="punctuation token">,</SPAN> NDVI_name<SPAN class="punctuation token">)</SPAN> NDVI_raster<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>NDVI_output<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckInExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN>landsat_dir<SPAN class="punctuation token">,</SPAN> output_dir<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Determine NDVI for each Landsat tile. """</SPAN> landsat_tiles <SPAN class="operator token">=</SPAN> list_landsat_tiles<SPAN class="punctuation token">(</SPAN>landsat_dir<SPAN class="punctuation token">)</SPAN> remove_zero_values<SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN> extract_reflectance_coefficients<SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN> toa_reflectance_correction<SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN> sun_angle_correction<SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">)</SPAN> calculate_ndvi<SPAN class="punctuation token">(</SPAN>landsat_tiles<SPAN class="punctuation token">,</SPAN> output_dir<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> parser <SPAN class="operator token">=</SPAN> argparse<SPAN class="punctuation token">.</SPAN>ArgumentParser<SPAN class="punctuation token">(</SPAN>description<SPAN class="operator token">=</SPAN><SPAN class="string token">'Calculate the NDVI for Landsat 8 tiles'</SPAN><SPAN class="punctuation token">)</SPAN> parser<SPAN class="punctuation token">.</SPAN>add_argument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'--landsat_dir'</SPAN><SPAN class="punctuation token">,</SPAN> metavar<SPAN class="operator token">=</SPAN><SPAN class="string token">'path'</SPAN><SPAN class="punctuation token">,</SPAN> required<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> help<SPAN class="operator token">=</SPAN><SPAN class="string token">'Input Landsat 8 tile directory'</SPAN><SPAN class="punctuation token">)</SPAN> parser<SPAN class="punctuation token">.</SPAN>add_argument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'--output_dir'</SPAN><SPAN class="punctuation token">,</SPAN> metavar<SPAN class="operator token">=</SPAN><SPAN class="string token">'path'</SPAN><SPAN class="punctuation token">,</SPAN> required<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> help<SPAN class="operator token">=</SPAN><SPAN class="string token">'Output NDVI directory'</SPAN><SPAN class="punctuation token">)</SPAN> args <SPAN class="operator token">=</SPAN> parser<SPAN class="punctuation token">.</SPAN>parse_args<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> main<SPAN class="punctuation token">(</SPAN>landsat_dir<SPAN class="operator token">=</SPAN>args<SPAN class="punctuation token">.</SPAN>landsat_dir<SPAN class="punctuation token">,</SPAN> output_dir<SPAN class="operator token">=</SPAN>args<SPAN class="punctuation token">.</SPAN>output_dir<SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The only question that I have is that my output raster is between 1 and -1 but is not represented as 1 to -1 is that because of the actual values within my tile or is there a problem with my calculation?
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.