2

I would like to export these contour lines as .SHP but GEE says that variable collection must be a FeatureCollection.

Here is the complete code

The entire code

The error shows it must be featureCollection

2 Answers 2

2

contourlines is an image, so it cannot be exported as a shapefile. You can use ee.Image.reduceToVectors() to convert it into a feature collection, and then export that collection as a shapefile. This might not work for your large region. If you're having problems, try to tile your region and export each tile separately:

var vectors = contourlines.reduceToVectors({
  geometry: geometry, 
  scale: 30,
  maxPixels: 1e13
})

This exports just a small part of your region: https://code.earthengine.google.com/6343517fff22a2252fed8d2e349cde69

2
  • Hmm yeah, that's a more straightforward manner
    – Kuik
    Commented May 13, 2020 at 8:52
  • Thank you a lot. Yeah, that helps me a lot. I hadn't had a clue for doing this.
    – Sherwood
    Commented May 14, 2020 at 6:37
2

You have a raster file, the contour lines, so you are possibly looking to export an image:

Export.image.toDrive({
  image: contourlines,
  description: "ContourLines_de_Ecuador_y_Peru",
  folder:"8_AnaMea&Con",
  region: geometry,
  scale: 30
})

Then you can try to use external software to transform that image into a SHP file.

You can also transform that image into a multipoint SHP file. You will need some external software to transform that multipoint SHP file into valuable line strings. At least, this might give you an idea what difficulties there are transforming an image into a vector file:

var lines = ee.List.sequence(0, 4000, 250);
var proj = image.projection();

var contourlines = ee.FeatureCollection(lines.map(function(line) {
  // get an image
  var mycontour = image
    .convolve(ee.Kernel.gaussian(5, 3))
    .subtract(ee.Image.constant(line)).zeroCrossing() 
    .multiply(ee.Image.constant(line)).toFloat();
    mycontour = mycontour.mask(mycontour);

    // get the coordinates of the line in a requested projection
    var pixelLonLat = ee.Image.pixelLonLat().updateMask(mycontour);
    var latLonReproj = pixelLonLat.reproject(proj.crs(), null, proj.nominalScale());

    // extract lat/lon coordinates as a list
    var coordsList = ee.List(pixelLonLat.reduceRegion({
      reducer: ee.Reducer.toList(2), 
      geometry: geometry,
      scale: proj.nominalScale()
    }).values().get(0));

  // only return a MutliPoint geometry when there are points
  return ee.Feature(ee.Algorithms.If(coordsList.length(), 
          ee.Feature(ee.Algorithms.GeometryConstructors.MultiPoint(coordsList),{elevation: line})));
}))

print(contourlines)
Map.addLayer(ee.Image().paint(contourlines, 'elevation', 1), {min:0, max: 4000, palette: ['red','green']})

link code

1
  • Thank you, yes this is another way to do this.
    – Sherwood
    Commented May 14, 2020 at 6:35

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.