#!/usr/bin/env python"""Calculate NDVI from RAT using RIOS ratmodule.Dan Clewleyhttps://spectraldifferences.wordpress.com/"""fromosgeoimportgdalfromriosimportrat# Open RAT datasetrat_dataset=gdal.Open("clumps.kea",gdal.GA_Update)# Get columns with average red and NIR for each objectred=rat.readColumn(rat_dataset,"RedAvg")nir=rat.readColumn(rat_dataset,"NIR1Avg")ndvi=(nir-red)/(nir+red)# Write out columnrat.writeColumn(rat_dataset,"NDVI",ndvi)# Close RAT datasetrat_dataset=None
#!/usr/bin/env python"""Calculate NDVI from RAT using RIOS ratappliermodule.Dan Clewleyhttps://spectraldifferences.wordpress.com/"""fromriosimportratapplierdef_ratapplier_calc_ndvi(info,inputs,outputs):""" Calculate NDVI from RAT. Called by ratapplier """# Get columns with average red and NIR for each object# within blockred=getattr(inputs.inrat,"RedAvg")nir=getattr(inputs.inrat,"NIR1Avg")# Calculate NDVIndvi=(nir-red)/(nir+red)# Save to 'NDVI' column (will create if doesn't exist)setattr(outputs.outrat,"NDVI",ndvi)if__name__=="__main__":# Set up rat applier for input / outputin_rats=ratapplier.RatAssociations()out_rats=ratapplier.RatAssociations()# Pass in clumps file# Same file is used for input and output to write# to existing RATin_rats.inrat=ratapplier.RatHandle("clumps.kea")out_rats.outrat=ratapplier.RatHandle("clumps.kea")# Apply function to all rows in chunksratapplier.apply(_ratapplier_calc_ndvi,in_rats,out_rats)
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.