3

I want to download the shapefile of an area in GEE tool/application, It is getting printed in the console but not able to download it using the label. I tried this by using the following answer to the similar question for images, you can check this as well, if required https://gis.stackexchange.com/a/344726/221102

// Define a panel
var mainPanel = ui.Panel({
  style: {width: '150px'}
});

//title to be shown
var title = ui.Label({
  value: 'Brahamaputra Islands',
  style: {'fontSize': '24px'}
});



///////define a variable with button and add the whole process as function in it//////////////////
var load = ui.Button('Load Islands');
load.onClick(function() {
    mainPanel.clear() // .remove() for a single layer and .clear for all panel
    var ed=ee.Date(Date.now());
    var eb=ed.advance(ee.Number(-35),'day')
    
    ///////////////// Dynamic World ////////////////////////
    var dw = ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1')
                 .filterDate(eb, ed)
                 .filterBounds(geometry);
    var img= ee.Image('GOOGLE/DYNAMICWORLD/V1/20230531T042711_20230531T043753_T46RCP');
    // Select label layer
    var classification = dw.select('label');
    var dwComposite = classification.reduce(ee.Reducer.mode());
    var water = dwComposite.eq(0);
    
    ////////////////////////////////masking//////////////////////////////
    water=water.selfMask().clip(geometry)
    print(water,"water")

    var bandNames = dwComposite.bandNames();
    print('dwComposite Band names:', bandNames);
    
    var mask = dwComposite.select('label_mode').eq(0);
    var nw = mask.updateMask(dwComposite).clip(geometry)

    // Convert to vectors.
    var vector = nw.reduceToVectors({
      geometry: geometry,
      crs: water.projection(),
      scale: 10,
      maxPixels: 1e13,
      geometryType: 'polygon',
      eightConnected: false, // it takes4connectedpixels if false
      labelProperty: 'zone',
      reducer: ee.Reducer.countEvery()
    });
    
    // Add area property to dissolved features(non water).
    var addArea = function(feature) {
      return feature.set({areaSqKm: feature.geometry().area(1).divide(10e6)});
    };
    var areaAdded = vector.map(addArea);

    ////////////////FILTER AREA/////////////////////////////////////////////////////////////////////////////
    var Area = areaAdded.filter(ee.Filter.and(
      ee.Filter.gte('areaSqKm', 0.0001),
      ee.Filter.lte('areaSqKm', 3.5)
    ));

    // Export to Drive.
    Export.table.toDrive({
      collection: areaAdded,
      description: 'non_water_vector',
      folder: 'missingvalues_hold',
      fileFormat: 'SHP',
      maxVertices: 1000000000
    });
    // Get a download URL for the FeatureCollection.
    var downloadUrl = Area.getDownloadURL({
      format: 'CSV',
      filename: 'Islands'
    });
    print('URL for downloading FeatureCollection as CSV', downloadUrl);
    var label= ui.Label
    label.setUrl(downloadUrl);
    label.style().set({shown: true});
  // Add UI elements to the Map.
  var downloadButton = ui.Button('Download viewport', downloadUrl);
  var urlLabel = ui.Label('Download', {shown: false});
  var panel = ui.Panel([downloadButton, urlLabel]);
Map.add(panel);

 });

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//add widgets to the panel
mainPanel.add(title)
mainPanel.add(load);
ui.root.add(mainPanel);

Map.setCenter(91.3021, 26.1741,12)

here is the link to my code. https://code.earthengine.google.com/5b79b9e77630be0ffe1d8ebc2acd3da6

Please help me regarding this.

0

2 Answers 2

2

Your issue is produced because for downloading a vector layer the downloadArgs are completely different for images. It can be corroborated at GEE documentation. So, following code works as expected. I used my own layer but it also will work with your polygon layer obtained from application of 'reduceToVectors' method.

Map.centerObject(table, 10);
Map.addLayer(table, null, 'Vector');

// Define a function to generate a download URL of the image for the
// viewport region. 
function downloadVect() {
  var viewBounds = ee.Geometry.Rectangle(Map.getBounds());
  var downloadArgs = {
    format: 'kml',
    filename: 'polygon8'
 };
  var url = ee.FeatureCollection(table).getDownloadURL(downloadArgs);
  urlLabel.setUrl(url);
  urlLabel.style().set({shown: true});
}

// Add UI elements to the Map.
var downloadButton = ui.Button('Download viewport', downloadVect);
var urlLabel = ui.Label('Download', {shown: false});
var panel = ui.Panel([downloadButton, urlLabel]);
Map.add(panel);

After running code in above link, I got following result where the downloading link worked without any problem.

enter image description here

Downloaded kml can also be observed in QGIS in the following picture:

enter image description here

Editing Note:

By the way, it looks that getDownloadURL() only can be used for obtaining small files (See this post).

I modified my former code for obtaining your 'Brahamaputra Islands' (there are 286 features) and it was impossible with getDownloadURL(). However, the file could be exported satisfactorily to Drive. Modified code can be found here.

Visualized in GEE:

enter image description here

Visualized in QGIS 3 after exporting to Drive:

enter image description here

2

There are several errors in your code.

  1. In this part of the code, you need to call ui.Label to get the actual label. So instead of this:
    ...
    print('URL for downloading FeatureCollection as CSV', downloadUrl);
    var label= ui.Label
    label.setUrl(downloadUrl);
    ...
    
    you need this...
    ...
    print('URL for downloading FeatureCollection as CSV', downloadUrl);
    var label= ui.Label() // note here the calling "()"
    label.setUrl(downloadUrl);
    ...
    
  2. The onClick argument of ui.Button must be a callable not a string, and the label must have a value, otherwise you wont see it. So change this part:
    ...
    print('URL for downloading FeatureCollection as CSV', downloadUrl);
    var label= ui.Label
    label.setUrl(downloadUrl);
    label.style().set({shown: true});
    // Add UI elements to the Map.
    var downloadButton = ui.Button('Download viewport', downloadUrl);
    ...
    
    with this:
    ...
    print('URL for downloading FeatureCollection as CSV', downloadUrl);
    var label= ui.Label('Download') // call ui.Label constructor to get the actual label, and give it a caption, in this case is "Download"
    label.setUrl(downloadUrl);
    label.style().set({shown: false});
    // Add UI elements to the Map.
    var downloadButton = ui.Button('Download viewport',
        function() {
            panel.add(label);
            label.style().set({shown: true})
        }
    ); // here i provide the function that will be called when the button is clicked, what it does is to add the label to the panel
    var urlLabel = ui.Label('Download', {shown: false});
    var panel = ui.Panel([downloadButton, urlLabel]);
    ...
    

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.