是否有方法或工具可以使用边界框生成特定大小的随机GeoJSON多边形?具体来说,我想用很多随机多边形填充mongodb并测试特定的功能。
答案 0 :(得分:1)
您可以使用边界框坐标以编程方式为矩形生成随机边界框坐标。
例如,如果您的边界框是[[100,100],[200,200]],您可以执行以下操作:
// generate a random width and height
// (e.g. with random numbers between 1 and 50)
var width = Math.floor(Math.random() * 50) + 1;
var height = Math.floor(Math.random() * 50) + 1;
// generate a random position that allows the rectangle to fit within the bounding box walls
// 100 is used in the calculation as 100 is the width and height of the example bounding box
var upperX = Math.floor(Math.random() * (100-width)) + 1;
var upperY = Math.floor(Math.random() * (100-height)) + 1;
var lowerX = upperX + width;
var lowerY = upperY + height;
var bounds = [[upperX, upperY], [lowerX, lowerY]];
// create rectangle
L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(map);
// loop through above code some chosen number of times