由于网状的R会话,中嵌入一个Python会话rgee和地球引擎的Python API共享相同的模块,类,函数和方法。换句话说,语法的逻辑是相同的,并且同样快(只需将.更改为$)。尽管如此,R 和 Python 的语言设计差异在特定场景下可能会导致一些问题。我们确定了三个潜在的错误案例。它们中的每一个都在下面进行了深入解释。
1.地图信息错误:
在以下两种情况下使用map方法时会出现此问题:(1)使用低于1.14的网状版本的用户(请更新!);(2) 如果您使用ee$List对象进行引导。例如:
library(rgee) ee$Initialize() mylist = ee$List$sequence(10) mylist$map(function(x) ee$Number(x)$add(1)) #> Error in py_call_impl(callable, dots$args, dots$keywords): RuntimeError: Evaluation error: argument "x" is missing, with no default. #> #> Detailed traceback: #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/apifunction.py", line 205, in <lambda> #> return lambda *args, **kwargs: func.call(*args, **kwargs) # pylint: disable=unnecessary-lambda #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/function.py", line 67, in call #> return self.apply(self.nameArgs(args, kwargs)) #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/function.py", line 80, in apply #> result = computedobject.ComputedObject(self, self.promoteArgs(named_args)) #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/function.py", line 107, in promoteArgs #> promoted_args[name] = Function._promoter(args[name], spec['type']) #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/__init__.py", line 242, in _Promote #> return CustomFunction.create(arg, 'Object', ['Object'] * args_count) #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/customfunction.py", line 121, in create #> return CustomFunction(signature, func) #> File "/home/aybarpc01/.virtualenvs/r-reticulate/lib/python3.7/site-packages/ee/customfunction.py", line 47, in __init__ #> self._body = body(*variables) #> File "/home/aybarpc01/R/x86_64-pc-linux-gnu-library/3.6/reticulate/python/rpytools/call.py", line 21, in python_function #> raise RuntimeError(res[kErrorKey])
之前的代码完全有效,但rgee会产生错误。通过添加函数ee_utils_pyfunc应该可以轻松解决这个问题。它将允许在将 R 函数发送到reticulate. 让我们来看看:
library(rgee) ee$Initialize() mylist = ee$List$sequence(0,10) mynewlist = mylist$map( ee_utils_pyfunc( function(x) ee$Number(x)$add(1) ) ) mynewlist$getInfo() #> [1] 1 2 3 4 5 6 7 8 9 10 11
2.不要忘记L
默认情况下,当您在 R 中定义一个数字时,它将产生一个双精度值。这在 Python 中不会发生,因为默认情况下它会创建一个int值。
Python
type(1) #> <class 'int'>
电阻
class(1) #> [1] "numeric"
但这有什么关系呢?让我们用一个例子来解释它:
Python
ee.Initialize() and_bitwise = ee.Number(32).bitwiseAnd(100) and_bitwise.getInfo() #> 32
rgee
and_bitwise = ee$Number(32)$bitwiseAnd(100) #caution: silent error and_bitwise$getInfo() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/aybarpc01/.local/lib/python3.7/site-packages/ee/computedobject.py", line 95, in getInfo return data.computeValue(self) File "/home/aybarpc01/.local/lib/python3.7/site-packages/ee/data.py", line 490, in computeValue return send_('/value', ({'json': obj.serialize(), 'json_format': 'v2'})) File "/home/aybarpc01/.local/lib/python3.7/site-packages/ee/data.py", line 1186, in send_ raise ee_exception.EEException(json_content['error']['message']) ee.ee_exception.EEException: Number.bitwiseAnd: Bitwise operands must be integer only.
用户需要考虑到地球引擎方法的大多数参数都严格限制为仅允许整数值。R 中整数的创建非常简单,您只需要在特定数字的末尾添加字母L或使用函数as.integer。R 中的正确代码是:
and_bitwise = ee$Number(32L)$bitwiseAnd(100L) and_bitwise$getInfo() #> [1] 32
3.ee$Date
由于 R 和 Python 作为编程语言的设计差异,也会出现此问题。目前,R 仅支持 32 位的整数数据类型。这样的整数最多只能计算到大约 20 亿。不幸的是,这个范围对于处理 自UNIX 纪元以来以毫秒为单位保存的Google Earth Engine 时间节点是非常不够的。
Python
my_date = ee.Date('1990-01-01') my_date.getInfo() #> {'type': 'Date', 'value': 631152000000} # greater than 2 billion
电阻
my_date <- ee$Date('1990-01-01') my_date$getInfo() #> $type #> [1] "Date" #> #> $value #> [1] -208192512
问题ee$Date只出现在最后一英里(Python 到 R 或反之亦然reticulate),如果小心处理应该不会有问题。rgee实现两个函数来处理地球引擎日期:eedate_to_rdate和rdate_to_eedate.
# Era5 数据集 era_img <- ee$ImageCollection("ECMWF/ERA5/DAILY")$ filterDate("2019-01-01", "2019-12-31")$ first() # 提取时间信息 ee_date <- era_img$get('system:time_start') ee_date$getInfo() # Silent error #> [1] 112573440 eedate_to_rdate(ee_date = ee_date, timestamp = TRUE) #> [1] 1.546301e+12
