UjjwalKGupta commited on
Commit
dbffabb
·
1 Parent(s): e42632d

Uncomment EE credentials

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -16,11 +16,11 @@ fiona.drvsupport.supported_drivers['LIBKML'] = 'rw'
16
 
17
  #Intialize EE library
18
  # Error in EE Authentication
19
- # ee_credentials = os.environ.get("EE")
20
- # os.makedirs(os.path.expanduser("~/.config/earthengine/"), exist_ok=True)
21
- # with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f:
22
- # f.write(ee_credentials)
23
- # ee.Initialize()
24
 
25
  # Functions
26
  def convert_to_2d_geometry(geom): #Handles Polygon Only
@@ -162,33 +162,33 @@ if uploaded_file is not None:
162
  st.write("It has area of {:.2f} meter squared.".format(polygon_info['area']))
163
  st.write("It has perimeter of {:.2f} meters.".format(polygon_info['perimeter']))
164
 
165
- # Read KML file
166
- # geom_ee_object = ee.FeatureCollection(json.loads(gdf.to_json()))
167
 
168
- # # Add buffer of 100m to ee_object
169
- # buffered_ee_object = geom_ee_object.map(lambda feature: feature.buffer(100))
170
 
171
- # # Filter data based on the date, bounds, cloud coverage and select NIR and Red Band
172
- # collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED").filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', max_cloud_cover)).filter(ee.Filter.date(start_date, end_date)).select(['B4', 'B8'])
173
 
174
- # # Get Zonal NDVI based on collection and geometries (Original KML and Buffered KML)
175
- # df_geom = get_zonal_ndvi(collection, geom_ee_object)
176
- # df_buffered_geom = get_zonal_ndvi(collection, buffered_ee_object)
177
 
178
- # # Merge both Zonalstats and create resultant dataframe
179
- # resultant_df = pd.merge(df_geom, df_buffered_geom, on='Date', how='inner')
180
- # resultant_df = resultant_df.rename(columns={'NDVI_x': 'AvgNDVI_Inside', 'NDVI_y': 'Avg_NDVI_Buffer', 'Imagery_x': 'Imagery'})
181
- # resultant_df['Ratio'] = resultant_df['AvgNDVI_Inside'] / resultant_df['Avg_NDVI_Buffer']
182
- # resultant_df.drop(columns=['Imagery_y'], inplace=True)
183
 
184
- # # Re-order the columns of the resultant dataframe
185
- # resultant_df = resultant_df[['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']]
186
 
187
- # st.write(resultant_df)
188
 
189
- # # plot the time series
190
- # st.write("Time Series Plot")
191
- # st.line_chart(resultant_df.set_index('Date'))
192
 
193
  else:
194
  st.write('ValueError: "Input must have single polygon geometry"')
 
16
 
17
  #Intialize EE library
18
  # Error in EE Authentication
19
+ ee_credentials = os.environ.get("EE")
20
+ os.makedirs(os.path.expanduser("~/.config/earthengine/"), exist_ok=True)
21
+ with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f:
22
+ f.write(ee_credentials)
23
+ ee.Initialize()
24
 
25
  # Functions
26
  def convert_to_2d_geometry(geom): #Handles Polygon Only
 
162
  st.write("It has area of {:.2f} meter squared.".format(polygon_info['area']))
163
  st.write("It has perimeter of {:.2f} meters.".format(polygon_info['perimeter']))
164
 
165
+ #Read KML file
166
+ geom_ee_object = ee.FeatureCollection(json.loads(gdf.to_json()))
167
 
168
+ # Add buffer of 100m to ee_object
169
+ buffered_ee_object = geom_ee_object.map(lambda feature: feature.buffer(100))
170
 
171
+ # Filter data based on the date, bounds, cloud coverage and select NIR and Red Band
172
+ collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED").filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', max_cloud_cover)).filter(ee.Filter.date(start_date, end_date)).select(['B4', 'B8'])
173
 
174
+ # Get Zonal NDVI based on collection and geometries (Original KML and Buffered KML)
175
+ df_geom = get_zonal_ndvi(collection, geom_ee_object)
176
+ df_buffered_geom = get_zonal_ndvi(collection, buffered_ee_object)
177
 
178
+ # Merge both Zonalstats and create resultant dataframe
179
+ resultant_df = pd.merge(df_geom, df_buffered_geom, on='Date', how='inner')
180
+ resultant_df = resultant_df.rename(columns={'NDVI_x': 'AvgNDVI_Inside', 'NDVI_y': 'Avg_NDVI_Buffer', 'Imagery_x': 'Imagery'})
181
+ resultant_df['Ratio'] = resultant_df['AvgNDVI_Inside'] / resultant_df['Avg_NDVI_Buffer']
182
+ resultant_df.drop(columns=['Imagery_y'], inplace=True)
183
 
184
+ # Re-order the columns of the resultant dataframe
185
+ resultant_df = resultant_df[['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']]
186
 
187
+ st.write(resultant_df)
188
 
189
+ # plot the time series
190
+ st.write("Time Series Plot")
191
+ st.line_chart(resultant_df.set_index('Date'))
192
 
193
  else:
194
  st.write('ValueError: "Input must have single polygon geometry"')