2014-04-01 109 views
2

有沒有一種方法或工具用邊界框生成特定大小的隨機GeoJSON多邊形?具體而言,我想用大量的隨機多邊形填充mongodb並測試特定的功能。生成隨機的GeoJSON多邊形

回答

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