0

I create circle geometries server side on a PostGIS database like this.

UPDATE element 
SET geo = ST_Buffer(
   ST_MakePoint(6.1043443253471, 42.3150676015829), 6, 'quad_segs=8')
WHERE id = 1;

I then fetch the WKT to be sent to openlayers on the client browser.

SELECT ST_AsText(geo) from element where id = 1

The problem is that the circle is displayed as an ellipsis on map like here

enter image description here

It is probably related to projection, but I don't understand how I could create this circle server side so that it appears as a real circle on map.

Can you help me on this? Thanks in advance.

JGH
  • 15,928
  • 4
  • 31
  • 48
Philiz
  • 429
  • 5
  • 25
  • 1
    That is probably because the radius has been treated as degrees and longitudes get closer together away from the equator- If your coordinates are geographic I think you will need to cast the point as a geography https://postgis.net/docs/ST_Buffer.html – Mike Dec 20 '21 at 21:30
  • 2
    I believe this [answer](https://stackoverflow.com/a/49985343/2275388) deals with the same issue. – Jim Jones Dec 20 '21 at 22:19

1 Answers1

0

Yes, it is the projection, as a degree of longitude does not have the same ground length, in meters, as a degree of latitude.

That being said, the picture and the sample code don't not match, as the code instructs to create a 6 degrees wide buffer, which is +- 600km, and the point is in the Mediterranean sea instead of a backyard (maybe X and Y were swapped?).

To overcome this, you can pick a local CRS that preserve distances, such as UTM.

Alternatively, you can create the buffer using geography, which has a unit in meters by default, then cast back to geometry.

select ST_Buffer(
    ST_MakePoint(6.1043443253471, 42.3150676015829)::geography, 
    6, 'quad_segs=8'
  )::geometry  
WHERE id = 1;
JGH
  • 15,928
  • 4
  • 31
  • 48
  • Thanks for the answer. But I need to use WGS84 to display on map like google... And concerning your query, I tried this but it output again an ellipsis geometry: SELECT ST_AsText(ST_Buffer(ST_MakePoint(46.21919859, 7.30204628)::geography, 4, 'quad_segs=8')::geometry) AS geo Am I doing something wrong? – Philiz Dec 20 '21 at 22:48
  • All good, I could make it work thanks – Philiz Dec 21 '21 at 17:25