-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinal2.py
More file actions
176 lines (145 loc) · 5.56 KB
/
sentinal2.py
File metadata and controls
176 lines (145 loc) · 5.56 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from dotenv import load_dotenv # added to load env variables
import os # added for env var support
load_dotenv(dotenv_path=".env.local") # load env variables
PROJECT_ID = os.environ.get("PROJECT_ID") # get project id from env
import ee
ee.Initialize(project=PROJECT_ID) # modified to load project id from env
# # ✅ Use new harmonized dataset
# collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
# .filterDate('2025-6-10', '2025-06-18') \
# .filterBounds(ee.Geometry.Point([78.0421, 27.1751])) # e.g., Taj Mahal
# image = collection.first()
# print(image.getInfo())
# import ee
# import geemap
# import matplotlib.pyplot as plt
# # Initialize Earth Engine
# ee.Initialize()
# # Load the Sentinel-2 image by ID
# image = ee.Image('COPERNICUS/S2_SR_HARMONIZED/20250612T052649_20250612T053352_T43RGL')
# # Select RGB bands for true color
# rgb_image = image.select(['B4', 'B3', 'B2'])
# # Define visualization parameters
# vis_params = {
# 'min': 0,
# 'max': 3000,
# 'bands': ['B4', 'B3', 'B2']
# }
# # Define region of interest (footprint of the image)
# region = image.geometry()
# # Get a thumbnail URL
# url = rgb_image.getThumbURL({
# 'dimensions': 1024,
# 'region': region,
# 'format': 'png',
# 'min': 0,
# 'max': 3000,
# 'bands': ['B4', 'B3', 'B2']
# })
# print("🖼️ Image thumbnail URL (open in browser):\n", url)
# # Optional: Download and show the image in Python
# import requests
# from PIL import Image
# from io import BytesIO
# response = requests.get(url)
# img = Image.open(BytesIO(response.content))
# # Show image in a matplotlib viewer
# plt.imshow(img)
# plt.axis('off')
# plt.title("Sentinel-2 True Color Composite")
# plt.show()
# # Optional: Save it locally
# img.save("sentinel2_true_color.png")
# print("✅ Image saved as 'sentinel2_true_color.png'")
# import ee
# ee.Initialize(project=PROJECT_ID) # modified to load project id from env
# # Define location (Taj Mahal example)
# point = ee.Geometry.Point([78.0421, 27.1751])
# # Load Sentinel-2 SR Harmonized data
# collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
# .filterDate('2025-06-10', '2025-06-18') \
# .filterBounds(point)
# # NDVI Calculation Function
# def compute_ndvi(image):
# ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
# image_with_ndvi = image.addBands(ndvi)
# return image_with_ndvi.set({'NDVI_mean': ndvi.reduceRegion(
# reducer=ee.Reducer.mean(),
# geometry=point.buffer(100), # 100m radius around point
# scale=10,
# maxPixels=1e8
# ).get('NDVI')})
# # Map NDVI calculation across the collection
# collection_with_ndvi = collection.map(compute_ndvi)
# # Get list of images with their NDVI values
# ndvi_list = collection_with_ndvi.aggregate_array('NDVI_mean').getInfo()
# id_list = collection_with_ndvi.aggregate_array('system:index').getInfo()
# # Print NDVI per image
# for img_id, ndvi in zip(id_list, ndvi_list):
# print(f"{img_id}: NDVI = {ndvi}")
# import ee
# ee.Initialize(project=PROJECT_ID) # modified to load project id from env
point = ee.Geometry.Point([78.0421, 27.1751])
collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
.filterDate('2025-06-10', '2025-06-18') \
.filterBounds(point)
# ✅ Define Index Functions
def add_indices(image):
ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
evi = image.expression(
'2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
{'NIR': image.select('B8'), 'RED': image.select('B4'), 'BLUE': image.select('B2')}
).rename('EVI')
savi = image.expression(
'((NIR - RED) / (NIR + RED + 0.5)) * 1.5',
{'NIR': image.select('B8'), 'RED': image.select('B4')}
).rename('SAVI')
gci = image.expression(
'(NIR / GREEN) - 1',
{'NIR': image.select('B8'), 'GREEN': image.select('B3')}
).rename('GCI')
ndwi = image.normalizedDifference(['B3', 'B8']).rename('NDWI')
msi = image.expression(
'SWIR / NIR',
{'SWIR': image.select('B11'), 'NIR': image.select('B8')}
).rename('MSI')
ndmi = image.normalizedDifference(['B8', 'B11']).rename('NDMI')
nbr = image.normalizedDifference(['B8', 'B12']).rename('NBR')
rendvi = image.normalizedDifference(['B8', 'B5']).rename('RENDVI')
# Mean values in 100m buffer
indices = ee.Image.cat([ndvi, evi, savi, gci, ndwi, msi, ndmi, nbr, rendvi])
stats = indices.reduceRegion(
reducer=ee.Reducer.mean(),
geometry=point.buffer(100),
scale=10,
maxPixels=1e8
)
return image.set(stats)
# ✅ Add indices and extract metadata
def enrich_image(image):
image = add_indices(image)
meta_props = [
'CLOUD_COVERAGE_ASSESSMENT',
'SNOW_ICE_PERCENTAGE',
'VEGETATION_PERCENTAGE',
'NOT_VEGETATED_PERCENTAGE',
'WATER_PERCENTAGE'
]
return image.set({p: image.get(p) for p in meta_props})
# ✅ Apply function to all images
enriched = collection.map(enrich_image)
# ✅ Print summary
props = enriched.aggregate_array('system:index').getInfo()
ndvi = enriched.aggregate_array('NDVI').getInfo()
evi = enriched.aggregate_array('EVI').getInfo()
savi = enriched.aggregate_array('SAVI').getInfo()
gci = enriched.aggregate_array('GCI').getInfo()
cloud = enriched.aggregate_array('CLOUD_COVERAGE_ASSESSMENT').getInfo()
# ✅ Print everything
for i in range(len(props)):
print(f"\n🛰️ Image: {props[i]}")
print(f" NDVI: {ndvi[i]}")
print(f" EVI: {evi[i]}")
print(f" SAVI: {savi[i]}")
print(f" GCI: {gci[i]}")
print(f" Cloud Cover: {cloud[i]}%")