3

I have applied unsupervised classification on an image in Google Earth Engine using k means clustering algorithm. I have made 15 classes and I have visualized those 15 classes by adding a map layer. It is perfect. All 15 classes are represented by a separate color using 'randomVisualizer'.

How can I convert this raster image into a vector feature collection with 15 objects?

All the polygons of class 1 to become a single object. So my feature collection would have 15 objects and I can select the each class with its id.If possible please demonstrate this via dummy example using any image and unsupervised classification.

3

One giant Multipolygon per class isn't going to scale very well, but it's not hard to produce that.

var geometry = ee.Geometry.Rectangle([29.1796875, 32.02670629333615, 32.607421875, 29.993002284551075]);
var landcover = ee.Image('MCD12Q1/MCD12Q1_005_2001_01_01').select('Land_Cover_Type_1');

// Run reduceToVectors per class by masking all other classes.
var classes = ee.List([0, 1, 2, 3, 4, 5, 6, 7, 8])
  .map(function(n) {
    var classImage = landcover.eq(ee.Number(n));
    var vectors = classImage.updateMask(classImage)
      .reduceToVectors({
        reducer: ee.Reducer.countEvery(), 
        geometry: geometry, 
        scale: 30,
        maxPixels: 1e8})
      .geometry();
    return ee.Feature(vectors, {"class": n});
  });
var result = ee.FeatureCollection(classes);
Map.addLayer(result);

But you'd be better off with 1 feature per polygon. In both cases, each feature will have a "label" identifying the class.

var geometry = ee.Geometry.Rectangle([29.1796875, 32.02670629333615, 32.607421875, 29.993002284551075]);
var landcover = ee.Image('MCD12Q1/MCD12Q1_005_2001_01_01').select('Land_Cover_Type_1');

var classes = landcover.reduceToVectors({
  reducer: ee.Reducer.countEvery(), 
  geometry: geometry, 
  scale: 30,
  maxPixels: 1e8
});
var result = ee.FeatureCollection(classes);
Map.addLayer(result);
2

You can convert an image into to vector feature collection by reducing homogenous regions. Given an image containing a band of labeled segments, ee.image.reduceToVectors() runs a reducer over the pixels in each segment producing a feature for each segment.

The input image should have an integer type in the first band. These will be your 15 colours. Adjacent pixels will be in the same segment if they have the same value in this band.

There are a number of parameters you can play with to get the best results. Consult the docs.

This should return a FeatureCollection with 15 features outlining your coloured regions.

  • This is the correct approach, but there's no guarantee you'll end up with 15 features. In fact, I'd find that highly unlikely. You'll end up with number of features equal to however many homogeneous patches of pixels there are in your region. Features will be identified by the 'label' property, but you'll get many such features with the same label, rather than a single feature with a multi-geometry, – Nicholas Clinton Sep 15 '17 at 16:51
  • You are right, ee.image.reduceToVectors() is not working. It does not contain the class information but only the number of polygons formed. I can't identify which polygon belongs to which class after unsupervised classification. – Rawail Naeem Sep 16 '17 at 16:37

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.