1

I need to create a grid within a polygon like it is done in

How to create regular point grid inside a polygon in Postgis?

There a function is created to draw the grid points. But necessary to know is how huge the cells are or how huge is the distance between the point. In my case I don't know anything about that. I just know how much grid cells should be created. How can I solve that problem? How is it possible to create a grid without knowing the cell size but the number of cells that should be created? Does anybody can help me?

Bine
  • 185
  • 7

1 Answers1

1

You can adapt the function in the linked question. Divide the problem into two parts:

  1. Estimate the point spacing
  2. Create the points

For step 1, divide ST_Area(geom) by the required number of points to get the area per point (I'm assuming n is large), then take the square root to get the separation. So, assuming you've chosen one implementation of makegrid(geometry, real) from that question,

makegrid(geom, sqrt(ST_Area(geom) / n)

If you need to return exactly n points, adapt this inside an iterative search for the best separation.

N.B. I've assumed real-valued separation because I don't know the units of your grid.

Toby Speight
  • 336
  • 3
  • 12