天天看點

GEE(google earth engine)通過exports和require實作調用子產品或另一個腳本檔案

版權聲明:轉載請注明作者(獨孤尚良dugushangliang)出處:https://blog.csdn.net/dugushangliang/article/details/105574991

參官方文檔,下面這段代碼是用于被調用的,可以當做是一個子產品,把它儲存為js檔案。exports表示輸出,即表明了這個檔案可以被外部引用的代碼部分。提供給外部調用的變量或函數都依附于exports這個對象。

exports.doc = 'The Foo module is a demonstration of script modules.' +
    '\n It contains a foo function that returns a greeting string. ' +
    '\n It also contains a bar object representing the current date.' +
    '\n' +
    '\n foo(arg):' +
    '\n   @param {ee.String} arg The name to which the greeting should be addressed' +
    '\n   @return {ee.String} The complete greeting.' +
    '\n' +
    '\n bar:' +
    '\n   An ee.Date object containing the time at which the object was created.';

exports.foo = function(arg) {
  return 'Hello, ' + arg + '!  And a good day to you!';
};

exports.bar = ee.Date(Date.now());
           

如下圖所示,檔案儲存在print這個Git庫中,Modules是檔案夾,FooModule.js是檔案名。如果調用這個子產品,使用 require來實作。require表示子產品的引入。這個require的參數需要注意下。這裡有個冒号(‘:’),冒号前面的就是Git庫的名字,冒号後面的就是檔案路徑。右側輸出的時間是格林尼治時間,我們使用的中原標準時間是東八區,是以我們的中原標準時間需要在格林尼治時間基礎上加上8個小時。

GEE(google earth engine)通過exports和require實作調用子產品或另一個腳本檔案

具體調用方法如下所示: 

var Foo = require('users/dugushangliang/print:Modules/FooModule.js');
//如果需要調用的子產品檔案直接在print這個Git庫下,則路徑表示為下面這樣。
//var Foo = require('users/dugushangliang/print:FooModule.js');

print(Foo.doc);

print(Foo.foo('world'));

print('Time now:', Foo.bar);
           

需要注意的是:這個儲存的檔案如果不帶字尾,即不是FooModule.js,而是FooModule,需要使用下面的代碼來調用。

var Foo = require('users/dugushangliang/print:Modules/FooModule');
           

建議:最好帶上這個字尾名,一個是考慮到規範,一個是調用FooModule.js比FooModule好像更快,可能是因為沒有字尾名的還需要判斷這個檔案是什麼類型。

獨孤尚良dugushangliang——著

繼續閱讀