2

I am fairly new to the GIS and wanted to ask something which can be very preliminary or even worse totally invalid.

How do I create a geom randomly centered around or close to a given point anywhere.

i.e. for example

{"type":"Feature","id":"01","properties":
{"name":"Alabama","density":94.65},"geometry":{"type":"Polygon",
"coordinates":[[[-87.359296,35.00118], [-85.606675,34.984749], 
.....,[-88.098683,34.891641],[-88.202745,34.995703],[-87.359296,35.00118]]]}}, 

so the geometry field value in the above geojson is for Alabama region.

If I were to create random complex polygon geom (with at least 20 different points) on map through code, how would I do it?

Basically I want to generate a random polygon around a point and this polygon can't be simple shape (i.e. quadrilateral or so)

2
  • Do you have any specifications for the program you are using or programming language? If so I would add it to the OP
    – GISHuman
    Apr 28, 2017 at 12:55
  • doesn't matter, but still it can be C#, java, python, nodejs anything Apr 28, 2017 at 13:16

3 Answers 3

2

I use something like:

public static Polygon createRandomPolygon(int n) {
    double latitude = (Math.random() * 180.0) - 90.0;
    double longitude = (Math.random() * 360.0) - 180.0;
    GeometryFactory geometryFactory = new GeometryFactory();
    /* Longitude (= x coord) first ! */
    Polygon poly = null;
    boolean valid = false;
    while (!valid) {
      ArrayList<Coordinate> points = new ArrayList<Coordinate>();
      points.add(new Coordinate(longitude, latitude));
      double lon = longitude;
      double lat = latitude;
      for (int i = 1; i < n; i++) {
        double deltaX = (Math.random() * 10.0) - 5.0;
        double deltaY = (Math.random() * 10.0) - 5.0;
        lon += deltaX;
        lat += deltaY;
        points.add(new Coordinate(lon, lat));
      }
      points.add(new Coordinate(longitude, latitude));
      poly = geometryFactory.createPolygon((Coordinate[]) points.toArray(new Coordinate[] {}));
      valid = poly.isValid();
    }
    return poly;
  }

You could adjust the size of the step to reflect your specific use case.

1
  • I am going to try this and see how it pans out Apr 28, 2017 at 14:16
0

An strategy I would use, is to generate a random set of points using a Gaussian method centered at the point, triangulate it with a Delaunay triangulation, and then randomly subtract triangles in the boundary. The sum of the remaining triangles would be your Polygon.

1
  • wow, okay, let's see how to do all those, thanks for your answer, this definitely helps to my understanding of geom Apr 28, 2017 at 14:18
0

I was able to solve this by generating different co-ordinates based on base co-ordinate, bearing and distance. so I did something like this:

        var loc = factory.map.getCoordinateFromPixel([x, y-95]);
        var origin = new LatLon(loc[0], loc[1]);
        var distance = 500;
        var bearing = 0;
        var data = []
        while (bearing < 360) {
            destination = origin.destinationPoint(distance, bearing);
            data.push([destination['lat'], destination['lon']]);
            distance = getRandomNumber(5000, 8000);
            bearing = bearing + getRandomNumber(10, 20);
        }
        return data;

        //closed 
        function getRandomNumber(min, max) {
            return Math.floor(Math.random() * (max - min)) + min;
        }

basically, I keep calculating points at a variable distance and variable angle, till the angle reaches 360 degrees. I used geodesy lib to save me from manually calculating euclidean distance of sorts on 3D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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