我正在使用功能组图——散点图。图表上显示的图例(系列)会自动按照首次出现的值排序,因此它看起来像一个随机数(附图)。我想通过从小到大对值进行排序来自定义图例,但一无所获(案例:1 - 11)。有没有人有办法解决吗?
原始代码:
var target_image = ee.Image("users/noahdornier/clustering_indo_1000"),
world = ee.Image("users/noahdornier/WORLD_1000M"),
aoi =
/* color: #d63000 */
/* shown: false */
ee.Geometry.MultiPolygon(
[[[[102.75002721516972, 1.4680288033344118],
[100.70657018391972, 2.8951792673714145],
[100.33303502766972, 2.8951792673714145],
[97.49856237141972, 5.393312517501195],
[95.12551549641972, 6.0601299904573915],
[94.90578893391972, 5.513615104828367],
[95.83962682454472, 4.057567062888153],
[96.68557409016972, 3.4875182905271256],
[97.55349401204472, 2.313507800956766],
[98.77297643391972, 0.699129134064055],
[99.16848424641972, -0.09186910187547472],
[100.99221471516972, -2.7056091356781256],
[104.05740026204472, -5.772994218064749],
[105.27688268391972, -7.072133582451994],
[107.03469518391972, -7.791135467379643],
[117.31789830891972, -9.236175756560042],
[119.06472448079472, -8.910710527895949],
[121.45974401204472, -9.095177102155404],
[125.25002721516972, -8.411103906919744],
[125.51369909016972, -8.095800665646815],
[126.79909948079472, -7.98701766219563],
[126.96389440266972, -7.638719155840991],
[126.76614049641972, -7.366412562627525],
[125.50271276204472, -7.67138440949234],
[124.85451940266972, -7.943496322921548],
[120.73464635579472, -8.063168831121013],
[116.03249791829472, -7.8999703703438895],
[114.23218234261151, -7.507266981562539],
[116.09985812386151, -7.289369791162866],
[116.05591281136151, -6.635054350951491],
[110.65063937386151, -6.2420419154349815],
[106.19019015511151, -5.280139755400546],
[108.91479953011151, -3.330186407666076],
[109.02466281136151, -2.057206806319892],
[106.98120578011151, -2.013289217009391],
[105.20142062386151, 1.1283132572995478],
[104.67407687386151, 1.457818715679925]]],
[[[101.66382296761151, -5.3895271312027235],
[102.30102999886151, -5.892449236809539],
[102.74048312386151, -5.433276587913964],
[102.10327609261151, -5.017532047202134]]],
[[[100.91675265511151, -3.3521217149314784],
[98.82935031136151, 0.4032852058161235],
[97.04956515511151, 3.1263453887943085],
[95.02808078011151, 3.2141015008427587],
[96.23657687386151, 2.006881952438559],
[97.99438937386151, -0.4096962540592795],
[99.84009249886151, -3.615306120450092],
[100.67505343636151, -3.7249440696109857]]],
[[[115.59448703011151, 4.529422807622796],
[114.34204562386151, 1.8092377906205146],
[112.16675265511151, 1.7213888877333188],
[110.51880343636151, 1.282087779087179],
[109.42017062386151, 2.621611575144256],
[108.12378390511151, 5.0986941574905105],
[107.44263156136151, 4.485613417733092],
[107.88208468636151, 3.4773237331185536],
[107.57446749886151, 2.9508113440379873],
[109.02466281136151, 1.919042912195218],
[108.43140109261151, 0.5131449569469554],
[108.49731906136151, -1.8595685043657795],
[109.70581515511151, -2.0791651491500165],
[109.79370578011151, -3.1327470370299695],
[115.02319796761151, -4.4043790834229775],
[115.63843234261151, -5.170733075792206],
[116.45142062386151, -4.251009078762999],
[117.00073703011151, -1.486193214428067],
[117.85767062386151, -0.8491217863949141],
[117.81372531136151, 0.29342397197210923],
[119.35181124886151, 0.8207409344719643],
[118.75854953011151, 2.42404943787239],
[118.05542453011151, 4.529422807622796]]],
[[[106.62964328011151, 3.6746925010488893],
[105.24536593636151, 3.1921631723678536],
[106.93726046761151, -0.12405511724463059],
[108.25561984261151, 0.24947915374714633]]],
[[[109.94751437386151, -5.93616063269345],
[110.73852999886151, -6.04542389857532],
[110.76050265511151, -5.673841131322457],
[109.99145968636151, -5.608242390819361]]],
[[[112.93579562386151, -5.914305366572922],
[112.82593234261151, -5.586374500703499],
[112.47436984261151, -5.673841131322457],
[112.49634249886151, -5.958015032025398]]],
[[[114.49585421761151, -5.783155743493844],
[114.82544406136151, -5.170733075792206],
[114.60571749886151, -4.842401171675471],
[114.18823703011151, -5.170733075792206],
[114.21020968636151, -5.761294497968417]]],
[[[127.32788546761151, 4.989256337425725],
[125.13061984261151, 4.770326339428847],
[124.93286593636151, 2.0727581473660655],
[123.76831515511151, 1.172249712857546],
[120.38452609261151, 1.6335359366920006],
[119.46167453011151, 0.5131449569469554],
[118.49487765511151, -3.1327470370299695],
[119.04419406136151, -4.119524740377622],
[118.84644015511151, -4.601522482592413],
[117.94556124886151, -4.951863387833757],
[118.20923312386151, -5.892449236809539],
[117.92358859261151, -7.463696176314802],
[118.71460421761151, -7.26757418818246],
[119.32983859261151, -6.9623261320668455],
[118.64868624886151, -5.695705724363025],
[120.27466281136151, -5.892449236809539],
[120.23071749886151, -7.093171558566238],
[120.86792453011151, -7.398331836441381],
[121.26343234261151, -6.8532603662055385],
[120.99976046761151, -6.001721217743898],
[120.58227999886151, -3.9880186664357504],
[122.16431124886151, -6.023572997842173],
[123.19702609261151, -5.805016144263823],
[124.29565890511151, -6.285724870233669],
[125.08667453011151, -6.154665101797492],
[123.41675265511151, -3.900336215534262],
[123.43872531136151, -3.1546866268768827],
[121.96655734261151, -2.057206806319892],
[123.72436984261151, -2.5621656951626965],
[125.70190890511151, -2.188952216217448],
[125.92163546761151, -2.8694370112140253],
[125.61401828011151, -3.768795447702829],
[127.04224093636151, -4.185269669573605],
[129.2614792176115, -3.856491546231025],
[130.4040573426115, -3.8345683617962245],
[131.6125534363615, -5.039420136243412],
[131.6784714051115, -5.892449236809539],
[132.9309128113615, -6.2420419154349815],
[133.4802292176115, -5.214498038782749],
[132.1179245301115, -5.017532047202134],
[131.6564987488615, -3.7249440696109857],
[130.9314010926115, -3.1327470370299695],
[129.1735885926115, -2.4524076065961915],
[126.60278781136151, -2.496311945232268],
[126.69067843636151, -1.5520880109802844],
[123.94409640511151, -1.1786595732958396],
[123.13110812386151, -0.058137235446128754],
[124.82300265511151, 0.3593408675863514],
[125.54810031136151, 1.282087779087179],
[126.09741671761151, 3.4115248458765195],
[127.56958468636151, 3.784323102607278]]],
[[[141.1267135926115, -9.4197108929551],
[141.1047409363615, -2.3645946322802285],
[138.1384323426115, -1.0907861927655182],
[134.8864792176115, -0.27786271874933127],
[131.9201706238615, -0.058137235446128754],
[131.3928268738615, 1.457818715679925],
[130.5139206238615, 0.24947915374714633],
[129.1296432801115, 0.7767998935047579],
[129.1296432801115, 2.906923436614606],
[127.54761203011151, 2.42404943787239],
[126.16333468636151, 1.304054839767967],
[125.96558078011151, 0.7328583956222846],
[127.02026828011151, 1.3699548585860928],
[127.04224093636151, 0.3813130647399897],
[126.77856906136151, -0.8271514811527663],
[127.37183078011151, -2.0352481611992785],
[129.0637253113615, -1.881529451829153],
[130.2941940613615, -2.4524076065961915],
[131.9201706238615, -3.1327470370299695],
[132.7331589051115, -4.294832242379294],
[133.8977096863615, -4.711023157552299],
[134.2712448426115, -4.492004961830662],
[136.6882370301115, -5.039420136243412],
[137.8967331238615, -5.848734398940255],
[138.3581589051115, -7.202181047230838],
[137.3254440613615, -7.768599262162074],
[137.3254440613615, -8.70366783238695],
[139.1272018738615, -8.660226068436923],
[140.1379440613615, -8.595054022613919]]],
[[[127.41577609261151, -8.551599761063049],
[130.6237839051115, -8.529870770145667],
[134.8864792176115, -7.616174839480993],
[135.4797409363615, -5.345774522641384],
[134.0954635926115, -4.929972381198272],
[133.6560104676115, -6.0891230493766955],
[132.0959518738615, -6.656879355978588],
[130.3161667176115, -6.001721217743898],
[127.17407687386151, -7.158580374296971],
[126.99829562386151, -8.029769030738152]]],
[[[123.66943820198648, -9.4630609181104],
[122.64770968636151, -10.458554191320776],
[120.98877413948648, -9.398033838096726],
[118.97827609261151, -9.051018395642876],
[118.67065890511151, -9.744701766492971],
[121.04370578011151, -10.91197781716022],
[122.91138156136151, -11.278530407999517],
[124.46505708754266, -10.254535925842475],
[125.72388156136151, -9.202879619423301],
[124.95483859261151, -8.768821017436395]]]]),
image = ee.Image("users/insgis/noah/moef_image_v1");
Map.addLayer(target_image);
Map.addLayer(world);
function generatesScatter(feature, xProperty, yProperty, series, title, hAxis_title, xAxis_title, hMinMax, vMinMax) {
var scatter_chart = ui.Chart.feature.groups(feature, xProperty, yProperty, series)
.setChartType('ScatterChart')
.setOptions({
title: title,
pointSize: 2,
dataOpacity: 0.4,
hAxis: {
viewWindow: {
min: hMinMax[0],
max: hMinMax[1],
},
'title': hAxis_title,
titleTextStyle: { italic: false, bold: true },
},
vAxis: {
viewWindow: {
min: vMinMax[0],
max: vMinMax[1],
},
'title': xAxis_title,
titleTextStyle: { italic: false, bold: true }
},
});
return scatter_chart
}
function stratifiedSampling(img_input, numPoints, classBand, classVal, pts, scale, region) {
// Sampling
var str_point = img_input.stratifiedSample({
numPoints: numPoints,
classBand: classBand,
// Class to be sampled
classValues: classVal,
// Points each class
classPoints: [pts, pts, pts, pts, pts, pts, pts, pts, pts, pts, pts],
scale: scale,
// region: region,
geometries: false
})
return str_point
}
// Class references
// Define a dictionary that maps the original values to new numeric values
var classDict = ee.Dictionary({
"Bush / Shrub": 1,
"Swamp Shrub": 2,
"Plantation Forest": 3,
"Primary Swamp Forest": 4,
"Secondary Swamp Forest": 5,
"Primary Mangrove Forest": 6,
"Dryland Agriculture": 7,
"Secondary Mangrove Forest": 8,
"Primary Dry Land Forest": 9,
"Secondary Dry Land Forest": 10,
"Shrub-Mixed Dryland Farm": 11
});
// List of class
var classList = classDict.keys()
// List of class values
// Using for remapping raster value from 0 to 10 --> 1 to 11
var valueList = classDict.values().sort()
// SEA
var SEA_select = world.select('VV_mean', 'VV_stdDev', 'VH_mean', 'VH_stdDev');
// K-Means Clustering
// Create training dataset.
var training = SEA_select.sample({
region: aoi,
scale: 1000,
numPixels: 10000
});
// Start unsupervised clusterering algorithm and train it.
var kmeans = ee.Clusterer.wekaKMeans(11).train(training);
// Cluster the input using the trained clusterer.
var result = SEA_select.cluster(kmeans);
// Remapping class raster
var cluster_image = result.select("cluster")
.remap([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], valueList)
.rename("cluster")
// Add SEA Band to image input
var cluster_concat = cluster_image.addBands(world)
// Cluster - Str sampling
var cluster_sample = stratifiedSampling(
cluster_concat, 1100, "cluster", valueList, 100, 1000, aoi
)
// Cluster - mean chart
var cluster_mean_chart = generatesScatter(
cluster_sample,
"VV_mean",
"VH_mean",
"cluster",
"VV Mean .vs VH Mean - Cluster Results",
"VV Mean",
"VH Mean",
[-22, -1.5],
[-30, -5]
)
print("Cluster Mean:",cluster_mean_chart)
解决方案:
很多时候我们会出现上面的问题,如果我们时需要按照图例的大小进行排序的话,我们就会遇到上面的问题,要更改默认行为,您必须跳过一些步骤才能以正确的排序顺序创建 Google Charts API 数据表。
修改后的代码:
// We create a list of rows for the data table
// Each row would contain 12 values, 1 X-axis valuie and 11 Y-axis series values
// Only 1 of the Y-axis series will be populated
var rows = valueList.map(function(cluster) {
var features = cluster_sample.filter(ee.Filter.eq('cluster', cluster))
return features.toList(100).map(function(f) {
var f = ee.Feature(f);
var cluster = f.get('cluster');
var vv = f.get('VV_mean');
var vh = f.get('VH_mean');
var empty = ee.List([{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}])
var row = empty.set(ee.Number(cluster).subtract(1), {v: vh});
var fullrow = ee.List([{v: vv}]).cat(row)
return {c: fullrow}
})
}).flatten()
// Rows is server-side object created using EE API
// Convert it to cliend-side javascript object
rows.evaluate(function(rowsResult) {
print(rowsResult)
var dataTable = {
cols: [
{id: 'vv', label: 'vv', type: 'number'},
{id: 'vh1', label: '1', type: 'number'},
{id: 'vh2', label: '2', type: 'number'},
{id: 'vh3', label: '3', type: 'number'},
{id: 'vh4', label: '4', type: 'number'},
{id: 'vh5', label: '5', type: 'number'},
{id: 'vh6', label: '6', type: 'number'},
{id: 'vh7', label: '7', type: 'number'},
{id: 'vh8', label: '8', type: 'number'},
{id: 'vh9', label: '9', type: 'number'},
{id: 'vh10', label: '10', type: 'number'},
{id: 'vh11', label: '11', type: 'number'},
],
rows: rowsResult
};
var chart = ui.Chart(dataTable, 'ScatterChart')
.setOptions({
title: 'VV Mean .vs VH Mean - Cluster Results',
pointSize: 2,
dataOpacity: 0.4,
hAxis: {
viewWindow: {
min: -22,
max: -1.5,
},
'title': 'VV Mean',
titleTextStyle: { italic: false, bold: true },
},
vAxis: {
viewWindow: {
min: -30,
max: -5,
},
'title': 'VH Mean',
titleTextStyle: { italic: false, bold: true }
},
});
;
print(chart)
})
修改后的图里结果:
这里面用到的函数:
ui.Chart(dataTable, chartType, options, view, downloadable)
A chart widget.
dataTable (List<List<Object>>|Object|String, optional):
A 2-D array of data or a Google Visualization DataTable literal. See: http://developers.google.com/chart/interactive/docs/reference#DataTable
chartType (String, optional):
The chart type; e.g 'ScatterChart',
'LineChart', and 'ColumnChart'. For the complete list of charts, see: https://developers.google.com/chart/interactive/docs/gallery
options (Object, optional):
An object defining chart style options such as:
view (Object, optional):
Sets a DataView initializer object, which acts as a filter over the underlying data. See: https://developers.google.com/chart/interactive/docs/reference#DataView
downloadable (Boolean, optional):
Whether the chart can be downloaded as CSV, SVG, and PNG. Defaults to true.