2
  • 我想计算两个多边形(旋转矩形)的碰撞面积。
  • 我想计算polyA碰撞区域中的区域(%)。
4

1 回答 1

2

turf.js(浏览器和节点的高级地理空间分析)提供了turf-intersectturf-area包。这些可用于计算两个多边形的碰撞和相交面积。

在草皮中,使用特征描述矩形(多边形),例如五边形的描述:

var polyA;

polyA = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.801742, 45.48565],
                [-122.801742, 45.60491],
                [-122.584762, 45.60491],
                [-122.584762, 45.48565],
                [-122.801742, 45.48565]
            ]
        ]
    }
};

计算两个多边形(旋转矩形)的碰撞面积

turf.intersect用于根据特征(多边形)描述交集,例如

var polyA,
    polyB,
    polyAPolyBIntersection;

polyA = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.801742, 45.48565],
                [-122.801742, 45.60491],
                [-122.584762, 45.60491],
                [-122.584762, 45.48565],
                [-122.801742, 45.48565]
            ]
        ]
    }
};

polyB = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-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]
            ]
        ]
    }
};

polyAPolyBIntersection = turf.intersect(polyA, polyB);

console.log('polyAPolyBIntersection', polyAPolyBIntersection);
<script src='//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js'></script>

计算polyA碰撞区域中的面积 (%)。

polyAPolyBIntersection描述 和 的polyA交集polyB。要计算polyA碰撞区域中 的面积(%),我们需要计算 和 的polyA碰撞polyAPolyBIntersection。然后计算所得碰撞的面积和polyA

var polyA,
    polyAArea,
    polyAPolyBIntersection,
    polyAPolyBIntersectionPolyAIntersection,
    polyAPolyBIntersectionPolyAIntersectionArea;

polyA = {
    type: 'Feature',
    properties: {
        fill: '#0f0'
    },
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.801742, 45.48565],
                [-122.801742, 45.60491],
                [-122.584762, 45.60491],
                [-122.584762, 45.48565],
                [-122.801742, 45.48565]
            ]
        ]
    }
};

// Using "intersection" result from the previous example.

polyAPolyBIntersection = {
    type: 'Feature',
    properties: {},
    geometry: {
        type: 'Polygon',
        coordinates: [
            [
                [-122.584762,45.545508794628965],
                [-122.584762,45.48565],
                [-122.68902729894835,45.48565],
                [-122.669906,45.507309],
                [-122.720031,45.526554],
                [-122.64038,45.553967],
                [-122.584762,45.545508794628965]
            ]
        ]
    }
};

// Calculate intersection between polyAPolyBIntersection and polyA.

polyAPolyBIntersectionPolyAIntersection = turf.intersect(polyAPolyBIntersection, polyA);

// Calculate area (in meters) of polyA and polyAPolyBIntersectionPolyAIntersection.
// Note that it does not matter what units we use since we want to calculate the relative intersection size (%).

polyAArea = turf.area(polyA);
polyAPolyBIntersectionPolyAIntersectionArea = turf.area(polyAPolyBIntersectionPolyAIntersection);

// Calculate how much of polyA is covered.

polyACoverage = polyAPolyBIntersectionPolyAIntersectionArea / polyAArea;

console.log('polyACoverage', polyACoverage);
<script src='//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js'></script>

polyACoverage是 0.2533680217675428,这意味着约 25%polyA位于polyAPolyBIntersection.

于 2015-08-19T19:30:07.743 回答