// You need to import the polygons before as vars // Define Polygons var polygons = { 'Greenbushes': greenbushes, 'Salar de Atacama SQM': atacamasqm, 'Pilgangoora': pilgangoora, 'Wodgina': wodgina, 'Mount Marion': mountmarion, 'Mt Cattlin': mtcattlin, 'Salar de Olaroz': olaroz, }; // Define a list of mines and their associated geometries var mines = ee.List([ ee.Dictionary({name: 'Greenbushes', geometry: greenbushes}), // Ersetze 'greenbushes' durch die tatsächliche Geometrie der ersten Mine ee.Dictionary({name: 'Salar de Atacama SQM', geometry: atacamasqm}), ee.Dictionary({name: 'Wodgina', geometry: wodgina}), ee.Dictionary({name: 'Mt Cattlin', geometry: mtcattlin}), ee.Dictionary({name: 'Mount Marion', geometry: mountmarion}), ee.Dictionary({name: 'Pilgangoora', geometry: pilgangoora}) ]); // Define the time period for the analysis var startYear = 2017; var endYear = 2023; // Load the Dynamic World data product var dw = ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1'); // Define the ground cover classes with the correct English names and in the correct order. var classNames = ee.Dictionary({ 0: 'Water', 1: 'Trees', 2: 'Grassland', 3: 'Wetlands', 4: 'Cropland', 5: 'Shrubland', 6: 'Built-up', 7: 'Snow/Ice', 8: 'Bare' }); // Function to calculate the number of pixels in each class for each year, mine, and quarter var getPixelCountsByClass = function(mine) { mine = ee.Dictionary(mine); // Conversion to an ee.Dictionary var mineName = mine.getString('name'); var mineGeometry = ee.Geometry(mine.get('geometry')); // Function that calculates the number of pixels in each class for each year and each quarter var quarterCounts = ee.List([]); // Iterate over the years for (var year = startYear; year <= endYear; year++) { // Definiere die Quartale var quarters = [ {start: ee.Date.fromYMD(year, 1, 1), end: ee.Date.fromYMD(year, 3, 31), quarter: 'Q1'}, {start: ee.Date.fromYMD(year, 4, 1), end: ee.Date.fromYMD(year, 6, 30), quarter: 'Q2'}, {start: ee.Date.fromYMD(year, 7, 1), end: ee.Date.fromYMD(year, 9, 30), quarter: 'Q3'}, {start: ee.Date.fromYMD(year, 10, 1), end: ee.Date.fromYMD(year, 12, 31), quarter: 'Q4'} ]; // Iterate over the quarters for (var i = 0; i < quarters.length; i++) { var startDate = ee.Date(quarters[i].start); var endDate = ee.Date(quarters[i].end); var quarter = quarters[i].quarter; // Filter the Dynamic World ImageCollection for the quarter and the mine area var filteredDw = dw.filterDate(startDate, endDate) .filterBounds(mineGeometry) .select('label'); // Check whether there is sufficient data var imageCount = filteredDw.size(); // If no data is available, skip the calculation for this quarter. if (imageCount.gt(0)) { // Create a composite that determines the most common value for each pixel var modeComposite = filteredDw.reduce(ee.Reducer.mode()); // Cut the composite to fit the mine area var clippedComposite = modeComposite.clip(mineGeometry); // Count the number of pixels in each class (0 to 8) within the polygon var classCounts = clippedComposite.reduceRegion({ reducer: ee.Reducer.frequencyHistogram(), geometry: mineGeometry, scale: 10, maxPixels: 1e9 }).get('label_mode'); classCounts = ee.Dictionary(classCounts); // Add the class names as separate fields var classFields = classNames.map(function(classNum, className) { // Get the number of pixels in the class; if not available, set it to 0 var count = ee.Number(classCounts.get(classNum, 0)); return count; }); // Add the year, quarter, mine name, and pixel counts for each class var result = classFields.set('year', year).set('quarter', quarter).set('mine', mineName); quarterCounts = quarterCounts.add(result); } } } return quarterCounts; }; // Iterate over all mines and add the results to a list var allResults = mines.iterate(function(mine, previousResults) { var mineResults = getPixelCountsByClass(mine); return ee.List(previousResults).cat(mineResults); }, ee.List([])); // Convert the list of results into a FeatureCollection for better display var countsFeatureCollection = ee.FeatureCollection(ee.List(allResults).map(function(dict) { return ee.Feature(null, dict); })); // Print the number of pixels per class with the class names for each year, each quarter, and each mine print('Pixelanzahl pro Klasse pro Jahr, Quartal und Mine', countsFeatureCollection); // Export the table as a CSV file with the class names, mine names, years, and quarters as column headers. Export.table.toDrive({ collection: countsFeatureCollection, description: 'PixelCountsByClassForMultipleMinesAndQuarters', fileFormat: 'CSV' });