1

I want to extract the elevation values and generate contour lines from SRTM DEM at a 100 m intervals. However, I was able to create the contour lines using a code available here. But I am not able to interpret the exported result (shapefile) format. The attribute of the exported result is shown below.

Attribute

I am not able to understand, what the field "count" mean. Also, I want to extract the elevation values corresponding to each contour line. Is it possible to extract the elevation values in Google Earth Engine using the generated contours?

I am stuck after this much progress.

var srtm = ee.Image('USGS/SRTMGL1_003');

var lines = ee.List.sequence(0, 10000, 100)
 
var contourlines = lines.map(function(line) {
var mycontour = srtm
.convolve(ee.Kernel.gaussian(5, 3))
.subtract(ee.Image.constant(line)).zeroCrossing()
.multiply(ee.Image.constant(line)).toFloat()
.clip(table)
 
return mycontour.mask(mycontour);
})
 
contourlines = ee.ImageCollection(contourlines).mosaic()
 
Map.addLayer(contourlines, {min: 0, max: 5000, palette:['00ff00', 'ff0000']}, 'contours')
Map.centerObject(table,5)
var contour = contourlines.toInt();
var vectors = contour.reduceToVectors({
  geometry: table, 
  scale: 30,
  maxPixels: 1e13
})


Export.table.toDrive({
  collection: vectors,
  description: "ContourLines",
  folder:"DEM",
  //geometry: NE,
  fileFormat: "SHP",
})

'table' is the extent of my study area.

1 Answer 1

1

Your code works as intended, but it is returning more information than you want. The function ee.Image.reduceToVectors does two things. First, it looks for homogeneous regions within an integer image, which should be the first band in the input ee.Image:

The first band is expected to be an integer type; adjacent pixels will be in the same segment if they have the same value in this band.

Second, it applies a reducer to all the other bands within the image. The default reducer is ee.Reducer.countEvery. The documentation for this reducer is lacking.. with the only description being:

Returns a Reducer that computes the number of inputs.

What this means for the reduceToVectors operation is that it counts the number of pixels (at the scale you specified, e.g. 30 m) within each polygon. For example, the following feature (obtained using your code above) shows a count value of 16:

enter image description here

This is actually a good default value for the reducer. The "label" corresponds to the value from the first band, i.e. the value of the "homogeneous region". In your case, it actually corresponds to the elevation value, in integer format. The DEM you used seems to actually come in integer format anyway (although it is presented as float). Combining this with the fact that you are generating the contours at 100m of elevation steps, in this specific example the labels correspond exactly to the elevation value you want.

In other cases, you might want to use a different reducer to gather information about other bands in the image. That way, you will get the "label" property (based on the value of the first band, which must be integer), and other properties depending on the other bands and which reducer you use.

3
  • Thanks for your explanation. However, I'm still not able to understand the elevation values corresponding to each of the contour generated. Will you please elaborate a little more. Commented Jun 9, 2023 at 4:33
  • You are using the NASA SRTM Digital Elevation 30m image, which has an elevation band. Then, you are building contour lines, which by definition if you follow this line you will remain at the same elevation. Using reduceToVectors, you are generating a feature collection where each feature contains (1) the geometry of the contour line, (2) the elevation value corresponding to the contour line. In this specific example, the elevation value is being stored as a property named "label" in each feature. Commented Jun 10, 2023 at 17:09
  • Got it! Thanks Oliver! Commented Jun 12, 2023 at 5:24

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.