gaitapi

script GEE – Línea de costa Landsat 8 y 7 (2013–2015 y 2017)

Jul 27th, 2025 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. // ===================== 1. ÁREA DE INTERÉS ======================
  2. var centro = ee.Geometry.Point([-55.70237, -34.78015]);
  3. var radio_m = 2000;
  4. var aoi = centro.buffer(radio_m);
  5.  
  6. // ===================== 2. PARÁMETROS ======================
  7. var anios = ee.List([2013, 2014, 2015, 2017]);
  8. var cloudMax = 30;
  9. var threshold = 0.0;
  10. var scale = 30;
  11. var simplTolerance = 30;
  12. var minArea = 1000; // m²
  13.  
  14. // ===================== 3. FUNCIÓN PARA OBTENER COLECCIÓN POR AÑO ======================
  15. function getLandsatCollection(anio) {
  16. anio = ee.Number(anio);
  17. var fechaInicio = ee.Date.fromYMD(anio, 1, 1);
  18. var fechaFin = fechaInicio.advance(1, 'year');
  19.  
  20. var col;
  21. if (anio.eq(2017)) {
  22. col = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2')
  23. .filterDate(fechaInicio, fechaFin)
  24. .map(function(img) {
  25. var toa = img.multiply(0.0000275).add(-0.2);
  26. var ndwi = toa.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI');
  27. return img.addBands(ndwi).copyProperties(img, ['system:time_start']);
  28. });
  29. } else {
  30. col = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  31. .filterDate(fechaInicio, fechaFin)
  32. .map(function(img) {
  33. var toa = img.multiply(0.0000275).add(-0.2);
  34. var ndwi = toa.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI');
  35. return img.addBands(ndwi).copyProperties(img, ['system:time_start']);
  36. });
  37. }
  38.  
  39. return col.filterBounds(aoi)
  40. .filter(ee.Filter.lt('CLOUD_COVER', cloudMax));
  41. }
  42.  
  43. // ===================== 4. FUNCIÓN DE PROCESAMIENTO POR AÑO ======================
  44. var extraerLineaAnual = function(anio) {
  45. anio = ee.Number(anio);
  46. var coleccion = getLandsatCollection(anio);
  47. var conteo = coleccion.size();
  48.  
  49. return ee.Algorithms.If(
  50. conteo.gt(0),
  51. (function() {
  52. var ndwiMedian = coleccion.select('NDWI').median().clip(aoi);
  53. var waterMask = ndwiMedian.gt(threshold);
  54.  
  55. var polygons = waterMask.reduceToVectors({
  56. geometry: aoi,
  57. geometryType: 'polygon',
  58. scale: scale,
  59. maxPixels: 1e12,
  60. eightConnected: false
  61. });
  62.  
  63. var filtrado = polygons.map(function(f) {
  64. var area = f.geometry().area(1);
  65. return f.set('area_m2', area);
  66. }).filter(ee.Filter.gt('area_m2', minArea));
  67.  
  68. var linea = filtrado.map(function(f) {
  69. var linea = f.geometry().simplify(simplTolerance);
  70. return ee.Feature(linea, {
  71. 'anio': anio,
  72. 'area_m2': f.get('area_m2'),
  73. 'fuente': ee.Algorithms.If(anio.eq(2017), 'Landsat7', 'Landsat8')
  74. });
  75. });
  76.  
  77. return linea;
  78. })(),
  79. ee.FeatureCollection([])
  80. );
  81. };
  82.  
  83. // ===================== 5. APLICAR Y EXPORTAR ======================
  84. var coleccionAnual = anios.map(function(a) {
  85. return extraerLineaAnual(a);
  86. });
  87. var lineasFinal = ee.FeatureCollection(coleccionAnual).flatten();
  88.  
  89. Map.centerObject(aoi, 13);
  90. Map.addLayer(lineasFinal, {color: 'green'}, 'Línea de costa 2013–2015 y 2017');
  91.  
  92. Export.table.toDrive({
  93. collection: lineasFinal,
  94. description: 'LineasCosta_SolisChico_Landsat8_7_2013_2017',
  95. fileFormat: 'GeoJSON'
  96. });
  97.  
Advertisement
Add Comment
Please, Sign In to add comment