3

I'd like to know how can I export as a featureCollection the List (line 31) of the following script:

https://code.earthengine.google.com/5f453a2f3cf39c4be49ac4f4df47ac08

6
  • Thank you! I will try to fix this issue. I thought that these variables ab, cc, var, cn, si, wr should also be taken from squares. Anyway, this is all really great!
    – pmj
    May 10, 2021 at 19:26
  • 1
    In my Editing Note you have a solution.
    – xunilk
    May 10, 2021 at 21:33
  • Fantastic! My only remaining question, if possible, is why 'var squares = ee.FeatureCollection(list.map(computeSquares)).flatten();' increases the number of elements from 312 to 460.
    – pmj
    May 11, 2021 at 11:44
  • 1
    I know the answer. Most of the countries have one feature in each feature collection, however, some of them have 3, 6 or 8 features (i.e. Russia). You can corroborate that with 'size' method of ee.FeatureCollection. However, main issue is 'sum'. Values are strange in many case and, I think is a scale issue,
    – xunilk
    May 11, 2021 at 12:26
  • 1
    It could be 'crs'. Image and country bounds don't coincide. For this reason there are many features with zero population. My suggestion is to use my former approach here: gis.stackexchange.com/questions/395815/… . It works as expected.
    – xunilk
    May 11, 2021 at 13:53

1 Answer 1

3

I assume that "export" means to Google Drive. In this case, you can add following lines (complete code here) after line 31.

// Apply your function to each item in the list by using the map() function => 31 line
var squares = ee.FeatureCollection(list.map(computeSquares)).flatten();

//print(squares);

// Export the FeatureCollection to a SHP file.
Export.table.toDrive({
  collection: squares,
  folder: 'GEE_Folder',
  description:'squares',
  fileFormat: 'SHP'
});

//Map.addLayer(ee.FeatureCollection(squares));
//Map.centerObject(squares);

//--------------end function/map--------

It worked for me and in 12 minutes exported to Google Drive about 150 MB of this vector file (squares) as shapefile.

Editing Note:

Following code also exports fields as CSV.

//population
var pop = ee.Image("JRC/GHSL/P2016/POP_GPW_GLOBE_V1/2015");

//Boundaries
var worldcountries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');

var list = worldcountries.aggregate_array('country_na');

//print(list);

//------Function/map---------

var computeSquares = function(country) {

  var filterCountry = ee.Filter.inList('country_na',[country]);
  var bound = worldcountries.filter(filterCountry);

  var popcountry = pop.clip(bound);

  var sumpop =popcountry.reduceRegions({
    collection:bound,
    reducer: ee.Reducer.sum(),
    scale:250,
  
   });

  return sumpop;

};

// Apply your function to each item in the list by using the map() function.
var squares = ee.FeatureCollection(list.map(computeSquares)).flatten();

//print(squares);

// Export the FeatureCollection to a SHP file.
Export.table.toDrive({
  collection: squares,
  folder: 'GEE_Folder',
  description:'squares',
  fileFormat: 'SHP'
});

//Map.addLayer(ee.FeatureCollection(squares));
//Map.centerObject(squares);

//abbreviati; country_co; country_na; sum; system:index; and wld_rgn
var ab = squares.aggregate_array('abbreviati');
var cc = squares.aggregate_array('country_co');
var cn = squares.aggregate_array('country_na');
var sum = squares.aggregate_array('sum');
var si = squares.aggregate_array('system:index');
var wr = squares.aggregate_array('wld_rgn');

var count = squares.size();

var seq = ee.List.sequence(0, count.subtract(1));

var fields = seq.map(function(ele) {
  
  var list = ee.List([]);
  
  list = list.add(ab.get(ele))
    .add(cc.get(ele))
    .add(cn.get(ele))
    .add(sum.get(ele))
    .add(si.get(ele))
    .add(wr.get(ele));
  
  return list;
  
});

print("fields", fields);

var myFeatures = ee.FeatureCollection(fields.map(function(el){
  el = ee.List(el); // cast every element of the list
  return ee.Feature(null, {
    'abbreviati': ee.String(el.get(0)),
    'country_co': ee.String(el.get(1)),
    'country_na': ee.String(el.get(2)),
    'sum': ee.Number(el.get(3)),
    'system:index': ee.String(el.get(4)),
    'wld_rgn': ee.String(el.get(5))
  });
}));

//print(myFeatures);

// Export features, specifying corresponding names.
Export.table.toDrive(myFeatures,
"export_fields", //my task
"GEE_Folder", //my export folder
"fields_CSV",  //file name
"CSV");


//--------------end function/map--------

An extract of produced CSV can be observed as follows:

system:index,abbreviati,country_co,country_na,sum,wld_rgn,.geo
0_00000000000000000000,,CD,Chad,0.0,Africa,
1_00000000000000000014,Mal.,MI,Malawi,0.0,Africa,
2_00000000000000000015,Zam.,ZA,Zambia,0.0,Africa,
3_00000000000000000016,Zimb.,ZI,Zimbabwe,0.0,Africa,
4_00000000000000000017,Bots.,BC,Botswana,0.0,Africa,
5_00000000000000000018,Nam.,WA,Namibia,0.0,Africa,
6_00000000000000000019,Ang.,AO,Angola,0.0,Africa,
7_0000000000000000001a,Buru.,BY,Burundi,0.0,Africa,
8_0000000000000000001b,Rw.,RW,Rwanda,0.0,Africa,
9_0000000000000000001c,S. Afr.,SF,South Africa,0.0,Africa,
10_0000000000000000001d,Leso.,LT,Lesotho,0.0,Africa,
11_0000000000000000001e,Swaz.,WZ,Swaziland,0.0,Africa,
12_00000000000000000022,May.,MF,Mayotte,0.0,Africa,
13_00000000000000000094,,NG,Niger,0.0,Africa,
14_00000000000000000095,,SU,Sudan,0.0,Africa,
15_00000000000000000096,,LY,Libya,0.0,Africa,
16_00000000000000000097,,UU,Koualou Area,0.0,Africa,
17_00000000000000000098,Tun.,TS,Tunisia,0.0,Africa,
18_000000000000000000a8,,EG,Egypt,0.0,Africa,
19_000000000000000000a9,,EG,Bir Tawil,0.0,Africa,
20_000000000000000000aa,,SU,Halaib Triangle,0.0,Africa,
21_000000000000000000c0,Erit.,ER,Eritrea,0.0,Africa,
22_000000000000000000c1,Eth.,ET,Ethiopia,0.0,Africa,
23_000000000000000000c2,Dji.,DJ,Djibouti,0.0,Africa,
24_000000000000000000c3,Som.,SO,Somalia,0.0,Africa,
25_000000000000000000c4,S. Sudan,OD,South Sudan,0.0,Africa,
26_000000000000000000c5,,UU,Abyei Area,0.0,Africa,
27_000000000000000000c6,,KE,Kenya,0.0,Africa,
28_000000000000000000c7,Ug.,UG,Uganda,0.0,Africa,
29_000000000000000000c8,Tanz.,TZ,Tanzania,0.0,Africa,
30_000000000000000000c9,Moz.,MZ,Mozambique,0.0,Africa,
31_000000000000000000ca,Como.,CN,Comoros,0.0,Africa,
32_000000000000000000cb,Madag.,MA,Madagascar,0.0,Africa,
33_000000000000000000dc,C. Ver.,CV,Cabo Verde,0.0,Africa,
34_000000000000000000de,W. Sah.,WI,Western Sahara,0.0,Africa,
35_000000000000000000df,Maur.,MR,Mauritania,0.0,Africa,
36_000000000000000000e0,Mor.,MO,Morocco,0.0,Africa,
37_000000000000000000e1,Sp.,SP,Spain (Canary Is),0.0,Africa,
38_000000000000000000e2,Port.,PO,Portugal (Madeira Is),0.0,Africa,
39_000000000000000000e4,Sp.,SP,Spain (Africa),0.0,Africa,
40_000000000000000000e8,Alg.,AG,Algeria,0.0,Africa,
41_000000000000000000e9,,ML,Mali,0.0,Africa,
42_000000000000000000ea,Burk.,UV,Burkina Faso,0.0,Africa,
43_000000000000000000eb,,TO,Togo,0.0,Africa,
44_000000000000000000ec,,GH,Ghana,0.0,Africa,
45_000000000000000000ed,C. d Iv.,IV,Cote d'Ivoire,0.0,Africa,
46_000000000000000000ee,Gui.,GV,Guinea,0.0,Africa,
47_000000000000000000ef,Sen.,SG,Senegal,0.0,Africa,
48_000000000000000000f0,Gam.,GA,"Gambia, The",0.0,Africa,
49_000000000000000000f1,Gui.-Bis.,PU,Guinea-Bissau,0.0,Africa,
50_000000000000000000f2,S. Leo.,SL,Sierra Leone,0.0,Africa,
51_000000000000000000f3,Liber.,LI,Liberia,0.0,Africa,
52_000000000000000000f4,,BN,Benin,0.0,Africa,
53_000000000000000000f5,Nig.,NI,Nigeria,0.0,Africa,
54_000000000000000000f6,Camer.,CM,Cameroon,0.0,Africa,
55_000000000000000000f7,Equa. Gui.,EK,Equatorial Guinea,0.0,Africa,
56_000000000000000000f8,S. To. & Prin.,TP,Sao Tome & Principe,0.0,Africa,
57_000000000000000000f9,,GB,Gabon,0.0,Africa,
58_000000000000000000fa,Rep. of the Congo,CF,Rep of the Congo,0.0,Africa,
59_000000000000000000fb,Cen. Afr. Rep.,CT,Central African Rep,0.0,Africa,
60_000000000000000000fc,Dem. Rep. of the Congo,CG,Dem Rep of the Congo,0.0,Africa,
61_0000000000000000003e,H.K.,HK,Hong Kong,0.0,E Asia,
62_0000000000000000003f,,MC,Macau,0.0,E Asia,
63_00000000000000000040,Tai.,TW,Taiwan,0.0,E Asia,
64_00000000000000000041,,UU,Senkakus,0.0,E Asia,
65_00000000000000000042,,JA,Japan,1.2367127457851154E8,E Asia,
66_00000000000000000043,S. Kor.,KS,"Korea, South",0.0,E Asia,
67_00000000000000000044,N. Kor.,KN,"Korea, North",0.0,E Asia,
68_00000000000000000045,,UU,Korean Is. (UN Jurisdiction),0.0,E Asia,
69_00000000000000000046,,CH,China,4.13626724095709E8,E Asia,
69_00000000000000000048,,CH,China,1.5496324354785228E7,E Asia,
69_000000000000000000d5,,CH,China,6541.133977252492,E Asia,
69_000000000000000000d9,,CH,China,113014.24493963121,E Asia,
70_00000000000000000046,,CH,China,4.13626724095709E8,E Asia,
70_00000000000000000048,,CH,China,1.5496324354785228E7,E Asia,
70_000000000000000000d5,,CH,China,6541.133977252492,E Asia,
70_000000000000000000d9,,CH,China,113014.24493963121,E Asia,
71_00000000000000000046,,CH,China,4.13626724095709E8,E Asia,
71_00000000000000000048,,CH,China,1.5496324354785228E7,E Asia,
71_000000000000000000d5,,CH,China,6541.133977252492,E Asia,
71_000000000000000000d9,,CH,China,113014.24493963121,E Asia,
72_00000000000000000046,,CH,China,4.13626724095709E8,E Asia,
72_00000000000000000048,,CH,China,1.5496324354785228E7,E Asia,
72_000000000000000000d5,,CH,China,6541.133977252492,E Asia,
72_000000000000000000d9,,CH,China,113014.24493963121,E Asia,
73_00000000000000000129,,UU,Liancourt Rocks,0.0,E Asia,
74_0000000000000000000a,Rus.,RS,Russia,1.021918526464285E8,Europe,
74_00000000000000000011,Rus.,RS,Russia,5249239.941191522,Europe,
74_0000000000000000000f,Rus.,RS,Russia,2.6806786216873176E7,N Asia,
74_00000000000000000010,Rus.,RS,Russia,2725952.8460667613,N Asia,
74_00000000000000000122,Rus.,RS,Russia,1057696.7556383794,N Asia,
74_00000000000000000128,Rus.,RS,Russia,4781155.418985548,N Asia,
75_0000000000000000000c,Ukr.,UP,Ukraine,0.0,Europe,
76_0000000000000000000d,Rom.,RO,Romania,0.0,Europe,
77_0000000000000000000e,Mol.,MD,Moldova,0.0,Europe,
78_0000000000000000000a,Rus.,RS,Russia,1.021918526464285E8,Europe,
78_00000000000000000011,Rus.,RS,Russia,5249239.941191522,Europe,
78_0000000000000000000f,Rus.,RS,Russia,2.6806786216873176E7,N Asia,
78_00000000000000000010,Rus.,RS,Russia,2725952.8460667613,N Asia,
78_00000000000000000122,Rus.,RS,Russia,1057696.7556383794,N Asia,
78_00000000000000000128,Rus.,RS,Russia,4781155.418985548,N Asia,
79_00000000000000000012,Fin.,FI,Finland,5410341.968123293,Europe,
80_00000000000000000013,Nor.,NO,Norway,4835198.770938944,Europe,
.
.
.
3
  • Thanks so much! Is there a way to export 'squares' as a '.csv' file containing the following six fields: abbreviati; country_co; country_na; sum; system:index; and wld_rgn?
    – pmj
    May 10, 2021 at 14:44
  • 1
    If they are properties of these feature collection it is possible, I will see in only one feature for developing corresponding function for extracting them.
    – xunilk
    May 10, 2021 at 14:53
  • Is there a way to do this wothout using "flatten()"? Flatten repeats some Feature.Collection over estimating the total value. For example values for China, Russia, etc are double counted. As one can see, the number of feature collections increased from 312 to 460 using flatten. Thanks.
    – pmj
    Sep 22, 2022 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.