Springe direkt zu Inhalt

تحليل اتجاه الانحدار الخطي

لإجراء انحدار خطي بسيط، نحتاج أولاً إلى الوصول إلى بيانات الصورة والرسم الهندسي للشرائح وإعدادها لمجال اهتمامنا.

في هذا المثال، سنستخدم جميع بيانات انعكاس السطح من المستوى 1 من لاندسات 8 مع تغطية سحابية أقل من 10٪ لمنطقة حمص، سوريا.

تجهيز البيانات

//Access the Area of Interest geometry
var extent_aoi = ee.FeatureCollection("FAO/GAUL/2015/level1")
                  .filter(ee.Filter.eq('ADM1_NAME', 'Homs')); //filter for entry that equals the ADM1_NAME 'Homs'
print(extent_aoi, 'Extent AOI');
Map.addLayer(extent_aoi, {},'Extent AOI');
Map.centerObject(extent_aoi, 8);

//Access Landsat 8 data from the day it launched until today. Filter for the desired region and less than 10% cloud cover.
var ls8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
        .filterBounds(extent_aoi)
        .filter(ee.Filter.lt('CLOUD_COVER', 10));
print(ls8, 'Image Collection 2013-2021 <10% clouds'); //Use the console to look at the amount of Images accumulated

var visParams = {'min': 400,'max': [4000,3000,3000],   'bands':'B5,B4,B3'}; //bands 5, 4 and 3 are to be visualized; False Color Composite
Map.addLayer(ls8, visParams ,'Image Collection 2013-2021 <10% clouds', false);   // Visualize the ImageCollection in the Map panel and assign a name to it; Hide the layer by default, as it takes long to render

أضف الوقت ونطاقات NDVI

بعد ذلك، نحتاج إلى إنشاء المتغيرات التابعة والمستقلة.

في هذا المثال، نريد تحليل اتجاه قيم NDVI بمرور الوقت.

هذا يعني أننا بحاجة إلى كتابة وظيفتين تسمحان لنا بتعيين NDVI ومعلومات الوقت لكل صورة من مجموعة الصور الخاصة بنا.

يمكننا استخدام. اعادة ا لتسمية NDVI-band المضافة، مما يجعل تصفح البيانات أسهل.

var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
};

//Apply the function to the ImageCollection
var ls8_ndvi = ls8.map(addNDVI);
print(ls8_ndvi, 'Image Collection with NDVI')
 
 
 
 //Step 3: Write a function that adds a time band to the image collection. The time is divided by a large number to avoid very small slopes in the regression output, hence making it easier to visualize and interpret.
var createTimeBand = function(image) {
  return image.addBands(image.metadata('system:time_start').divide(1e15));
};

//Apply the function to the ImageCollection.
var ls8_trend = ls8_ndvi.map(createTimeBand);

//Have a look at the prepared ImageCollection
print(ls8_trend, 'Image Collection with NDVI and time band');

قم بتشغيل وظيفة Linear Fit

الآن بعد أن أصبح لدينا بيانات ومتغيرات صورنا جاهزة، يمكننا بسهولة حساب التوافق الخطي للانحدار باستخدام مخفض الخط.

تحقق من وثائقه على Docs -> ee.Reducer -> ee.Reducer.linearFit لمعرفة المزيد حول تركيبه.

المتغير الأول هو المتغير المستقل، في حالتنا النطاق الزمني، والمتغير الثاني هو التابع، في حالتنا نطاق NDVI.

//Calulate the NDVI-Trends using the linear fit reducer. The first variable is the independent variable, the second one the dependent.
var linearFit_ndvi = ls8_trend.select(['system:time_start', 'NDVI'])
  .reduce(ee.Reducer.linearFit());

//Have a look at the output in the Console. It consists of two bands, the "scale", which is the slope, and the offset of the linear regression.
print(linearFit_ndvi, 'Linear Regression Fit')



//Display the results. In this example, we will display it in two different ways: One showing the trends in colour, and one showing the trends in a greyscale. 
//For both variants, we need to use the Inspector to evaluate a suitable range of max-values to visualize the data.

//Trends displayed in colour
Map.addLayer(linearFit_ndvi.clip(extent_aoi),
  {min: 0, max: [-130, 0.9, 130], bands: ['scale', 'offset', 'scale']}, 'Linear Fit NDVI colours');

//Negative slopes appear in red, showing negative trends -> vegetation loss!
//Positive slopes appear in blue, showing positive trends -> vegetation gain!

//Trends displayed in greyscale  
Map.addLayer(linearFit_ndvi.select('scale').clip(extent_aoi),
          {min:-130, max:130}, 'Linear Fit NDVI Greyscale');

//Negative slopes appear in black, showing negative trends -> vegetation loss!
//Positive slopes appear in white, showing positive trends -> vegetation gain!