2014-04-01 70 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