GEE ——errors & debuggings (2023GEE峰会总结)

简介:

在gee中有三种错误,一种就是系统错误,也就是我们看到的会在JavaScript code editor中出现的错误,也就是在程序还没有启动之前就会提示的错误,而客户端错误则主要是会提示一些在代码过程中的错误,比如说没出现过的变量名称,另外就是服务器出席那的错误,也就是说,你的代码和你索要运行的结果之间的错误,比如说,原本这个影像中是没有这个波段的,但是你却使用了,或者说你输入的波段名称不对而导致的错误。所有的这里显示的错误就如下面这张图所显示的。前言 – 人工智能教程

GEE ——errors & debuggings (2023GEE峰会总结)_第1张图片

bug解决方案:

这里针对debug的解决方法无非就是限制性输出,也就是减少控制台输出的量,另外我们会使用到下面的一些函数来实现这个功能。

函数:

ee.Filter(filter)

Constructs a new filter. This constructor accepts the following args:

  • Another filter.

  • A list of filters (which are implicitly ANDed together).

  • A ComputedObject returning a filter. Users shouldn't be making these; they're produced by the generator functions below.

Arguments:

filter (Filter|List|Object, optional):

Optional filter to add.

Returns: Filter

first()

Returns the first entry from a given collection.

Arguments:

this:collection (FeatureCollection):

The collection from which to select the first entry.

Returns: Element

limit(max, propertyascending)

Limit a collection to the specified number of elements, optionally sorting them by a specified property first.

Returns the limited collection.

Arguments:

this:collection (Collection):

The Collection instance.

max (Number):

The number to limit the collection to.

property (String, optional):

The property to sort by, if sorting.

ascending (Boolean, optional):

Whether to sort in ascending or descending order. The default is true (ascending).

Returns: Collection

aside(func, var_args)

Calls a function passing this object as the first argument, and returning itself. Convenient e.g. when debugging:调用一个函数,将此对象作为第一个参数,并返回自身。例如,在调试时非常方便:

var c = ee.ImageCollection('foo').aside(print)

.filterDate('2001-01-01', '2002-01-01').aside(print, 'In 2001')

.filterBounds(geom).aside(print, 'In region')

.aside(Map.addLayer, {min: 0, max: 142}, 'Filtered')

.select('a', 'b');

Returns the same object, for chaining.

Arguments:

this:computedobject (ComputedObject):

The ComputedObject instance.

func (Function):

The function to call.

var_args (VarArgs):

Any extra arguments to pass to the function.

Returns: ComputedObject

GEE ——errors & debuggings (2023GEE峰会总结)_第2张图片

这里重点说一下aside函数,这个功能就是在你执行程序每一步的时候都可以一步步的让其输出到控制台中,最后到底检查时哪一行代码出现了问题:

var c = ee.ImageCollection('foo').aside(print)

.filterDate('2001-01-01', '2002-01-01').aside(print, 'In 2001')

.filterBounds(table).aside(print, 'In region').aside(print, 'xxxx')

.aside(Map.addLayer, {min: 0, max: 142}, 'Filtered')

.select('a', 'b');

print(c)

 GEE ——errors & debuggings (2023GEE峰会总结)_第3张图片

我们看一下简单的错误:

Early Errors -Corrected

image.set(days,image.get('system:time_start')/(60*60*24*1000))

Result Capture

image =image.set(.)

Casting

ee.Number(image.get('system:time_start'))

or

image.getNumber('system:time_start')

Javascript Operators   image.getNumber(.).divide(60*60*24**1000)

GEE ——errors & debuggings (2023GEE峰会总结)_第4张图片 

MAP function 错误

这里的map不能使用print或者getinfo或者export等函数的操作。

Return a Value

function(x){y=x.add(1);}

User-defined methods must return a value

Return an Element

function(x){return x.date()}

Collection.map:A mapped algorithm must return a Feature or Image.

Using getlnfo or print

function(x){return x.set('y',x.date().getInfo())}

Line 1:A mapped function's arguments cannot be used in client-side operation

Javascript branching

if(i==0){return 1 }else {return 2 }

Mapped Aggregations function(x){return x.reduceRegion({...maxPixels:1e9})}

GEE ——errors & debuggings (2023GEE峰会总结)_第5张图片

GEE ——errors & debuggings (2023GEE峰会总结)_第6张图片 

GEE ——errors & debuggings (2023GEE峰会总结)_第7张图片

Scaling Issues

Error:Image.reduceRegions:Computed value is too large.

Error:Image.classify:Feature null has a non-numeric value for property B1.Task timed out after 7200 seconds.

另外一些函数在使用过程中也指的注意:

How l Debug Your Code -my checklist

Low Hanging Fruit

getlnfo()

for loops

iterate()

toList()

Complex geometries

Don't clip

image.reduceToVectors()

image.reproject()

image.resample()

image.reduceResolution()

Joins

Collections

filterDate /filterBounds

calendarRange()without filterDate()

collection.geometry()

toBands()/toArray()

Aggregations

Tilescale in reduce*Combine reducers

image.reduceNeighborhood()

Neighborhood size使用较小的pixels作为参数

Use optimizations

How I Debug Your Code -my checklist

Distances

use fastDistanceTransform()

/ee.Image.pixelArea().sqrt()

Geometries

Specify an error marginSimplify if possible

bounds()vs.drawing a box

Discard them completely

Pre-caching before sampling

Classifier size(trees,training)

Never use Math.random()

 错误代码示范:

https://code.earthengine.google.com/34bdb87a407011a6c9b821fad47d7987

 GEE ——errors & debuggings (2023GEE峰会总结)_第8张图片

var l8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2").limit(400);

function reduceBands(x, acc) {
    return ee.Image(acc).addBands(x);
}

var iterateVersion = ee.Image(l8.iterate(reduceBands, ee.Image()));
print('Iterate: image has', iterateVersion.bandNames().length(), 'bands');

var toBandsVersion = l8.toBands();
print('toBands: image has', toBandsVersion.bandNames().length(), 'bands');

你可能感兴趣的:(GEE错误集,gee,debugs,errors,错误,数据,bug,云计算)