1

I have given a database with coordinates and want to use these coordinates as the center points for hexagon polygons. PostGIS provides this convenient function: ST_Hexagon

The example from the reference works as expected:

SELECT ST_AsText(ST_SetSRID(ST_Hexagon(1.0, 0, 0), 3857));

POLYGON((-1 0,-0.5
     -0.866025403784439,0.5
     -0.866025403784439,1
     0,0.5
     0.866025403784439,-0.5
     0.866025403784439,-1 0)) 

However, when I use my coordinates with ETRS89 / UTM zone 32N (EPSG:25832) and an edge length of 100m:

SELECT ST_AsText(ST_SetSRID(ST_Hexagon(100.0, 5794575, 640800), 25832));

My results are totally wrong:

POLYGON((869186150 110989902.35155405,
         869186200 110989815.74901368,
         869186300 110989815.74901368,
         869186350 110989902.35155405,
         869186300 110989988.95409442,
         869186200 110989988.95409442,
         869186150 110989902.35155405))

The expected results should be around these values:

5794632.380551 640799.725314
5794604.912737 640849.167379
5794546.200284 640849.510727
5794517.359079 640799.381966
5794546.88698 640749.596553
5794602.852651 640748.909858

What do I wrong?

1

The function signature is ST_Hexagon(float8 size, integer cell_i, integer cell_j, geometry origin);

The coordinates are in cell space, not in geographic space. That is, the 1st hexagon has cell coordinates 0;0, its neighbor 0;1 etc, regardless of the hexagon size.

What you can do is to keep the asking for the hexagon 0;0, but to move the grid origin to your location

SELECT ST_AsText(
  ST_SetSRID(
    ST_Hexagon(100.0, 0,0, st_makepoint(5794575, 640800)),
  25832));
                                   
----------
 POLYGON((5794475 640800,
5794525 640713.3974596215,
5794625 640713.3974596215,
5794675 640800,
5794625 640886.6025403785,
5794525 640886.6025403785,
5794475 640800))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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