-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. traObjExp
131 lines (106 loc) · 4.54 KB
/
2. traObjExp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Author: Afag Rizayeva
Date: 01-17-2022
Purpose: Export the objects overlapping with point grid for further labeling
Inputs: 2 Corona image layers manually imported in GEE
Point grid
Adjust the input files' directories
Run the code
Export the accuracies as csv files and the final classification map
*/
// Import two Corona images
// Rename the band names for afterward- ('da') and forward-facing ('df') cameras
var image2 = ee.Image('users/rizayeva/corona_2_5m/DS1011-1040DA_2_5m_JPEG_1band_EPSG32638').rename(['da']);
var image3 = ee.Image('users/rizayeva/corona_2_5m/DS1011-1040DF_2_5m_JPEG_1band_EPSG32638').rename(['df']);
// Concatenate the two images
var img = ee.Image.cat([image2, image3]);
print(img);
// print('Band names: ', img.bandNames()); // ee.List of band names
var img = ee.Image(img).divide(255);
// Map.addLayer(img, {bands: ["df", "df", "da"], gamma: 0.8}, 'DS_DADF');
// Center the map view
Map.centerObject(img.geometry(), 7);
// Import and visualize the point grid for extraction of the segment grid
var table4 = ee.FeatureCollection('users/rizayeva/grid5km_label_07142020_EPSG32638');
Map.addLayer(table4, {}, 'training points');
///////////// Run segmentation
// Set the seed grid size
var seeds = ee.Algorithms.Image.Segmentation.seedGrid(40);
// Run SNIC segmentation algorithm on the regular square grid
var snic = ee.Algorithms.Image.Segmentation.SNIC({
image: img,
size: 32,
compactness: 0.1,
connectivity: 4,
neighborhoodSize:256,
seeds: seeds
}).select(['da_mean', 'df_mean', 'clusters'], ['da', 'df', 'clusters']).reproject({crs: 'EPSG:4326', scale: 18});
// print('Snic band names: ', snic.bandNames()); // ee.List of band names
// Select clusters from segmentation results
var clusters = snic.select('clusters');
// Map.addLayer(clusters.randomVisualizer(), {}, 'clusters', false)
// Map.addLayer(snic, {bands: ['da', 'df'], min:0, max:1, gamma: 0.8}, 'means', false);
///////////// Vectorize the objects
// Create a cell grid across the image
var tiles = image2.addBands(image3).reduceToVectors({
reducer: ee.Reducer.mean(),
geometry: image2.addBands(image3).geometry(),
geometryType: 'bb',
scale: 12000,
eightConnected: false,
labelProperty: 'object',
});
Map.addLayer(tiles, {}, 'tiles2', false);
///////////// Create buffers for the cell grid across the image to prevent clipped segments later on
// Build the function
var buffer = function(feature) {
return feature.buffer(1000);};
// Map the function
var tilesbuffer = tiles.map(buffer);
// print('tilesbuffer', tilesbuffer);
// Get the total number of the cells across the image
var tilesbuffer_list =tilesbuffer.toList(tilesbuffer.size());
// print('tilesbuffer_list', tilesbuffer_list);
Map.addLayer(tilesbuffer, {}, 'tilesbuffer');
var size = tilesbuffer_list.size().getInfo();
// ee.Feature(collection.toList(3).get(1))
// Select the column from the training data that has unique values for each of the points in the point grid
var classes = ee.FeatureCollection(table4.select('FID_test_l'));
print('classes', classes.first());
///////////// Get segments overlapping with the point grid
// Define a spatial filter as geometries that intersect.
var spatialFilter = ee.Filter.intersects({
leftField: '.geo',
rightField: '.geo',
maxError: 10
});
// Define a save all join.
var saveAllJoin = ee.Join.saveAll({
matchesKey: 'FID_test_l',
});
// Create a for loop to extract all segments that overlap with the point grid
var trainobjs = [];
var i;
for (i = 0; i < size; i++) {
var trainingobjects = saveAllJoin.apply(clusters.reduceToVectors({
reducer: ee.Reducer.countEvery(),
geometry: ee.Feature(tilesbuffer_list.get(i)).geometry(),
scale: 2.5,
eightConnected: false,
maxPixels: 1e15
}), classes, spatialFilter);
trainobjs[i]=trainingobjects;
}
print('trainobjs', trainobjs);
// Convert the list of Feature collections into a Feature collection of Feature collections
var trainobjsfeat = ee.FeatureCollection(trainobjs);
print('Feature collection of feature collections - trainobjsfeat', trainobjsfeat);
// To get a Feature collection, flatten the Feature collection of Feature collections
var trainobjsfeat = trainobjsfeat.flatten();
print('Feature collection - trainobjsfeat', trainobjsfeat);
Map.addLayer(trainobjsfeat, {}, 'trainobjsfeat', false);
// Export segment grid that overlaps with the point grid in GeoJSON format
Export.table.toDrive({
collection: trainobjsfeat,//class,
description: 'trainobjsfeatclass_DS10',
fileFormat: 'GeoJSON'//'shp'
});