公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
几何图形运算
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Earth Engine 支持对 Geometry
对象执行各种操作。
这些操作包括对各个几何图形执行的操作,例如计算缓冲区、质心、边界框、周长、凸包等。例如:
Code Editor (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');
从前面的示例可以看出,缓冲区距离是以米为单位指定的。
支持的几何图形运算还包括几何图形之间的关系计算,例如交集、并集、差集、距离、包含等。为了测试其中的一些关系,几何图形默认使用“偶-奇”规则。根据奇偶规则,如果从某个点到已知位于多边形外部的某个点的线条穿过奇数个其他边,则该点位于多边形内。多边形的内部是指壳体内部的所有内容,而非孔洞内部。举个简单的例子,圆形多边形内的点必须恰好穿过一条边才能逃离多边形。几何图形可以根据需要选择使用“左侧内侧”规则。假设按给定顺序遍历圆环的点;内部将位于左侧。
为了演示使用“左侧内侧”规则 (evenOdd: false
) 创建的地图几何图形与使用“偶数-奇数”规则创建的地图几何图形之间的区别,以下示例将一个点与两个不同的多边形进行了比较:
Code Editor (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
构造函数提供的坐标顺序如何影响结果。具体而言,该点位于左内多边形的外部,但位于偶数-奇数多边形的内部。
以下示例根据两个多边形之间的关系计算和可视化派生几何图形:
Code Editor (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 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\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."]]