GetData_MODIS_WithPolygon_Colab

PHOTO EMBED

Fri Sep 29 2023 13:51:45 GMT+0000 (Coordinated Universal Time)

Saved by @leandrorbc #python

!pip install eeconvert

pip install --upgrade urllib3

import pandas as pd
import geopandas as gpd
import datetime
import ee
import eeconvert

ee.Authenticate()
ee.Initialize()

from google.colab import drive
drive.mount('/content/drive')

Maiz_str = '/content/drive/MyDrive/Puntos/SHP/Col_2021_Maiz.shp'
Soja_str = '/content/drive/MyDrive/Puntos/SHP/Col_2021_Soja.shp'

df = gpd.read_file(Maiz_str, parse_dates=['Fecha_Siembra', 'Rango_Floor','Rango_Top'])
#df = gpd.read_file(Soja_str, parse_dates=['Fecha_Cosecha', 'Date_R3_Fr', 'Date_R6_Fr'])# en el registro 315 modifiqué el año 0201 por 2016 de la columna Fecha_Cosecha
#df = df.head(10)
display(df.columns)

df['Start_Date'] = pd.to_datetime(df['Rango_Flor']).dt.strftime('%Y-%m-%d')
df['End_Date'] = pd.to_datetime(df['Rango_Top_']).dt.strftime('%Y-%m-%d')


#df['Start_Date'] = pd.to_datetime(df['Date_R3_Fr']).dt.strftime('%Y-%m-%d')
#df['End_Date'] = pd.to_datetime(df['Date_R6_Fr']).dt.strftime('%Y-%m-%d')

df = df[df['Start_Date'].notna()]
df = df[df['End_Date'].notna()]
df = df[df['Longitud'].notna()]
df = df[df['Latitud'].notna()]

df = df[['Start_Date','End_Date', 'geometry', 'Parcela']]

new_df = pd.DataFrame([],columns=['id', 'longitude', 'latitude', 'time', 'NDVI', 'Parcela'])

for index, row in df.iterrows():
  newGDF = df.filter(items = [index], axis=0)
  fc = eeconvert.gdfToFc(newGDF)
  feature = fc.geometry().buffer(-125)
  print(row.Parcela)
  Start_Date = ee.Date(row.Start_Date)
  End_Date = ee.Date(row.End_Date)

  dataset = ee.ImageCollection("MODIS/061/MOD13Q1").select('NDVI').filter(ee.Filter.date(Start_Date,End_Date))
  NDVIvalues = dataset.getRegion(feature, 250).getInfo()
  NDVI_df = pd.DataFrame(NDVIvalues)
  NDVI_df.columns = NDVI_df.iloc[0]
  NDVI_df = NDVI_df.iloc[1:].reset_index(drop=True)
  NDVI_df.insert(1, "Parcela", row.Parcela)
  new_df = new_df.append(NDVI_df)

new_df.to_csv('/content/drive/MyDrive/Puntos/NDVI_Poligonos_Maiz.csv',header=True, index=False)
content_copyCOPY