GEE中常常出现的bug和error总结

举报
此星光明 发表于 2023/11/05 00:44:33 2023/11/05
【摘要】 ​ 简介:在gee中有三种错误,一种就是系统错误,也就是我们看到的会在JavaScript code editor中出现的错误,也就是在程序还没有启动之前就会提示的错误,而客户端错误则主要是会提示一些在代码过程中的错误,比如说没出现过的变量名称,另外就是服务器出席那的错误,也就是说,你的代码和你索要运行的结果之间的错误,比如说,原本这个影像中是没有这个波段的,但是你却使用了,或者说你输入的波...

 简介:

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

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>|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<Object>):

Any extra arguments to pass to the function.

Returns: ComputedObject

这里重点说一下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)


我们看一下简单的错误:

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)


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})}

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

 

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');


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。