gaitapi

script GEE Lineas de Costa Landsat 1990 a 2022 ej Solis Chico

Jul 27th, 2025 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 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.sequence(1990, 2022);
  8. var cloudMax = 30; // mayor tolerancia en Landsat
  9. var threshold = 0.0;
  10. var scale = 30; // resolución Landsat
  11. var simplTolerance = 30; // simplificación en metros
  12. var minArea = 1000; // m²
  13.  
  14. // ===================== 3. FUNCIÓN PARA ELEGIR COLECCIÓN Y BANDAS ======================
  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. var ndwi_banda;
  22.  
  23. if (anio.lte(2011)) {
  24. col = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
  25. .filterDate(fechaInicio, fechaFin)
  26. .map(function(img) {
  27. var toa = img.multiply(0.0000275).add(-0.2);
  28. var ndwi = toa.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI');
  29. return img.addBands(ndwi).copyProperties(img, ['system:time_start']);
  30. });
  31. } else if (anio.eq(2012)) {
  32. col = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2')
  33. .filterDate(fechaInicio, fechaFin)
  34. .map(function(img) {
  35. var toa = img.multiply(0.0000275).add(-0.2);
  36. var ndwi = toa.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI');
  37. return img.addBands(ndwi).copyProperties(img, ['system:time_start']);
  38. });
  39. } else {
  40. col = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  41. .filterDate(fechaInicio, fechaFin)
  42. .map(function(img) {
  43. var toa = img.multiply(0.0000275).add(-0.2);
  44. var ndwi = toa.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI');
  45. return img.addBands(ndwi).copyProperties(img, ['system:time_start']);
  46. });
  47. }
  48.  
  49. return col.filterBounds(aoi)
  50. .filter(ee.Filter.lt('CLOUD_COVER', cloudMax));
  51. }
  52.  
  53. // ===================== 4. PROCESAMIENTO POR AÑO ======================
  54. var extraerLineaAnual = function(anio) {
  55. anio = ee.Number(anio);
  56. var coleccion = getLandsatCollection(anio);
  57. var conteo = coleccion.size();
  58.  
  59. return ee.Algorithms.If(
  60. conteo.gt(0),
  61. (function() {
  62. var ndwiMedian = coleccion.select('NDWI').median().clip(aoi);
  63. var waterMask = ndwiMedian.gt(threshold);
  64.  
  65. var polygons = waterMask.reduceToVectors({
  66. geometry: aoi,
  67. geometryType: 'polygon',
  68. scale: scale,
  69. maxPixels: 1e12,
  70. eightConnected: false
  71. });
  72.  
  73. var filtrado = polygons.map(function(f) {
  74. var area = f.geometry().area(1);
  75. return f.set('area_m2', area);
  76. }).filter(ee.Filter.gt('area_m2', minArea));
  77.  
  78. var linea = filtrado.map(function(f) {
  79. var linea = f.geometry().simplify(simplTolerance);
  80. return ee.Feature(linea, {
  81. 'anio': anio,
  82. 'area_m2': f.get('area_m2'),
  83. 'fuente': 'Landsat'
  84. });
  85. });
  86.  
  87. return linea;
  88. })(),
  89. ee.FeatureCollection([])
  90. );
  91. };
  92.  
  93. // ===================== 5. APLICACIÓN Y VISUALIZACIÓN ======================
  94. var coleccionAnual = anios.map(function(a) {
  95. return extraerLineaAnual(a);
  96. });
  97.  
  98. var lineasFinal = ee.FeatureCollection(coleccionAnual).flatten();
  99.  
  100. Map.centerObject(aoi, 13);
  101. Map.addLayer(lineasFinal, {color: 'green'}, 'Líneas Landsat 1990–2022');
  102.  
  103. // ===================== 6. EXPORTACIÓN ======================
  104. Export.table.toDrive({
  105. collection: lineasFinal,
  106. description: 'LineasCosta_Landsat_SolisChico_1990_2022',
  107. fileFormat: 'GeoJSON'
  108. });
  109.  
Advertisement
Add Comment
Please, Sign In to add comment