你好,
我试图通过屏蔽值的上下 10 个百分位数来消除计算出的 NDVI 数据集中的异常值,但我在第 398 行中不断收到错误消息,显示第 398 行: ndvi.gt不是函数。 我怎样才能解决这个问题?
代码链接:
https://code.earthengine.google.com/f5a139f1c30df78322e7136c63c42e2e
原始代码:
var table = ee.FeatureCollection("users/selenachav5/mangrove_vector"), Upper = /* color: #d63000 */ /* shown: false */ /* displayProperties: [ { "type": "rectangle" }, { "type": "marker" }, { "type": "rectangle" } ] */ ee.Geometry({ "type": "GeometryCollection", "geometries": [ { "type": "Polygon", "coordinates": [ [ [ -81.2407135715856, 25.79366283798797 ], [ -81.2407135715856, 25.77758769332227 ], [ -81.22148749736685, 25.77758769332227 ], [ -81.22148749736685, 25.79366283798797 ] ] ], "geodesic": false, "evenOdd": true }, { "type": "Point", "coordinates": [ -81.32741223482145, 25.738523746255495 ] }, { "type": "Polygon", "coordinates": [ [ [ -81.82460825109085, 25.96361611727523 ], [ -81.82460825109085, 24.946857002999202 ], [ -80.1574329581221, 24.946857002999202 ], [ -80.1574329581221, 25.96361611727523 ] ] ], "geodesic": false, "evenOdd": true } ], "coordinates": [] }), Lower = /* color: #98ff00 */ee.Geometry.Point([-81.16010932282697, 25.231743823283935]), Mid = /* color: #0b4a8b */ee.Geometry.Point([-81.08192793682238, 25.35792290478201]), TTI = /* color: #00ffff */ee.Geometry.Point([-81.56369714187413, 25.872789041185186]), geometry = /* color: #0000ff */ /* shown: false */ ee.Geometry.Point([-80.93190442232145, 25.542910956897835]), imageVisParam = {"opacity":1,"bands":["constant"],"max":1,"palette":["ff0000","a52a2a","008000"]}, geometry2 = /* color: #d63000 */ /* shown: false */ /* displayProperties: [ { "type": "rectangle" }, { "type": "rectangle" } ] */ ee.Geometry.MultiPolygon( [[[[-81.81911508702835, 25.95620775711364], [-81.81911508702835, 24.956817897095913], [-80.15468637609085, 24.956817897095913], [-80.15468637609085, 25.95620775711364]]], [[[-80.9045032706221, 26.01052489646315], [-80.9045032706221, 25.93644985046701], [-80.8715442862471, 25.93644985046701], [-80.8715442862471, 26.01052489646315]]]], null, false), table2 = ee.FeatureCollection("users/selenachav5/nps_boundary"), image2 = ee.ImageCollection("LANDSAT/LC08/C02/T1_TOA"), image3 = ee.ImageCollection("LANDSAT/LC08/C02/T1_RT_TOA"), image4 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2"); print(table.limit(10), 'table') Map.addLayer(table, {}, 'raw table',0) // Get bounding box for your geometry var bbox = table.geometry().bounds(); Map.addLayer(bbox, {}, 'bounding box') // Convert mangrove to image var mangroveImg = ee.Image(0).paint(table, 1); Map.addLayer(mangroveImg, {min:0, max:1}, 'mangroveImg') var enpbound = ee.Image(0).paint(table2, 1); Map.addLayer(mangroveImg, {min:0, max:1}, 'enpbound') // Now you can map over the collection and mask out all non-mangrove pixels function maskNonMangrove(i){ return i.updateMask(mangroveImg).copyProperties(i); } function maskENP(i){ return i.updateMask(enpbound).copyProperties(i); } function maskL8(im) { var qa = im.select('QA_PIXEL'); var mask = qa.eq(21824); return im.updateMask(mask).copyProperties(im); } //var water = function(image) { //var qa = image.select('QA_PIXEL'); /// Check that the cloud bit is off. // See https://www.usgs.gov/media/files/landsat-8-9-olitirs-collection-2-level-1-data-format-control-book // var mask = qa.bitwiseAnd(7).eq(0); // return image.updateMask(mask); //}; //var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR"); var Before = image4 .filterBounds(geometry2) .filterDate('2013-04-01', '2017-09-08') .map(maskL8) // this is fine .map(maskNonMangrove) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var After = image4 .filterBounds(geometry2) .filterDate('2017-09-09', '2023-01-01') .map(maskL8) // this is fine .map(maskNonMangrove) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var Thirteen = image4 .filterBounds(geometry2) .filterDate('2013-01-01', '2013-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) //.map(water) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var ThirteenM = image4 .filterBounds(geometry2) .filterDate('2013-01-01', '2013-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var FourteenPercent = image4 .filterBounds(geometry2) .filterDate('2014-01-01', '2014-12-31') .map(function(im) {return (im.normalizedDifference(['SR_B5', 'SR_B4']))}) .reduce(ee.Reducer.percentile([10])) var Fourteen = image4 .filterBounds(geometry2) .filterDate('2014-01-01', '2014-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) .map( function(im) {return im.select('nd').updateMask(im.select('nd').gt(FourteenPercent))}) var FourteenM = image4 .filterBounds(geometry2) .filterDate('2014-01-01', '2014-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var Fifteen = image4 .filterBounds(geometry2) .filterDate('2015-01-01', '2015-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var FifteenM = image4 .filterBounds(geometry2) .filterDate('2015-01-01', '2015-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var Sixteen = image4 .filterBounds(geometry2) .filterDate('2016-01-01', '2016-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var SixteenM = image4 .filterBounds(geometry2) .filterDate('2016-01-01', '2016-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var Seventeen = image4 .filterBounds(geometry2) .filterDate('2017-01-01', '2017-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var SeventeenM = image4 .filterBounds(geometry2) .filterDate('2017-01-01', '2017-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var Eighteen = image4 .filterBounds(geometry2) .filterDate('2018-01-01', '2018-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var EighteenM = image4 .filterBounds(geometry2) .filterDate('2018-01-01', '2018-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var Nineteen = image4 .filterBounds(geometry2) .filterDate('2019-01-01', '2019-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var NineteenM = image4 .filterBounds(geometry2) .filterDate('2019-01-01', '2019-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var Twenty = image4 .filterBounds(geometry2) .filterDate('2020-01-01', '2020-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var TwentyM = image4 .filterBounds(geometry2) .filterDate('2020-01-01', '2020-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var TwentyOne= image4 .filterBounds(geometry2) .filterDate('2021-01-01', '2021-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var TwentyTwo = image4 .filterBounds(geometry2) .filterDate('2022-01-01', '2022-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var TwentyOneM= image4 .filterBounds(geometry2) .filterDate('2021-01-01', '2021-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var TwentyTwoM = image4 .filterBounds(geometry2) .filterDate('2022-01-01', '2022-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) //.map(water) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var DecadePercent = image4 .filterBounds(geometry2) .filterDate('2013-01-01', '2023-12-31') .map(function(im) {return (im.normalizedDifference(['SR_B5', 'SR_B4']))}) .reduce(ee.Reducer.percentile([10])) var Decade= image4 .filterBounds(geometry2) .filterDate('2013-01-01', '2022-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) .map(function(im) {var opticalBands = im.select('SR_B.').multiply(0.0000275).add(-0.2);return im.addBands(opticalBands,null,true)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) var DecadeM= image4 .filterBounds(geometry2) .filterDate('2013-01-01', '2022-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) .map(function(im) {return im.select(['SR_B.']).multiply(0.0000275).add(-0.2)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B6']))}) var DecadeO= image4 .filterBounds(geometry2) .filterDate('2013-01-01', '2022-12-31') .map(maskL8) // this is fine .map(maskNonMangrove) .map(maskENP) .map(function(im) {var opticalBands = im.select('SR_B.').multiply(0.0000275).add(-0.2);return im.addBands(opticalBands,null,true)}) .map(function(im) {return im.addBands(im.normalizedDifference(['SR_B5', 'SR_B4']))}) .map( function(im) {return im.select('nd').updateMask(im.select('nd').gt(FourteenPercent))}) //.map( function(im) {return im.select('nd').updateMask(im.select('nd').gt(FourteenPercent))}) // Add mean NDVI to the map to inspect //Map.addLayer(Before.select('nd').mean(), {min:0, max:1, palette:['red','brown','green']}, 'mean NDVI mangroves Before') //Map.addLayer(After.select('nd').mean(), {min:0, max:1, palette:['red','brown','green']}, 'mean NDVI mangroves After') Map.addLayer(Thirteen.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2013') Map.addLayer(Fourteen.select('nd'), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2014') Map.addLayer(Fifteen.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2015') Map.addLayer(Sixteen.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2016') Map.addLayer(Seventeen.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2017') Map.addLayer(Eighteen.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2018') Map.addLayer(Nineteen.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2019') Map.addLayer(Twenty.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2020') Map.addLayer(TwentyOne.select('nd').mean(), {min:0, max:0.8, palette:['#ffffcc','#c2e699','#78c679','#31a354','#006837']}, 'NDVI 2021') Map.addLayer(ThirteenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2013') Map.addLayer(FourteenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2014') Map.addLayer(FifteenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2015') Map.addLayer(SixteenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2016') Map.addLayer(SeventeenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2017') Map.addLayer(EighteenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2018') Map.addLayer(NineteenM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2019') Map.addLayer(TwentyM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Mositure 2020') Map.addLayer(TwentyOneM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2021') Map.addLayer(TwentyTwoM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Moisture 2022') Map.addLayer(DecadeM.select('nd').mean(), {min:0, max:0.8, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c']}, 'Decade Mositure') var vis = {min: 0, max: 0.8, palette:'#ffffcc,#c2e699,#78c679,#31a354,#006837'} var palette= ['#ffffcc','#c2e699','#78c679','#31a354','#006837'] var vis2 = {min: 0, max: 0.8, palette:'#eff3f,#bdd7e7,#6baed6,#3182bd,#08519c'} var palette2= ['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c'] function makeColorBarParams(palette2) { return { bbox: [0, 0, 1, 0.1], dimensions: '100x10', format: 'png', min: 0, max: 1, palette:['#eff3ff','#bdd7e7','#6baed6','#3182bd','#08519c'], }; } var colorBar = ui.Thumbnail({ image: ee.Image.pixelLonLat().select(0), params: makeColorBarParams(vis2.palette2), style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'}, }); // Create a panel with three numbers for the legend. var legendLabels = ui.Panel({ widgets: [ ui.Label(vis2.min, {margin: '4px 8px'}), ui.Label( ((vis2.max-vis2.min) / 2+vis2.min), {margin: '4px 8px', textAlign: 'center', stretch: 'horizontal'}), ui.Label(vis2.max, {margin: '4px 8px'}) ], layout: ui.Panel.Layout.flow('horizontal') }); var legendTitle = ui.Label({ value: 'Normalized Difference Mositure Index', style: {fontWeight: 'bold'} }); // Add the legendPanel to the map. var legendPanel = ui.Panel([legendTitle, colorBar, legendLabels]); Map.add(legendPanel); var ndviB = Before.select('nd') var ndvia = After.select('nd') var ndvi = Decade.select('nd') var ndviOL= DecadeO.select('nd') var P_95_NDVI = ndvi.reduce(ee.Reducer.percentile([95])); var P_05_NDVI = ndvi.reduce(ee.Reducer.percentile([5])); var NDVI_P = ndvi.updateMask(ndvi.gt(P_05_NDVI).and(ndvi.lt(P_95_NDVI))); var Moisture = DecadeM.select('nd') var Change = Thirteen.select('nd');Fourteen.select('nd');Fifteen.select('nd');Sixteen.select('nd');Seventeen.select('nd');Eighteen.select('nd');Nineteen.select('nd');Twenty.select('nd');TwentyOne.select('nd') //print(ui.Chart.image.series(ndviB,Upper,ee.Reducer.mean(), 30, 'system:time_start')) //print(ui.Chart.image.series(ndviA,Upper,ee.Reducer.mean(), 30, 'system:time_start')) //print(ui.Chart.image.series(ndviB,Lower,ee.Reducer.mean(), 30, 'system:time_start')) //print(ui.Chart.image.series(ndviA,Lower,ee.Reducer.mean(), 30, 'system:time_start')) //print(ui.Chart.image.series(ndviB,Mid,ee.Reducer.mean(), 30, 'system:time_start')) print(ui.Chart.image.series(ndviA,Mid,ee.Reducer.mean(), 30, 'system:time_start')) //print(ui.Chart.image.series(ndviB,TTI,ee.Reducer.mean(), 30, 'system:time_start')) //print(ui.Chart.image.series(ndviA,TTI,ee.Reducer.mean(), 30, 'system:time_start')) print(ui.Chart.image.series(ndvi, Mid, ee.Reducer.mean(), 30, 'system:time_start')) print(ui.Chart.image.series(ndviOL, Mid, ee.Reducer.mean(), 30, 'system:time_start')) print(ui.Chart.image.series(DecadeM, Mid,ee.Reducer.mean(), 30, 'system:time_start')) // You need to submit it as an export task
问题1:Line 398: ndvi.gt is not a function
这个问题主要的是影像集合和影像之间的差异,function不能直接作用在影像上,所以我们对之前的代码进行分析就可以了,正确代码如下:
var ndviB = Before.select('nd') var ndvia = After.select('nd') var ndvi = Decade.select('nd').mosaic() var ndviOL= DecadeO.select('nd').mosaic() var P_95_NDVI = ndvi.reduce(ee.Reducer.percentile([95])); var P_05_NDVI = ndvi.reduce(ee.Reducer.percentile([5])); var NDVI_P = ndvi.updateMask(ndvi.gt(P_05_NDVI).and(ndvi.lt(P_95_NDVI))); var Moisture = DecadeM.select('nd') var Change = Thirteen.select('nd');Fourteen.select('nd');Fifteen.select('nd');Sixteen.select('nd');Seventeen.select('nd');Eighteen.select('nd');Nineteen.select('nd');Twenty.select('nd');TwentyOne.select('nd')
问题2:
Error generating chart: No features contain non-null values of "system:time_start".
这个问题请查看:
参看:
(1039条消息) Google Earth Engine(GEE)——求随机点中的长时序的各波段的折线图(附给矢量集合添加时间属性)_此星光明的博客-CSDN博客