简体   繁体   中英

Find the radius of circle in PostGIS using query in php?

I have the geometry type defined for the polygons and circle in PostGIS and now I need to find the radius of the circle in this geometry type?? I have found the centre by using ST_X(ST_ASTEXT(ST_CENTROID(shape))) AS lat, ST_Y(ST_ASTEXT(ST_CENTROID(shape))) as lgt, Can anyone help me to get the radius ??

You can use the ST_Envelope function to get the bounding box of any polygon (not just a circle). You can then use the ST_XMin , ST_YMin , ST_XMax and ST_YMax functions to extract the width and height. As it is a circle, you could use either, so something like:

SELECT (ST_XMax(bbox)-ST_XMin(bbox))/2 as radius
FROM 
  (SELECT ST_Envelope(shape) as bbox from sometable) env 

where the sub-query is just shorthand to avoid having to call ST_Envelope twice, but you could also write,

SELECT (ST_XMax(ST_Envelope(shape)) - ST_XMin(ST_Envelope(shape)))/2 as radius
FROM sometable

which seems shorter, but, is I feel less elegant, and would be longer if you wanted the height. The query optimizer will view them as the same query, either way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM