การดำเนินการเกี่ยวกับเรขาคณิต
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Earth Engine รองรับการดำเนินการที่หลากหลายกับออบเจ็กต์ Geometry
ซึ่งรวมถึงการดำเนินการกับเรขาคณิตแต่ละรายการ เช่น การคำนวณบัฟเฟอร์ ศูนย์กลางมวล กล่องขอบเขต เส้นรอบนอก รูปทรงโค้งมน เป็นต้น ตัวอย่างเช่น
เครื่องมือแก้ไขโค้ด (JavaScript)
// Create a geodesic polygon.
var polygon = ee.Geometry.Polygon([
[[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]
]);
// Compute a buffer of the polygon.
var buffer = polygon.buffer(1000000);
// Compute the centroid of the polygon.
var centroid = polygon.centroid();
Map.addLayer(buffer, {}, 'buffer');
Map.addLayer(centroid, {}, 'centroid');
สังเกตจากตัวอย่างก่อนหน้านี้ว่าระยะบัฟเฟอร์จะระบุเป็นเมตร
การดำเนินการเชิงเรขาคณิตที่รองรับยังรวมถึงการคํานวณเชิงสัมพันธ์ระหว่างเรขาคณิต เช่น การซ้อนทับ การรวม การลบ ระยะทาง ประกอบด้วย ฯลฯ ในการทดสอบความสัมพันธ์บางอย่างเหล่านี้ เรขาคณิตจะใช้กฎ "คู่-คี่" โดยค่าเริ่มต้น ตามกฎคู่-คี่ จุดจะอยู่ภายในรูปหลายเหลี่ยมหากเส้นจากจุดนั้นไปยังจุดที่ทราบว่าอยู่นอกรูปหลายเหลี่ยมตัดกับขอบอื่นๆ เป็นจํานวนคี่ ส่วนด้านในของรูปหลายเหลี่ยมคือทุกอย่างที่อยู่ภายในเปลือก ไม่ใช่ภายในรู ตัวอย่างง่ายๆ คือ จุดภายในรูปหลายเหลี่ยมแบบวงกลมต้องตัดผ่านขอบเพียง 1 เส้นเพื่อออกจากรูปหลายเหลี่ยม เรขาคณิตอาจใช้กฎ "ด้านซ้ายภายใน" ก็ได้ หากจําเป็น ลองจินตนาการว่าคุณเดินไปตามจุดของวงแหวนตามลำดับที่ระบุไว้ โดยด้านในจะอยู่ทางด้านซ้าย
ตัวอย่างต่อไปนี้เปรียบเทียบจุดกับรูปหลายเหลี่ยม 2 รูปที่แตกต่างกันเพื่อแสดงให้เห็นความแตกต่างระหว่างเรขาคณิตที่สร้างด้วยกฎ "ด้านซ้ายด้านใน" (evenOdd: false
) กับกฎ "คู่-คี่"
เครื่องมือแก้ไขโค้ด (JavaScript)
// Create a left-inside polygon.
var holePoly = ee.Geometry.Polygon({
coords: [
[[-35, -10], [-35, 10], [35, 10], [35, -10], [-35, -10]]
],
evenOdd: false
});
// Create an even-odd version of the polygon.
var evenOddPoly = ee.Geometry({
geoJson: holePoly,
evenOdd: true
});
// Create a point to test the insideness of the polygon.
var pt = ee.Geometry.Point([1.5, 1.5]);
// Check insideness with a contains operator.
print(holePoly.contains(pt)); // false
print(evenOddPoly.contains(pt)); // true
ตัวอย่างก่อนหน้านี้แสดงให้เห็นว่าลําดับของพิกัดที่ระบุให้กับตัวสร้าง Polygon
ส่งผลต่อผลลัพธ์อย่างไรเมื่อสร้างรูปหลายเหลี่ยมด้านซ้ายด้านใน กล่าวโดยละเอียดคือ จุดอยู่นอกรูปหลายเหลี่ยมด้านในซ้าย แต่อยู่ภายในรูปหลายเหลี่ยมแบบคู่ไม่คู่
ตัวอย่างต่อไปนี้จะคํานวณและแสดงภาพเรขาคณิตที่ดึงข้อมูลตามความสัมพันธ์ระหว่างรูปหลายเหลี่ยม 2 รูป
เครื่องมือแก้ไขโค้ด (JavaScript)
// Create two circular geometries.
var poly1 = ee.Geometry.Point([-50, 30]).buffer(1e6);
var poly2 = ee.Geometry.Point([-40, 30]).buffer(1e6);
// Display polygon 1 in red and polygon 2 in blue.
Map.setCenter(-45, 30);
Map.addLayer(poly1, {color: 'FF0000'}, 'poly1');
Map.addLayer(poly2, {color: '0000FF'}, 'poly2');
// Compute the intersection, display it in green.
var intersection = poly1.intersection(poly2, ee.ErrorMargin(1));
Map.addLayer(intersection, {color: '00FF00'}, 'intersection');
// Compute the union, display it in magenta.
var union = poly1.union(poly2, ee.ErrorMargin(1));
Map.addLayer(union, {color: 'FF00FF'}, 'union');
// Compute the difference, display in yellow.
var diff1 = poly1.difference(poly2, ee.ErrorMargin(1));
Map.addLayer(diff1, {color: 'FFFF00'}, 'diff1');
// Compute symmetric difference, display in black.
var symDiff = poly1.symmetricDifference(poly2, ee.ErrorMargin(1));
Map.addLayer(symDiff, {color: '000000'}, 'symmetric difference');
ในตัวอย่างเหล่านี้ โปรดทราบว่ามีการตั้งค่าพารามิเตอร์ maxError
เป็น 1 เมตรสําหรับการดำเนินการเชิงเรขาคณิต maxError
คือข้อผิดพลาดสูงสุดที่อนุญาตเป็นเมตรจากการเปลี่ยนรูปแบบ (เช่น การฉายภาพหรือการฉายภาพใหม่) ที่อาจเปลี่ยนแปลงเรขาคณิต หากเรขาคณิตรายการใดรายการหนึ่งอยู่ในการฉายที่แตกต่างจากรายการอื่น Earth Engine จะทำการประมวลผลในระบบพิกัดทรงกลม โดยมีค่าความแม่นยำของการฉายที่ระบุโดย maxError
นอกจากนี้ คุณยังระบุการฉายภาพที่ต้องการใช้คํานวณได้ด้วย หากจําเป็น
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[[["\u003cp\u003eEarth Engine provides numerous geometric operations, including buffer, centroid, bounding box calculations, and more, all measured in meters.\u003c/p\u003e\n"],["\u003cp\u003eGeometries utilize the "even-odd" rule for relational computations like intersection and union, but this can be changed to the "left-inside" rule if needed.\u003c/p\u003e\n"],["\u003cp\u003eThe order of coordinates influences results for left-inside polygons, affecting point containment compared to even-odd polygons.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003emaxError\u003c/code\u003e is used in geometric operations to account for potential errors from projections, ensuring accurate calculations.\u003c/p\u003e\n"]]],["Earth Engine performs various operations on `Geometry` objects, including computing a buffer, centroid, or bounding box. Geometries can use \"even-odd\" or \"left-inside\" rules to define whether a point is within. The `contains` operator checks for point inclusion. The code demonstrates the intersection, union, difference, and symmetric difference operations between two polygons. The `maxError` parameter sets the maximum allowable error in meters for geometry transformations and allows to perform operations between different projections.\n"],null,["# Geometric Operations\n\nEarth Engine supports a wide variety of operations on `Geometry` objects.\nThese include operations on individual geometries such as computing a buffer,\ncentroid, bounding box, perimeter, convex hull, etc. For example:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Create a geodesic polygon.\nvar polygon = ee.Geometry.Polygon([\n [[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]]\n]);\n\n// Compute a buffer of the polygon.\nvar buffer = polygon.buffer(1000000);\n\n// Compute the centroid of the polygon.\nvar centroid = polygon.centroid();\nMap.addLayer(buffer, {}, 'buffer');\nMap.addLayer(centroid, {}, 'centroid');\n```\n\nObserve from the previous example that the buffer distance is specified in meters.\n\nSupported geometric operations also include relational computations between geometries\nsuch as intersection, union, difference, distance, contains, etc. To test some of these\nrelations, geometries use the \"even-odd\" rule by default. By the even-odd rule, a point\nis inside the polygon if a line from that point to some point known to be outside the\npolygon crosses an odd number of other edges. The inside of a polygon is everything\ninside the shell and not inside a hole. As a simple example, a point within a circular\npolygon must cross exactly one edge to escape the polygon. Geometries can optionally use\nthe \"left-inside\" rule, if necessary. Imagine walking the points of a ring in the order\ngiven; the inside will be on the left.\n\nTo demonstrate the difference between geometries created with the \"left-inside\" rule\n(`evenOdd: `**false**) and those created with the \"even-odd\" rule,\nthe following example compares a point to two different polygons:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Create a left-inside polygon.\nvar holePoly = ee.Geometry.Polygon({\n coords: [\n [[-35, -10], [-35, 10], [35, 10], [35, -10], [-35, -10]]\n ],\n evenOdd: false\n});\n\n// Create an even-odd version of the polygon.\nvar evenOddPoly = ee.Geometry({\n geoJson: holePoly,\n evenOdd: true\n});\n\n// Create a point to test the insideness of the polygon.\nvar pt = ee.Geometry.Point([1.5, 1.5]);\n\n// Check insideness with a contains operator.\nprint(holePoly.contains(pt)); // false\nprint(evenOddPoly.contains(pt)); // true\n```\n\nThe previous example demonstrates how the order of coordinates provided to the\n`Polygon` constructor affects the result when a left-inside polygon is\nconstructed. Specifically, the point is outside the left-inside polygon but inside the\neven-odd polygon.\n\nThe following example computes and visualizes derived geometries based on the relationship\nbetween two polygons:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Create two circular geometries.\nvar poly1 = ee.Geometry.Point([-50, 30]).buffer(1e6);\nvar poly2 = ee.Geometry.Point([-40, 30]).buffer(1e6);\n\n// Display polygon 1 in red and polygon 2 in blue.\nMap.setCenter(-45, 30);\nMap.addLayer(poly1, {color: 'FF0000'}, 'poly1');\nMap.addLayer(poly2, {color: '0000FF'}, 'poly2');\n\n// Compute the intersection, display it in green.\nvar intersection = poly1.intersection(poly2, ee.ErrorMargin(1));\nMap.addLayer(intersection, {color: '00FF00'}, 'intersection');\n\n// Compute the union, display it in magenta.\nvar union = poly1.union(poly2, ee.ErrorMargin(1));\nMap.addLayer(union, {color: 'FF00FF'}, 'union');\n\n// Compute the difference, display in yellow.\nvar diff1 = poly1.difference(poly2, ee.ErrorMargin(1));\nMap.addLayer(diff1, {color: 'FFFF00'}, 'diff1');\n\n// Compute symmetric difference, display in black.\nvar symDiff = poly1.symmetricDifference(poly2, ee.ErrorMargin(1));\nMap.addLayer(symDiff, {color: '000000'}, 'symmetric difference');\n```\n\nIn these examples, note that that `maxError` parameter is set to one meter for\nthe geometry operations. The `maxError` is the maximum allowable error, in\nmeters, from transformations (such as projection or reprojection) that may alter the\ngeometry. If one of the geometries is in a different projection from the other, Earth\nEngine will do the computation in a spherical coordinate system, with a projection\nprecision given by `maxError`. You can also specify a specific projection in\nwhich to do the computation, if necessary."]]