Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a world map. I need randomly to generate polygons and render them on map. I have no problem with rendering, but can anybody suggest how to generate polygons with random geometry? (I need n polygons, n - random number). The main problem is how to generate geometry to them.

share|improve this question
2  
There are infinitely many kinds of randomness, distinguished by the relative frequencies with which objects are generated. What specifically do you want to be "random" and how? For example, should all locations have equal chances of being covered with part of a polygon? Should the polygons all have the same numbers of vertices? Will you allow them to be concave? What should their sizes be? Etc., etc. As an example, a set of random polygons is illustrated in this answer. –  whuber Apr 23 '13 at 19:30

1 Answer 1

Someting like this should do what you want, but please note that the random points here are all integers

(function () {

        function getRandomArbitary (min, max) {
            return Math.random() * (max - min) + min;
        }

        function featureIntersects (features, feature) {
            for (var i = 0; i<features.length; i++) {
                if (feature.geometry.intersects(features[i].geometry)) {
                    return true;
                }
            }
            return false;
        }

        function lineIntersects (points, point) {
            var line = new OpenLayers.Geometry.LineString([points[points.length-1], point]);
            var other = new OpenLayers.Geometry.LineString(points.slice(0, points.length-2));
            return line.intersects(other);
        }

        function randomPoly (bounds) {
            var numPoints = getRandomArbitary(2, 5);

            var boundsArray = bounds.toArray();
            var minLon = boundsArray[0];
            var minLat = boundsArray[1];
            var maxLon = boundsArray[2];
            var maxLat = boundsArray[3];
            var points = [];
            for (var i=0; i<numPoints; i++) {
                var point = new OpenLayers.Geometry.Point(getRandomArbitary(minLon, maxLon), getRandomArbitary(minLat, maxLat));
                if (i > 2 && lineIntersects(points, point)) {
                    numPoints += 1;
                } else {
                    points.push(point);
                }
            }
            points.push(points[0].clone());

            return new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon(new OpenLayers.Geometry.LinearRing(points)));
        } 

        var map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.OSM("Simple OSM Map");
        map.addLayer(layer);
        var vector = new OpenLayers.Layer.Vector("vector");
        map.addLayer(vector);
        map.zoomToMaxExtent();

        var numFeatures = getRandomArbitary(1, 10);
        var features = [];
        for (var i=0; i<numFeatures; i++) {
            var poly = randomPoly(map.getExtent());
            if (featureIntersects(features, poly)) {
                numFeatures +=1;
            }else {
                features.push(poly);
            }
        }
        vector.addFeatures(features);
}());
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.