1

I'm attempting to add one constant image iEmpty to a masked image iCTSurfaceWater, such that the value of the resulting image at a particular pixel is equal to iCTSurfaceWater, and regions where iCTSurfaceWater is mapped are equal to the constant value in iEmpty. I've tried adding, unmasking, and mosaicking, all without luck.

How can I accomplish this?

var findConnecticut /*:FeatureCollection */ = function() {

  var fcUSWatersheds = ee.FeatureCollection('ft:1IXfrLpTHX4dtdj1LcNXjJADBB-d93rkdJ9acSEWK');

  var fcLowerConnecticut = fcUSWatersheds.filterMetadata("name", "equals", "Lower Connecticut");
  var fcUpperConnecticut = fcUSWatersheds.filterMetadata("name", "equals", "Upper Connecticut");
  var fcConnecticut = fcLowerConnecticut.merge(fcUpperConnecticut);
  fcConnecticut = fcConnecticut.union();

  return fcConnecticut;
}

var fConnecticut = ee.Feature(findConnecticut().first());
Map.addLayer(fConnecticut, {}, "Connecticut River Basin");
print(fConnecticut);

var iCTSurfaceWater = iGlobalSurfaceWater.clip(fConnecticut);
print(iCTSurfaceWater);
Map.addLayer(iCTSurfaceWater, {}, "Water in CT");

var iEmpty = ee.Image(0).clip(fConnecticut);

print(iEmpty);

//var icCTAll = ee.ImageCollection([iEmpty, iCTSurfaceWater]);
var iCTAll = iCTSurfaceWater.add(iEmpty);

print(iCTAll);

Map.addLayer(iCTAll, {}, "All of CT");
  • Do you mean to fill all the masked areas, i.e. not mapped as surface water, with 0's? – Kersten Feb 26 '17 at 17:52
  • Yes, that's exactly what I'm trying to do. – tbadams45 Feb 27 '17 at 3:26
2

To replace masked pixels with a different value you can use the .unmask() function. Without arguments it will replace all masked pixels with 0.

I'm guessing you are using the JRC Global Surface Water layer, but the import statement was missing from your example. The function works the same on all images.

Here's the adapted script: https://code.earthengine.google.com/3a5bcd547fe4ff7ae083e004ef68e905

And the code:

var iGlobalSurfaceWater = ee.Image("JRC/GSW1_0/GlobalSurfaceWater")

var findConnecticut /*:FeatureCollection */ = function() {
  var fcUSWatersheds = ee.FeatureCollection('ft:1IXfrLpTHX4dtdj1LcNXjJADBB-d93rkdJ9acSEWK');

  var fcLowerConnecticut = fcUSWatersheds.filterMetadata("name", "equals", "Lower Connecticut");
  var fcUpperConnecticut = fcUSWatersheds.filterMetadata("name", "equals", "Upper Connecticut");
  var fcConnecticut = fcLowerConnecticut.merge(fcUpperConnecticut);
  fcConnecticut = fcConnecticut.union();

  return fcConnecticut;
}

var fConnecticut = ee.Feature(findConnecticut().first());
Map.addLayer(fConnecticut, {}, "Connecticut River Basin");
print(fConnecticut);

var iCTSurfaceWater = iGlobalSurfaceWater.unmask().clip(fConnecticut);
print(iCTSurfaceWater);
Map.addLayer(iCTSurfaceWater, {}, "Water in CT");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.