SHARE
TWEET

Untitled

a guest Sep 21st, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. vietnam = ee.FeatureCollection('users/servirmekong/countries/VNM_adm1')
  2. compositeSentinel2 = ee.Image('users/phamngochairs/NB_S2A_Spectral_2018_2018')
  3.  
  4. # export training data
  5. aquaculture =  ee.FeatureCollection("users/servirmekong/referenceData/aquaculture");
  6. barren = ee.FeatureCollection("users/servirmekong/referenceData/barren");
  7. cropland = ee.FeatureCollection("users/servirmekong/referenceData/cropland");
  8. deciduousForest = ee.FeatureCollection("users/servirmekong/referenceData/deciduousForest");
  9. evergreen = ee.FeatureCollection("users/servirmekong/referenceData/evergreen_forest");
  10. floodedForest = ee.FeatureCollection("users/servirmekong/referenceData/floodedForest");
  11. plantations = ee.FeatureCollection("users/servirmekong/referenceData/plantations");
  12. grass = ee.FeatureCollection("users/servirmekong/referenceData/grass");
  13. mangroves = ee.FeatureCollection("users/servirmekong/referenceData/mangrove");
  14. mixedForest = ee.FeatureCollection("users/servirmekong/referenceData/mixedForest");
  15. plantations = ee.FeatureCollection("users/servirmekong/referenceData/plantations");
  16. rice = ee.FeatureCollection("users/servirmekong/referenceData/rice");
  17. shrub = ee.FeatureCollection("users/servirmekong/referenceData/shrub");
  18. snow = ee.FeatureCollection("users/servirmekong/referenceData/snow");
  19. water = ee.FeatureCollection("users/servirmekong/referenceData/water");
  20. imperv = ee.FeatureCollection("users/servirmekong/referenceData/imperv");
  21. wetlands = ee.FeatureCollection("users/servirmekong/referenceData/wetlands");
  22. noRice = aquaculture.merge(barren).merge(cropland).merge(deciduousForest).merge(evergreen).merge(floodedForest).merge(plantations).merge(grass).merge(mangroves).merge(mixedForest).merge(shrub).merge(snow).merge(water).merge(imperv).merge(wetlands);
  23.  
  24. deltaProvience = ['Dong Thap', 'Tien Giang', 'Vinh Long', 'Tra Vinh', 'An Giang', 'Can Tho', 'Hau Giang', 'Soc Trang', 'Bac Lieu', 'Kien Giang', 'Ben Tre', 'Long An'];
  25. geometry = vietnam.filter(ee.Filter.inList('VARNAME_1', deltaProvience));
  26.  
  27. def _set_true_feature(feature):
  28.     return feature.set(RESPONSE, 1)
  29.  
  30. def _set_false_feature(feature):
  31.     return feature.set(RESPONSE, 0)
  32.  
  33. rice = rice.filterBounds(geometry).map(_set_true_feature).randomColumn();
  34. trainingRice = rice.filterMetadata('random', 'greater_than', 0.3);
  35. testingRice = rice.filterMetadata('random', 'less_than', 0.3);
  36.  
  37. noRice = noRice.filterBounds(geometry).map(_set_false_feature).randomColumn();
  38. trainingNoRice = noRice.filterMetadata('random', 'greater_than', 0.3);
  39. testingNoRice = noRice.filterMetadata('random', 'less_than', 0.3);
  40.  
  41.  
  42. compositeSentinel2 = compositeSentinel2.clip(geometry).neighborhoodToArray(kernel);
  43.  
  44. trainingRiceSampled = compositeSentinel2.sampleRegions(
  45.   collection = trainingRice,
  46.   properties = [RESPONSE],
  47.   scale = 10
  48. )
  49. trainingRiceList = trainingRice.toList(trainingRice.size());
  50.  
  51.  
  52. testingRiceSampled = compositeSentinel2.sampleRegions(
  53.   collection = testingRice,
  54.   properties = [RESPONSE],
  55.   scale = 10
  56. )
  57. testingRiceList = testingRice.toList(testingRice.size());
  58.  
  59. trainingNoRiceSampled = compositeSentinel2.sampleRegions(
  60.   collection = trainingNoRice,
  61.   properties = [RESPONSE],
  62.   scale = 10
  63. )
  64. trainingNoRiceList = trainingNoRice.toList(trainingNoRice.size());
  65.  
  66. testingNoRiceSampled = compositeSentinel2.sampleRegions(
  67.   collection = testingNoRice,
  68.   properties = [RESPONSE],
  69.   scale = 10
  70. )
  71. testingNoRiceList = testingNoRice.toList(testingNoRice.size());
  72.  
  73.  
  74. # //////////////////////////////
  75. # //Export all the training data (in many pieces), with one task per geometry.
  76.  
  77. # // These numbers determined experimentally.
  78. n = 100; # Number of shards in each polygon.
  79. N = 2000; # Total sample size in each polygon.
  80.  
  81.  
  82. #var selectors = Array('blue','green','red','nir','swir1','swir2','rice');
  83. selectors = compositeSentinel2.bandNames().getInfo();
  84. selectors.append(RESPONSE)
  85.  
  86. # trainingRice testingRice trainingNoRice testingNoRice
  87. # 11            6            11             6
  88.  
  89. #for g in range(trainingRice.size().getInfo()):
  90. states = {
  91.     'trainingRice': {
  92.         'm': 11,
  93.         'list': trainingRiceList,
  94.         'fn': _set_true_feature
  95.     },
  96.     'trainingNoRice': {
  97.         'm': 11,
  98.         'list': trainingNoRiceList,
  99.         'fn': _set_false_feature
  100.     },
  101.     'testingRice': {
  102.         'm': 6,
  103.         'list': testingRiceList,
  104.         'fn': _set_true_feature
  105.     },
  106.     'testingNoRice': {
  107.         'm': 6,
  108.         'list': testingNoRiceList,
  109.         'fn': _set_false_feature
  110.     }
  111. }
  112.  
  113. for state in states:
  114.   TRAINING_BASE = state
  115.   m = states[state]['m']
  116.   _list = states[state]['list']
  117.   fn = states[state]['fn']
  118.   for g in range(m):
  119.     geomSample = ee.FeatureCollection([])
  120.     for i in range(n):
  121.       feature = ee.Feature(_list.get(g))
  122.       sample = compositeSentinel2.sample(
  123.         region = feature.geometry(),
  124.         scale = 10,
  125.         numPixels = N / n, # Size of the shard.
  126.         seed = i,
  127.         tileScale = 8
  128.       ).map(fn)
  129.       geomSample = geomSample.merge(sample)
  130.    
  131.     desc = TRAINING_BASE + '_g' + str(g)
  132.     task = ee.batch.Export.table.toCloudStorage(
  133.       collection = geomSample,
  134.       description = desc,
  135.       bucket = BUCKET,
  136.       fileNamePrefix = desc,
  137.       fileFormat = 'TFRecord',
  138.       selectors = selectors
  139.     )
  140.     task.start()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top