1

I would like to convert a shp to raster via GEE. My SHP has several polygons, which have a value between 1 and 2 in the attribute table.

I would like the output raster to assume values 1 and 2 according to the value of the polygons.

I know there is a "toReduce" command, but I didn't understand how to do that, even with the tutorial provided by Google.

Context

I would like to add a mask for an image rating. In this shp, the value 1 would be ignored by the classifier.

4
  • Perhaps if you add some context on what you are trying to achieve, it might be possible to provide a working solution. Dec 19, 2020 at 6:59
  • hello @Sean Roulet I would like to add a mask for an image rating. In this shp, the value 1 would be ignored by the classifier. Dec 19, 2020 at 12:06
  • Would it work if you had a raster with values 1 and 2, taken from the polygon properties? Then you could use that raster as a mask. Dec 20, 2020 at 14:24
  • Hello friend, using a raster would be a good idea, I did it. But I would like to test the results using a shp -> raster transformation. Dec 20, 2020 at 21:14

1 Answer 1

2

Using a raster as a mask to apply to your source image is the best way to do it so that the results are quick. Earth Engine works best when dealing with data in ImageSpace.

To mask pixels that are contained within a shapefile this is the process that I would use.

// This is an image with just 1.  You use it to build your mask.    
var myImage = ee.Image.constant(1);

// This would be your shape file. (had to create it from a bogus geometry to test it.    
var myFeature = ee.Feature(geometry);
    
// here you create an image that is a mask from your features.
var newImage = myImage.clip(myFeature).mask().not();
    
Map.addLayer(newImage,{},"maskToApply");
    
// Using the mask from above, you update the mask of the source Image 
// I use the image of constants created above for an example
var exitImage = myImage.updateMask(newImage);
    
// When added to the map, you can see that the image has a new mask, where all pixels that are within the polygons are masked.
Map.addLayer(myImage, {},"Original Image");
Map.addLayer(exitImage,{}, "Exit Image");

PS: Kudos on this goes to Rodrigo E. Principe who wrote the code that I inspired myself from here: Masking pixels inside the polygons using Google Earth Engine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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