JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
JavaScript
function isRectangle(turfInputPolygon, threshold) {
var threshold = threshold || 2;
var turfPolygon = turfInputPolygon;
if (turf.booleanClockwise(turf.polygonToLine(turfPolygon).features[0])) {
turfPolygon = turf.rewind(turfPolygon);
};
var turfPolygonPts = turf.explode(turfPolygon);
turfPolygonPts.features.push(turfPolygonPts.features[1]);
var rightAngles = 0;
var sumAngles = 0;
for (var i = 1, len = turfPolygonPts.features.length; i < len - 1; i++) {
var b1 = turf.bearing(turfPolygonPts.features[i - 1], turfPolygonPts.features[i]);
var b2 = turf.bearing(turfPolygonPts.features[i], turfPolygonPts.features[i + 1]);
var angle = Math.min((b1 - b2 + 360) % 360, (b2 - b1 + 360) % 360);
sumAngles += angle;
if ((90 - threshold) <= angle && angle <= (90 + threshold)) rightAngles ++;
};
return rightAngles == 4 && ((360 - threshold) <= sumAngles && sumAngles <= (360 + threshold));
};
var geojson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-102.59602204999999,
32.36491866700004
],
[
-102.59952111499996,
32.37500140000003
],
[
-102.60087906199999,
32.378914276000046
],
[
-102.58443896599994,
32.38292495700006
],
[
-102.58184811899997,
32.37500015000006
],
[
-102.57980711899995,
32.36875659300006
],
[
-102.59602204999999,
32.36491866700004
]
]
]
]
}
}
]
};
var arrMultiPolygon = geojson.features[0].geometry.coordinates;
var multiPolygon = turf.multiPolygon(arrMultiPolygon);
alert(isRectangle(multiPolygon, 2));