I am trying to extract attribute values from a Feature Collection in Google Earth Engine and use that value as a parameter value afterward.
In the example below, I define the FeatureCollection on the fly. However, in reality, I have the FC as an asset; the idea is to use it as a configuration file. Maybe there is a better way to do this.
The following code works; however, I can only get one attribute now, the 'PROJ' attribute. How would I extract the 'NAMEUNIT' and use it as a description to Export the result?
var geometry = ee.Geometry.Polygon(
[[[-3.982054891721716, 40.51882898775919],
[-3.982054891721716, 40.26256975598024],
[-3.193785848752966, 40.26256975598024],
[-3.193785848752966, 40.51882898775919]]], null, false);
// Load your Feature Collection
var provinces = ee.FeatureCollection([ee.Feature(null, {NAMEUNIT: 'Madrid', PROJ: 'EPSG:32630'}),
ee.Feature(null, {NAMEUNIT: 'Barcelona', PROJ: 'EPSG:32631'}),
ee.Feature(null, {NAMEUNIT: 'Sevilla', PROJ: 'EPSG:32629'})]);
// Filter the Feature Collection for the province of Madrid
var madridFeature = provinces.filter(ee.Filter.eq('NAMEUNIT', 'Madrid')).first();
// Extract the epsg_code property
var proj = madridFeature.get('PROJ');
// Load the image you want to export (replace with your image)
var image = ee.ImageCollection("LANDSAT/LC08/C02/T1_TOA").filterBounds(geometry)
var projValueGlobal;
// Use evaluate() to retrieve the value on the client side
proj.evaluate(function(projValue) {
projValueGlobal = projValue; // Store the value in the global variable
print('Projection for Madrid (inside callback):', projValueGlobal);
// Define the export task
// I need to do it inside the projValue function; otherwise, it does not work
Export.image.toDrive({
image: image,
description: 'Exported_Image_Madrid',
scale: 30, // Adjust the scale as needed
region: geometry,
crs: projValueGlobal, // Use the extracted EPSG code
maxPixels: 1e13 // Adjust max pixels if necessary
});
});
If instead, I do the following:
// Filter the Feature Collection for the province of Madrid
var madridFeature = provinces.filter(ee.Filter.eq('NAMEUNIT', 'Madrid')).first();
// Extract the epsg_code property
var proj = madridFeature.get('PROJ');
Export.image.toDrive({
image: image,
description: 'Exported_Image_Madrid',
scale: 30, // Adjust the scale as needed
region: geometry,
crs: proj, // Use the extracted EPSG code
max pixel: 1e13 // Adjust max pixels if necessary
});
I get the following problem when exporting the result, see the CRS value: