-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAH_lab5_scripts.py
More file actions
55 lines (44 loc) · 1.69 KB
/
Copy pathAH_lab5_scripts.py
File metadata and controls
55 lines (44 loc) · 1.69 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
# Lab 5 scripts
import AH_lab5_functions as l5
import matplotlib.pyplot as plt
import geopandas as gpd
import os
# Part 1:
# Assign a variable to the Landsat file
landsat_file = "Landsat_image_corv.tif"
# Pass this to your new smart raster class
smart_raster = l5.SmartRaster(landsat_file)
# Calculate NDVI and save to and output file
try:
ndvi_success, ndvi_result = smart_raster.calculate_ndvi()
if ndvi_success:
print("NDVI calculated successfully.")
else:
print(f"NDVI calculation failed: {ndvi_result}")
except Exception as e:
print(f"An error occurred while calculating NDVI: {e}")
# Part 2:
# Assign a variable to the parcels data shapefile path
parcels_file = "Benton_County_TaxLots.shp"
# Pass this to your new smart vector class
import AH_lab5_functions as l5
smart_vector = l5.SmartVector(parcels_file)
# Calculate zonal statistics and add to the attribute table of the parcels shapefile
try:
zonal_success = smart_vector.add_zonal_stats("ndvi_output.tif", stat="mean", output_column="mean_ndvi")
if zonal_success:
print("Zonal statistics calculated successfully.")
print(smart_vector.vector.head())
else:
print("Zonal statistics calculation failed.")
except Exception as e:
print(f"An error occurred while calculating zonal statistics: {e}")
# Part 3: Optional
# Use matplotlib to make a map of your census tracts with the average NDVI values
try:
fig, ax = plt.subplots(1, 1, figsize=(10, 10))
smart_vector.vector.plot(column="mean_ndvi", ax=ax, legend=True, cmap="YlGn")
ax.set_title("Census Tracts with Average NDVI Values")
plt.show()
except Exception as e:
print(f"An error occurred while plotting: {e}")