Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Just a short question to lighten up my understanding of some PostGIS-code:

Regarding the creation of user-defined grids I often find the expressions CEILING and FLOOR. Like in this non-representative example:

SELECT floor(st_xmax(the_polygon)/1500) FROM ...

What are they exactly designated for?

Thanks in advance for lightening me up!


share|improve this question
up vote 10 down vote accepted

Floor and ceiling are classical computer and math functions that basically round numbers down or up.

select x, floor(x), ceil(x), trunc(x)
from (select unnest(ARRAY[-4.8, -4.2, 4.2, 4.8]) as x) as f;

  x   | floor | ceil | trunc
 -4.8 |    -5 |   -4 |    -4
 -4.2 |    -5 |   -4 |    -4
  4.2 |     4 |    5 |     4
  4.8 |     4 |    5 |     4
(4 rows)

In the context of building a grid, it can be used to define the extents of the grid to aesthetically-pleasing numbers. For example, if the x- extents of a layer go between Eastings 2754324.254 and 2770756.610, they can be "rounded" to the bounding 1 km grid with:

select floor(xmin/1000) * 1000 as xmin, ceil(xmax/1000) * 1000 as xmax
from (select 2754324.254 as xmin, 2770756.610 as xmax) as f;

  xmin   |  xmax
 2754000 | 2771000
(1 row)
share|improve this answer
Hello Mike, thanks for the extensive answer! It makes sense now in the context, that I’m using it. I guess my main mistake was, to think FLOOR and CEILINING are special PostGIS-functions. That was also the reason, that Google didn't told me smth useful. If I look for it in connection with PostgreSQL the results are 'slightly' different.. %) Nevertheless thanks a lot for explaining it directly to the grid-manner=) – knutella Jan 21 '13 at 14:53

Your Answer


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.