Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // DATA PREPARATION
  2. //Load the nightlight data
  3. var light_dataset = ee.ImageCollection('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS').select('stable_lights');
  4. Map.addLayer(light_dataset,{min: 3.0,max: 60.0,palette:['000000','9999CC']},'nighttimeLights');
  5. //Load province boundary of China
  6. var China = China;
  7. Map.addLayer(China,{color:'FFFFFF'},'China_Province');
  8. Map.centerObject(China,4);
  9. print(China);
  10. //Load country boundary of China
  11. var boundary_geo=China.geometry();
  12. var boundary = ee.Feature(boundary_geo);
  13.  
  14.  
  15. // Part.1 AN OVERVIEW OF URBAN BUILT-UP AREA CHANGES IN CHINA
  16. //Load the nightlight from 2003-2013
  17. var light_2003 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152003').select('stable_lights').clip(boundary);
  18. Map.addLayer(light_2003,{min: 3.0,max: 60.0,palette:['000000','FF9933','ffffff'],opacity:0.8},'Lights_2003');
  19. var light_2008 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F162008').select('stable_lights').clip(boundary);
  20. Map.addLayer(light_2008,{min: 3.0,max: 60.0,palette:['000000','FF9933','ffffff'],opacity:0.8},'Lights_2008');
  21. var light_2013 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182013').select('stable_lights').clip(boundary);
  22. Map.addLayer(light_2013,{min: 3.0,max: 60.0,palette:['000000','FF9933','ffffff'],opacity:0.8},'Lights_2013');
  23.  
  24. // find the place that are considered "lit"
  25. var blank = ee.Image(0);
  26. function AreaLit(i){
  27.   var threhold=blank.where(i.gt(31),1);
  28.   var alit = threhold.mask(threhold);
  29.   alit = ee.Image.pixelArea().mask(alit);
  30.   alit=alit.divide(1000000);
  31.   alit=alit.set('index',i.get('system:index'));
  32.   return alit;
  33. }
  34. // stable nighttime light area change (by province) from 2003-2013
  35. function tabulate(i){
  36.   return China.map(function(f){
  37.     var r=i.reduceRegion({
  38.       reducer:ee.Reducer.sum(),
  39.       geometry:f.geometry(),
  40.       scale:500,
  41.       bestEffort:true,
  42.       maxPixels:1e9});
  43.     return ee.Feature(null,{
  44.       name:f.get('Province'),
  45.       area:r.get('area'),
  46.       index:i.get('index')});});}
  47. var arelit=light_dataset.map(AreaLit);
  48. var arelit_table=arelit.map(tabulate).flatten();
  49. Export.table.toDrive(arelit_table,'DMSP_results','DMSP results','results','csv');
  50.  
  51.  
  52. // Part.2 CALCULATE THE POSITIVE/NEGATIVE GROWTH OF URBAN BUILT-UP AREAS
  53. // Add a band containing image data as years
  54. function createTimeBand(img){
  55.   return img.addBands(img.metadata('system:time_start').divide(1e18));}
  56. //fit a linear trend to the nighttime lights collection
  57. var getyear=function(year){
  58.   return ee.Date.fromYMD({
  59.     day:1,month:1,year:year});};
  60. var linear_fit=function(y1,y2){
  61.   var startyear = getyear(y1);
  62.   var endyear = getyear(y2);
  63.   var timeband=light_dataset.filterDate(startyear,endyear).map(createTimeBand);
  64.   var linearfit=timeband.select(['system:time_start','stable_lights']).reduce(ee.Reducer.linearFit()).clip(boundary);
  65.   return linearfit;
  66. };
  67. var fit_china_92_95=linear_fit(1992,1995);
  68. var fit_china_96_10=linear_fit(1996,2010);
  69. var fit_china_10_14=linear_fit(2010,2014);
  70. // Display Trend in Red(positive growth),Blue(negative growth), and green(stable brightness)
  71. var visParams={min:0,max:[0.18,20,-0.18],bands:['scale','offset','scale'],opacity:0.8};
  72. Map.addLayer(fit_china_92_95,visParams,"linear_fit_92_95");
  73. Map.addLayer(fit_china_96_10,visParams,"linear_fit_96_10");
  74. Map.addLayer(fit_china_10_14,visParams,"linear_fit_10_14");
  75.  
  76. // Visualize the rate of urban sprawl and shrinkage by province
  77. // extract the pixels with positive growth (The slope of linear regression is positive) and negative growth (The slope of linear regression is negative)
  78. var positive_growth=function(img){
  79.   var img_scale=img.select("scale");
  80.   var positive=img_scale.multiply(img_scale.gt(0));
  81.   return positive;
  82. };
  83. var negative_growth=function(img){
  84.   var img_scale=img.select("scale");
  85.   var negative=img_scale.multiply(img_scale.lt(0).multiply(-1));
  86.   return negative;
  87. };
  88. var positive_10_14=positive_growth(fit_china_10_14);
  89. var negative_10_14=negative_growth(fit_china_10_14);
  90. Map.addLayer(positive_10_14,{min: 3.0,max: 60.0,palette:['000000','FF6666'],opacity:0.8},"positive_10_14");
  91. Map.addLayer(negative_10_14,{min: 3.0,max: 60.0,palette:['000000','006699'],opacity:0.8},"negative_10_14");
  92.  
  93. //calculate the area of positive_growth and negative_growth by province, respectively
  94. function count_positive(Chi){
  95.   var area =positive_10_14.reduceRegion({
  96.     reducer:ee.Reducer.sum(),
  97.     geometry:Chi.geometry(),
  98.     scale:500,
  99.     bestEffort:true,
  100.     maxPixels:1e9});
  101.   var Chi_copy=Chi;
  102.   return Chi_copy.set({LitArea:area.get('scale')});
  103. }
  104. function count_negative(Chi){
  105.   var area =negative_10_14.reduceRegion({
  106.     reducer:ee.Reducer.sum(),
  107.     geometry:Chi.geometry(),
  108.     scale:500,
  109.     bestEffort:true,
  110.     maxPixels:1e9});
  111.   var Chi_copy=Chi;
  112.   return Chi_copy.set({LitArea:area.get('scale')});
  113. }
  114. var area_positive=China.map(count_positive);
  115. var area_negative=China.map(count_negative);
  116. //calculate the ratio of positive_growth/negative_growth area and total area
  117. function density(Chi){
  118.   var Area=ee.Number(Chi.area());
  119.   var ratio=ee.Number(Chi.get('LitArea')).divide(Area);
  120.   var Chi_copy=Chi;
  121.   return Chi_copy.set({"ratio":ratio});}
  122. var ratio_positive=area_positive.map(density);
  123. var ratio_negative=area_negative.map(density);
  124. // plot the choropleth map of ratio by province
  125. var choropleth=function(fc){
  126.   var img=fc.reduceToImage(["ratio"],ee.Reducer.first());
  127.   return img;
  128. };
  129. var img_positive=choropleth(ratio_positive);
  130. Map.addLayer(img_positive,{max:130,min:0,palette:["FFFFFF","E99C9A","E96D6A","C02A26","5D0606"],opacity:1},"positive growth area/total area");
  131. var img_negative=choropleth(ratio_negative);
  132. Map.addLayer(img_negative,{max:130,min:0,palette:["FFFFFF","8FB2D1","679FD1","20517C","092C4C"],opacity:1},"negative growth area/total area");
  133. //identify provinces with the largest urban expansion rate and contraction rate
  134. var top_positive=ratio_positive.limit(10,"ratio",false);
  135. print(top_positive);
  136. var top_negative=ratio_negative.limit(10,"ratio",false);
  137. print(top_negative);
  138. //export the result table
  139. Export.table.toDrive({
  140.   collection:top_positive,
  141.   description: "top_positive",
  142.   fileNamePrefix: "top_positive",
  143.   fileFormat: "KML",
  144.   selectors: ["Province","ratio"]
  145. });
  146. Export.table.toDrive({
  147.   collection:top_negative,
  148.   description: "top_negative",
  149.   fileNamePrefix: "top_negative",
  150.   fileFormat: "KML",
  151.   selectors: ["Province","ratio"]
  152. });
  153.  
  154. // Part.3 CHARACTERISTIC OF URBAN CONTRACTION & EXPANSION AREAS
  155. //Load the selected two province
  156. var selected = selected;
  157. Map.addLayer(selected,{color:'FFFFFF'},'selected_province');
  158. Map.centerObject(selected,4);
  159. print(selected);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement