使用d3plus.largestRect获取多边形内最大矩形返回平行四边形问题
问题:d3plus.largestRect返回平行四边形而非直角矩形
我正在使用d3plus.largestRect计算多边形或GeoJSON内的最大非轴对齐矩形,该方法应执行近似算法,虽然计算的面积大致正确,但它并未返回带90度角的矩形,而是返回了平行四边形。我查阅了大量Stack Overflow帖子,未发现有用户遇到相同问题,若有需要,我也可以更换其他库或代码。
相关代码
// A file with a function that will return the maximum rectangle of a given geojson polygon // using d3plus, Author: Daniel Smilkov import "https://cdn.jsdelivr.net/npm/d3plus@2"; // const largestRect = require("d3plus").largestRect; export function maxRect(polygon) { // polygon is a geojson polygon and aspectRatio is a number that is the ratio of width to height // we want to return a geojson polygon that is the maximum rectangle that fits inside the polygon // and has the given aspect ratio console.log(d3plus) // we need to convert the geojson polygon to an array of points var points = polygon.coordinates[0].map(function (coord) { return [coord[0], coord[1]]; }); // we use d3plus to calculate the rectangle var rect = d3plus.largestRect(points); console.log(rect) // we need to convert the rectangle back to a geojson polygon. rect.points will be an array of 5 points // the first and last point are the same var rectPolygon = { "type": "Polygon", "coordinates": [ [ [rect.points[0][0], rect.points[0][1]], [rect.points[1][0], rect.points[1][1]], [rect.points[2][0], rect.points[2][1]], [rect.points[3][0], rect.points[3][1]], [rect.points[0][0], rect.points[0][1]] ] ] }; return rectPolygon; } // run the function var polygon ={ "type": "Polygon", "coordinates": [ [ [ -110.06669998168945, 27.583459194048213 ], [ -110.06601333618164, 27.581709456471202 ], [ -110.05614280700684, 27.58490460838308 ], [ -110.05056381225586, 27.573416842281983 ], [ -110.06249427795409, 27.570449607879656 ], [ -110.06155014038086, 27.56748229324079 ], [ -110.02970695495605, 27.575014550647136 ], [ -110.0387191772461, 27.593348490311683 ], [ -110.06635665893555, 27.583383119081834 ], [ -110.06669998168945, 27.583459194048213 ] ] ] } var newPolygon = maxRect(polygon); console.log(JSON.stringify(newPolygon));
第一个示例效果

第二个示例GeoJSON
{ "type": "Polygon", "coordinates": [ [ [ -121.498361304, 38.622343198 ], [ -121.498593986, 38.622343198 ], [ -121.498593986, 38.622508322 ], [ -121.498565407, 38.622508355 ], [ -121.498565823, 38.622465787 ], [ -121.498410255, 38.622463692 ], [ -121.498409129, 38.622508534 ], [ -121.498361664, 38.622508588 ], [ -121.498361304, 38.622343198 ] ] ] }
第二个示例效果

内容的提问来源于stack exchange,提问作者tejas bhakta
相关产品推荐
相关产品推荐

