access to electricity

overlaying buildings with nightlight data

here we calculate the mean nightlight for 2021 and overlay it with a featurecollection on buildings in Cambodia. For each building we sample the nightlight layer.

// import districts
var districts = ee.FeatureCollection("projects/servir-mekong/admin/KHM_adm2");
// filter for district of interest
var dis = districts.filter(ee.Filter.eq("NAME_2", "Tbaeng Mean chey"));
// add districts to map
Map.addLayer(districts,{},"districts in Cambodia");

// import building dataset for cambodia and filter for district of interest
var buildings = ee.FeatureCollection("projects/servir-mekong/undp/KHM_buildingsv1").filterBounds(dis);

print("number of buildings",buildings.size());
// add to map
Map.addLayer(buildings,{},"buildings");

// import the nighlight dataset
var nightlight2021 = ee.Image(ee.ImageCollection("NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG").filterDate("2021-01-01","2021-12-31").select("avg_rad").mean()).rename("nightlight");

// add layer to map 
Map.addLayer(nightlight2021.clip(dis),{min:0,max:1,palette:['000000','700000','808080','FFFF00','ffffff','ffffff','ffffff']},"nightlights 2021");
Map.addLayer(nightlight2021.clip(buildings),{min:0,max:1,palette:['000000','700000','808080','FFFF00','ffffff','ffffff','ffffff']},"nightlights 2021 buildings");

Copy the code below to calculate the histogram

buildings = buildings.map(function(feat){
  var sample = nightlight2021.reduceRegion({reducer:ee.Reducer.mean(),geometry:feat.geometry(),scale:500})
  return feat.set("nightlight",sample.get("nightlight"))
})

var chart = ui.Chart.feature.histogram({features:buildings, property:"nightlight"})
print(chart)

Leave a Reply