问题
- I want to calculate collision area of two polygons (rotated rectangles).
- I want to calculate what area of the
polyA
is in the collision area (%).
回答1:
turf.js (advanced geospatial analysis for browsers and node) provides turf-intersect and turf-area packages. These can be used to calculate collision and area of intersection of two polygons.
In turf, rectangles (polygons) are described using features, e.g. a description of a pentagon:
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]
]
]
}
};
To calculate collision area of two polygons (rotated rectangles)
turf.intersect
is used to describe intersection in terms of a feature (polygon), e.g.
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>
To calculate what area of the polyA
is in the collision area (%).
polyAPolyBIntersection
describes the intersection of polyA
and polyB
. To calculate what area of the polyA
is in the collision area (%), we need to calculate the collision of polyA
and polyAPolyBIntersection
. Then calculate area of the resulting collision and 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
is 0.2533680217675428, which means that ~25% of polyA
is in the polyAPolyBIntersection
.
来源:https://stackoverflow.com/questions/32104083/calculate-collision-and-area-of-intersection-of-two-rotated-rectangles-polygons