使用union
函数可以将两个或多个GeoJSON几何体组合成一个单一的Geometry对象。
feature1
:(GeoJSON Feature) 第一个要组合的要素。feature2
:(GeoJSON Feature) 另一个要组合的要素。返回一个新的GeoJSON Feature对象,其中包含组合了两个或多个输入Geometry的Geometry对象。
var turf = require('@turf/turf');
var feature1 = turf.polygon([[
[-122.801742, 45.48565],
[-122.801742, 45.60491],
[-122.584762, 45.60491],
[-122.584762, 45.48565],
[-122.801742, 45.48565]
]], {
"fill": "#00f"
});
var feature2 = turf.polygon([[
[-122.520217, 45.535693],
[-122.64038, 45.553967],
[-122.720031, 45.526554],
[-122.669906, 45.507309],
[-122.723464, 45.446643],
[-122.532577, 45.408574],
[-122.487258, 45.477466],
[-122.520217, 45.535693]
]], {
"fill": "#f00"
});
var output = turf.union(feature1, feature2);
输出结果为:
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[-122.801742, 45.48565],
[-122.801742, 45.60491],
[-122.584762, 45.60491],
[-122.584762, 45.48565],
[-122.801742, 45.48565]
]
],
[
[
[-122.520217, 45.535693],
[-122.64038, 45.553967],
[-122.720031, 45.526554],
[-122.669906, 45.507309],
[-122.723464, 45.446643],
[-122.532577, 45.408574],
[-122.487258, 45.477466],
[-122.520217, 45.535693]
]
]
]
},
"properties": {
"fill": "#00f"
}
}