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.
Answer
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);
No comments:
Post a Comment