import ee # Initialize the Earth Engine API ee.Initialize(project='farmbase-b2f7e') def classify_crop_in_polygon_sentinel(polygon_coords, start_date, end_date): """ Classify crops within a single polygon using Sentinel-2 data. Args: polygon_coords (list): A list of coordinates representing the polygon in the format [[lon1, lat1], [lon2, lat2], ...]. start_date (str): Start date for image collection (e.g., '2021-01-01'). end_date (str): End date for image collection (e.g., '2021-12-31'). Returns: ee.Image: A classified image representing the predicted crops within the polygon. """ # Create an ee.Geometry.Polygon from the input polygon coordinates polygon = ee.Geometry.Polygon(polygon_coords) # Define a function to mask clouds using the Sentinel-2 QA60 band def mask_clouds(image): cloudBitMask = 1 << 10 cirrusBitMask = 1 << 11 qa60 = image.select(['QA60']) mask = qa60.bitwiseAnd(cloudBitMask).eq(0).And(qa60.bitwiseAnd(cirrusBitMask).eq(0)) return image.updateMask(mask).divide(10000) # Load Sentinel-2 data and apply the cloud mask sentinel2 = ee.ImageCollection('COPERNICUS/S2') \ .filterBounds(polygon) \ .filterDate(start_date, end_date) \ .map(mask_clouds) \ .median() # Select the relevant bands for classification (e.g., 'B4', 'B3', 'B2') bands = ['B4', 'B3', 'B2'] # Apply a classifier of your choice (e.g., Random Forest) classifier = ee.Classifier.randomForest().train(training, 'class', bands) classified = sentinel2.select(bands).classify(classifier) # Clip the dataset to the polygon clipped_data = classified.clip(polygon) print(clipped_data) return clipped_data # Example usage: polygon_coords = [ [75.3589059, 24.4474494], [75.3588523, 24.4469977], [75.3594504, 24.4469415], [75.3594465, 24.4468412], [75.3598378, 24.4468241], [75.3598374, 24.4467459], [75.3600919, 24.446758], [75.3602018, 24.4473412], [75.3589059, 24.4474494] ] # Replace with your polygon coordinates start_date = '2021-01-01' end_date = '2021-12-31' predicted_crop = classify_crop_in_polygon_sentinel(polygon_coords, start_date, end_date) # import ee # # Initialize Earth Engine # ee.Initialize(project='farmbase-b2f7e') # # Define the farm boundary as a GeoJSON geometry # farm_boundary = ee.Geometry.Polygon( # [ # [ # [75.3589059, 24.4474494], # [75.3588523, 24.4469977], # [75.3594504, 24.4469415], # [75.3594465, 24.4468412], # [75.3598378, 24.4468241], # [75.3598374, 24.4467459], # [75.3600919, 24.446758], # [75.3602018, 24.4473412], # [75.3589059, 24.4474494] # ] # ] # ) # # Define the time range for satellite imagery # start_date = '2022-01-01' # end_date = '2022-12-31' # # Load satellite imagery (e.g., Sentinel-2) # image_collection = ( # ee.ImageCollection('COPERNICUS/S2') # .filterBounds(farm_boundary) # .filterDate(ee.Date(start_date), ee.Date(end_date)) # .sort('CLOUDY_PIXEL_PERCENTAGE') # .first() # ) # # Define a region of interest for classification # roi = farm_boundary # # Load the USDA NASS Cropland Data Layer (CDL) # cdl = ee.Image('USDA/NASS/CDL/2021') # You can choose the year you need # # Select relevant bands from the CDL # cdl_bands = ['cropland'] # # Sample the CDL at the farm boundary # cdl_sample = cdl.select(cdl_bands).sample( # region=roi, # scale=30, # geometries=True, # ) # # Define the CDL class property as 'crop_type' # cdl_sample = cdl_sample.rename(['crop_type']) # # Merge the CDL sample with your own training data (if available) # # Replace 'training_data' with your own FeatureCollection # training_data = training_data.merge(cdl_sample) # # Select relevant bands from the imagery # bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12'] # # Sample the imagery at training data points # training = image_collection.select(bands).sampleRegions( # collection=training_data, # properties=['crop_type'], # scale=10, # ) # # Define a classifier (e.g., Random Forest) # classifier = ee.Classifier.randomForest(10).train( # features=training, # classProperty='crop_type', # inputProperties=bands, # ) # # Classify the image # classified = image_collection.select(bands).classify(classifier) # # Get the crop type prediction for the farm boundary # prediction = classified.reduceRegion( # reducer=ee.Reducer.mode(), # geometry=roi, # scale=10, # ) # # Print the predicted crop type # print('Predicted Crop Type:', prediction.get('classification'))