-1

How to insert/create any random points within certain area or square km (say for mumbai)

Considering area is mumbai and I want to insert all possible random points that lies within this area

My idea was to write custom function in Postgres like stored procedure

Specific : I need to have a fixed area that is mumbai .. and within this area i want to generate some small polygons , my concern is that how can i get the cordinates of mumbai , or how i can insert using boundary cordinates of mumbai?

sshet
  • 1,152
  • 1
  • 6
  • 15
  • 1
    *insert **all** possible random points* - are you sure? There are infinite number of (random) points in an area. – pozs Jan 08 '15 at 09:39
  • not sure about wanted to test it for load balance .. suppose given a polygon , how many small polygons lies inside that polygon within that whole area( for ex mumbai ) – sshet Jan 08 '15 at 09:51
  • if you want to break down a polygon into smaller chunks, that's a different question; please update this one or post a new. – pozs Jan 08 '15 at 09:55
  • Your question is somewhat unclear. Random points will not create a useful, valid polygon. What are you actually trying to do? – John Powell Jan 08 '15 at 20:42

1 Answers1

1

Create a table named 'Location' with two decimal columns, x and y.

Insert records using random() to generate co-ordinates, with your specified max for x and y.

You can do this in a function, but why not straight in a SQL statement - you can execute it from the command line or pgAdmin.

Stackoverflow answer on how to use random: Generate a random number in the range 1 - 10

EDIT - Additional information for polygon requirement after original question edit

Add an extra column to 'Location' of type Integer, name it shape_id

For every shape_id there will be three or more rows, for the three+ points of a polygon.

When inserting locations into the table you will need to check the previous entries for that shape_id, and be sure it satisfies the rules of a polygon.

Community
  • 1
  • 1
Rots
  • 5,506
  • 3
  • 43
  • 51
  • If you use random() to create a polygon you will end up with spaghetti. There are more sophisticated algorithms to create non self-intersecting polygons, but the OP question is somewhat vague about what is required, it has to be said. – John Powell Jan 08 '15 at 20:41
  • @JohnBarça the question was modified to add the polygon requirement, that was not there previously. I could modify the answer or delete it, as you're right it would generate 'spaghetti'. – Rots Jan 08 '15 at 20:44