แนวคิดการเขียนโปรแกรมเชิงฟังก์ชัน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ข้อมูลเบื้องต้นเกี่ยวกับการเขียนโปรแกรมเชิงฟังก์ชัน
Earth Engine ใช้ระบบการประมวลผลแบบขนานเพื่อทำการคำนวณในเครื่องจำนวนมาก
Earth Engine ใช้เทคนิคมาตรฐานที่ภาษาฟังก์ชันใช้กันโดยทั่วไป เช่น ความโปร่งใสในการอ้างอิงและการประเมินแบบเลื่อนเวลา เพื่อให้การเพิ่มประสิทธิภาพและประสิทธิภาพเพิ่มขึ้นอย่างมาก
แนวคิดหลักที่ทำให้การเขียนโปรแกรมเชิงฟังก์ชันแตกต่างจากการเขียนโปรแกรมเชิงกระบวนการคือการไม่มีผลข้างเคียง ซึ่งหมายความว่าฟังก์ชันที่คุณเขียนจะไม่ขึ้นอยู่กับหรือ
อัปเดตข้อมูลที่อยู่นอกฟังก์ชัน ดังที่คุณจะเห็นในตัวอย่างด้านล่าง คุณสามารถ
จัดโครงสร้างปัญหาใหม่เพื่อให้แก้ปัญหาได้โดยใช้ฟังก์ชันที่ไม่มีผลข้างเคียง ซึ่ง
เหมาะกับการดำเนินการแบบขนานมากกว่า
สำหรับ Loops
ไม่แนะนำให้ใช้ลูป for ใน Earth Engine คุณสามารถได้ผลลัพธ์เดียวกันโดยใช้map()
การดำเนินการที่คุณระบุฟังก์ชันที่ใช้กับแต่ละองค์ประกอบได้อย่างอิสระ
ซึ่งจะช่วยให้ระบบกระจายการประมวลผลไปยัง
เครื่องต่างๆ ได้
ตัวอย่างด้านล่างแสดงวิธีใช้รายการตัวเลขและสร้างรายการอื่นที่มี
กำลังสองของแต่ละหมายเลขโดยใช้ map()
โปรแกรมแก้ไขโค้ด (JavaScript)
// This generates a list of numbers from 1 to 10.
var myList = ee.List.sequence(1, 10);
// The map() operation takes a function that works on each element independently
// and returns a value. You define a function that can be applied to the input.
var computeSquares = function(number) {
// We define the operation using the EE API.
return ee.Number(number).pow(2);
};
// Apply your function to each item in the list by using the map() function.
var squares = myList.map(computeSquares);
print(squares); // [1, 4, 9, 16, 25, 36, 49, 64, 81]
เงื่อนไข If/Else
ปัญหาที่พบบ่อยอีกอย่างสำหรับผู้ใช้ใหม่ที่คุ้นเคยกับกระบวนการ
รูปแบบการเขียนโปรแกรมคือการใช้โอเปอเรเตอร์แบบมีเงื่อนไข if/else อย่างเหมาะสมใน Earth Engine แม้ว่า API
จะมีee.Algorithms.If()
อัลกอริทึม แต่เราไม่แนะนำให้ใช้
และสนับสนุนให้ใช้วิธีการที่ใช้งานได้จริงมากกว่าโดยใช้ map()
และตัวกรอง
Earth Engine ใช้
การดำเนินการที่เลื่อนออกไป ซึ่งหมายความว่าการประเมินนิพจน์จะล่าช้าจนกว่าจะมีการ
ต้องใช้ค่าจริง ในบางกรณี รูปแบบการดำเนินการประเภทนี้จะประเมิน
ทั้งทางเลือกที่เป็นจริงและเท็จของคำสั่ง ee.Algorithms.If()
ซึ่งอาจ
ส่งผลให้มีการคำนวณและการใช้หน่วยความจำเพิ่มเติม ทั้งนี้ขึ้นอยู่กับนิพจน์และทรัพยากร
ที่จำเป็นต่อการดำเนินการ
สมมติว่าคุณต้องการแก้โจทย์ที่คล้ายกับตัวอย่างข้างต้น โดยงานคือการคำนวณกำลังสองของ
เฉพาะจำนวนคี่ วิธีการแก้ปัญหานี้โดยใช้ฟังก์ชันโดยไม่มีเงื่อนไข if/else แสดงไว้
ด้านล่าง
โปรแกรมแก้ไขโค้ด (JavaScript)
// The following function determines if a number is even or odd. The mod(2)
// function returns 0 if the number is even and 1 if it is odd (the remainder
// after dividing by 2). The input is multiplied by this remainder so even
// numbers get set to 0 and odd numbers are left unchanged.
var getOddNumbers = function(number) {
number = ee.Number(number); // Cast the input to a Number so we can use mod.
var remainder = number.mod(2);
return number.multiply(remainder);
};
var newList = myList.map(getOddNumbers);
// Remove the 0 values.
var oddNumbers = newList.removeAll([0]);
var squares = oddNumbers.map(computeSquares);
print(squares); // [1, 9, 25, 49, 81]
โดยเฉพาะอย่างยิ่งเมื่อทำงานกับคอลเล็กชัน หากต้องการใช้
อัลกอริทึมอื่นกับคอลเล็กชันตามเงื่อนไขบางอย่าง วิธีที่แนะนำคือกรอง
คอลเล็กชันตามเงื่อนไขก่อน แล้วจึงใช้map()
ฟังก์ชันอื่นกับ
ชุดย่อยแต่ละชุด ซึ่งจะช่วยให้ระบบดำเนินการแบบขนานได้ ตัวอย่าง
โปรแกรมแก้ไขโค้ด (JavaScript)
// Import Landsat 8 TOA collection and filter to 2018 images.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
.filterDate('2018-01-01', '2019-01-01');
// Divide the collection into 2 subsets and apply a different algorithm on them.
var subset1 = collection.filter(ee.Filter.lt('SUN_ELEVATION', 40));
var subset2 = collection.filter(ee.Filter.gte('SUN_ELEVATION', 40));
// Multiply all images in subset1 collection by 2;
// do nothing to subset2 collection.
var processed1 = subset1.map(function(image) {
return image.multiply(2);
});
var processed2 = subset2;
// Merge the collections to get a single collection.
var final = processed1.merge(processed2);
print('Original collection size', collection.size());
print('Processed collection size', final.size());
การทำซ้ำแบบสะสม
คุณอาจต้องดำเนินการตามลำดับ โดยการวนซ้ำครั้งถัดไปจะใช้ผลลัพธ์ของ
การวนซ้ำแต่ละครั้ง Earth Engine มีเมธอด iterate()
สำหรับงานดังกล่าว โปรดทราบว่า iterate()
จะดำเนินการตามลำดับและ
จึงจะช้าสำหรับการดำเนินการขนาดใหญ่ ใช้เฉพาะในกรณีที่คุณใช้ map()
และตัวกรองเพื่อให้ได้เอาต์พุตที่ต้องการไม่ได้
ตัวอย่างที่ดีของ iterate()
คือการสร้างลำดับตัวเลขฟีโบนักชี ในที่นี้ ตัวเลขแต่ละตัวในลำดับคือผลรวมของตัวเลข 2 ตัวก่อนหน้า ฟังก์ชัน iterate()
มีอาร์กิวเมนต์ 2 รายการ
คือฟังก์ชัน (อัลกอริทึม) และค่าเริ่มต้น ฟังก์ชันจะส่งค่า 2 ค่า
ค่าปัจจุบันในการวนซ้ำ และผลลัพธ์ของการวนซ้ำก่อนหน้า ตัวอย่างต่อไปนี้
แสดงวิธีใช้ลำดับฟีโบนัชชีใน Earth Engine
โปรแกรมแก้ไขโค้ด (JavaScript)
var algorithm = function(current, previous) {
previous = ee.List(previous);
var n1 = ee.Number(previous.get(-1));
var n2 = ee.Number(previous.get(-2));
return previous.add(n1.add(n2));
};
// Compute 10 iterations.
var numIteration = ee.List.repeat(1, 10);
var start = [0, 1];
var sequence = numIteration.iterate(algorithm, start);
print(sequence); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
ตอนนี้คุณมีความเข้าใจแนวคิดของ JavaScript เป็นอย่างดีแล้ว คุณสามารถดูบทแนะนำเกี่ยวกับ API เพื่อดูข้อมูลเบื้องต้นเกี่ยวกับฟังก์ชันการทำงานด้านภูมิสารสนเทศของ
Earth Engine API
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eEarth Engine leverages functional programming principles, like referential transparency and lazy evaluation, for parallel processing and optimization.\u003c/p\u003e\n"],["\u003cp\u003eAvoid for-loops and if/else statements; utilize \u003ccode\u003emap()\u003c/code\u003e for parallel processing and filters for conditional operations on collections.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003eiterate()\u003c/code\u003e for cumulative, sequential operations where each step depends on the previous, but note its potential performance limitations.\u003c/p\u003e\n"],["\u003cp\u003eFunctional programming in Earth Engine prioritizes side-effect-free functions for efficient distributed computation across its infrastructure.\u003c/p\u003e\n"],["\u003cp\u003eEarth Engine's deferred execution model impacts how \u003ccode\u003eee.Algorithms.If()\u003c/code\u003e statements are evaluated, potentially leading to unnecessary computations.\u003c/p\u003e\n"]]],[],null,["# Functional Programming Concepts\n\nIntroduction to functional programming\n--------------------------------------\n\nEarth Engine uses a parallel processing system to carry out computation across a large number of\nmachines. To enable such processing, Earth Engine takes advantage of standard techniques commonly\nused by functional languages, such as referential transparency and lazy evaluation, for significant\noptimization and efficiency gains.\n\nThe main concept that sets functional programming apart from procedural programming is *the\nabsence of side effects*. What it means is that the functions that you write doesn't rely on or\nupdate data that is outside of the function. As you will see in the examples below, it is possible\nto re-structure your problem so that it can be solved using functions without side-effects - which\nare much better suited to be executed in parallel.\n\n### For Loops\n\nThe use of for-loops is discouraged in Earth Engine. The same results can be achieved using a\n`map()` operation where you specify a function that\ncan be independently applied to each element. This allows the system to distribute the processing to\ndifferent machines.\n\nThe example below illustrates how you would take a list of numbers and create another list with\nthe squares of each number using `map()`:\n\n\n### Code Editor (JavaScript)\n\n```javascript\n// This generates a list of numbers from 1 to 10.\nvar myList = ee.List.sequence(1, 10);\n\n// The map() operation takes a function that works on each element independently\n// and returns a value. You define a function that can be applied to the input.\nvar computeSquares = function(number) {\n // We define the operation using the EE API.\n return ee.Number(number).pow(2);\n};\n\n// Apply your function to each item in the list by using the map() function.\nvar squares = myList.map(computeSquares);\nprint(squares); // [1, 4, 9, 16, 25, 36, 49, 64, 81]\n```\n\n### If/Else Conditions\n\nAnother common problem faced by new users who are used to procedural\nprogramming paradigm is the proper use of if/else conditional operators in Earth Engine. While, the API\ndoes provide a `ee.Algorithms.If()` algorithm, the use of it is strongly discouraged\nin favor of a more functional approach using `map()` and filters.\nEarth Engine uses [deferred execution](/earth-engine/guides/deferred_execution), which means that the evaluation of an expression is delayed until its\nrealized value is actually required. In some cases, this type of execution model will evaluate\nboth the true and false alternatives of an `ee.Algorithms.If()` statement. This can\nlead to extra computation and memory usage, depending on the expressions and the resources\nrequired to execute them.\n\nSay you want to solve a variant of the above example, where the task is to compute squares of\nonly odd numbers. A functional approach to solving this without if/else conditions, is demonstrated\nbelow:\n\n### Code Editor (JavaScript)\n\n```javascript\n// The following function determines if a number is even or odd. The mod(2)\n// function returns 0 if the number is even and 1 if it is odd (the remainder\n// after dividing by 2). The input is multiplied by this remainder so even\n// numbers get set to 0 and odd numbers are left unchanged.\nvar getOddNumbers = function(number) {\n number = ee.Number(number); // Cast the input to a Number so we can use mod.\n var remainder = number.mod(2);\n return number.multiply(remainder);\n};\n\nvar newList = myList.map(getOddNumbers);\n\n// Remove the 0 values.\nvar oddNumbers = newList.removeAll([0]);\n\nvar squares = oddNumbers.map(computeSquares);\nprint(squares); // [1, 9, 25, 49, 81]\n```\n\nThis paradigm is especially applicable when working with collections. If you wanted to apply\na different algorithm to the collection based on some conditions, the preferred way is to first\nfilter the collection based on the condition, and then `map()` a different function to\neach of the subsets. This allows the system to parallelize the operation. For example:\n\n\n### Code Editor (JavaScript)\n\n```javascript\n// Import Landsat 8 TOA collection and filter to 2018 images.\nvar collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')\n .filterDate('2018-01-01', '2019-01-01');\n\n// Divide the collection into 2 subsets and apply a different algorithm on them.\nvar subset1 = collection.filter(ee.Filter.lt('SUN_ELEVATION', 40));\nvar subset2 = collection.filter(ee.Filter.gte('SUN_ELEVATION', 40));\n\n// Multiply all images in subset1 collection by 2;\n// do nothing to subset2 collection.\nvar processed1 = subset1.map(function(image) {\n return image.multiply(2);\n});\nvar processed2 = subset2;\n\n// Merge the collections to get a single collection.\nvar final = processed1.merge(processed2);\nprint('Original collection size', collection.size());\nprint('Processed collection size', final.size());\n```\n\n### Cumulative Iteration\n\nYou may need to do sequential operation, where the result of\neach iteration is used by the subsequent iteration. Earth Engine provides a `iterate()`\nmethod for such tasks. Remember that `iterate()` is executed in a sequential manner and\nhence will be slow for large operations. Use it only when you are not able to use `map()`\nand filters to achieve the desired output.\n\nA good demonstration of `iterate()` is for creation of [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number) sequence. Here, each\nnumber in the series is the sum of previous 2 numbers. The `iterate()` function takes 2\narguments, a function (algorithm) and a starting value. The function itself gets passed on 2 values,\nthe current value in the iteration, and the result of the previous iteration. The following example\ndemonstrates how to implement a fibonacci sequence in Earth Engine.\n\n### Code Editor (JavaScript)\n\n```javascript\nvar algorithm = function(current, previous) {\n previous = ee.List(previous);\n var n1 = ee.Number(previous.get(-1));\n var n2 = ee.Number(previous.get(-2));\n return previous.add(n1.add(n2));\n};\n\n// Compute 10 iterations.\nvar numIteration = ee.List.repeat(1, 10);\nvar start = [0, 1];\nvar sequence = numIteration.iterate(algorithm, start);\nprint(sequence); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n```\n\nNow that you have a good understanding of javascript concepts, you can see the [API Tutorial](/earth-engine/tutorials/tutorial_api_01) for an introduction to the geospatial functionality of the\nEarth Engine API."]]