Search code examples
gispolygonrasterpointsgoogle-earth-engine

Raster to Point conversion: how to convert every single pixel? on Google Earth Engine


Good day. I am trying to convert a raster to points using Google Earth Engine. My raster has one band (clusters) and it has been clipped to my ROI. I am aware of the reduceToVectors function on Google Earth Engine, but as I understand, this function creates areas with the same adjacent value, whereas what I want is to create as many points as there are pixels.

So far, I have tried different versions of:

var vectors = image.reduceToVectors({
  reducer : null,
  geometry : treat,
  scale:30,
  crs :image.projection().getInfo().crs,
  geometryType : 'centroid',
  labelProperty : 'null',
  eightConnected: false,
  maxPixels: 1e15
});

Thanks a lot for your help.


Solution

  • ee.Image.sample returns a point for every pixel.

    var vectors = image.sample({
      region: treat,
      geometries: true,  // if you want points
    });
    

    If you do not specify a scale and crs, it will use each pixel in the input image's original resolution. If you do, it will sample at the given scale instead.

    Demonstration script:

    var region = ee.Geometry.Polygon(
      [[[-110.00683426856995, 40.00274575078824],
        [-110.00683426856995, 39.99948706365032],
        [-109.99576210975647, 39.99948706365032],
        [-109.99576210975647, 40.00274575078824]]], null, false);
    var image = ee.Image('CGIAR/SRTM90_V4');
    Map.setCenter(-110, 40, 16);
    Map.addLayer(image, {min: 1000, max: 2000}, 'SRTM');
    
    var vectors = image.sample({
      region: region,
      geometries: true,
    });
    print(vectors);
    Map.addLayer(ee.FeatureCollection([region]).style({"color": "white"}));
    Map.addLayer(vectors);
    

    https://code.earthengine.google.com/625a710d6d315bad1c2438c73bde843b