2

How would I save the following in a postgis geometry field? How would the WKT look like for the arc or fan. Is there a ARC well known text to save this?

ArcFan

Theuns Heydenrych
  • 449
  • 1
  • 4
  • 12

1 Answers1

1

one could define a custom "constructor" for a sector defined in terms of three points p1,p2,p3 as follows (p1 defines the center):

CREATE OR REPLACE FUNCTION ST_FormatPoint(p GEOMETRY)
  RETURNS text AS
$$
BEGIN
  RETURN ST_X(p) || ' ' || ST_Y(p);
END;
$$
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION ST_Sector(p1 GEOMETRY, p2 GEOMETRY, p3 GEOMETRY)
  RETURNS GEOMETRY AS
$$
DECLARE
  phi double precision;
  p4 geometry;
BEGIN
  phi := (ST_Azimuth(p1, p3) - ST_Azimuth(p1, p2))/2;
  p4 := ST_Rotate(p2, -phi, p1);

  RETURN ST_GeomFromText(
    'CURVEPOLYGON(CIRCULARSTRING(' ||
      ST_FormatPoint(p1) || ', ' || ST_FormatPoint(p1) || ', ' ||
      ST_FormatPoint(p2) || ', ' || ST_FormatPoint(p4) || ', ' || ST_FormatPoint(p3) || ', ' ||
      ST_FormatPoint(p1) || ', ' || ST_FormatPoint(p1)
    || '))');
END;
$$
LANGUAGE 'plpgsql';

Then, a query

WITH s as (
    select
        ST_Sector(ST_Point(0,0), ST_Point(1,0), ST_Point(0, 1)) as poly
)
select st_perimeter(poly), st_area(poly) from s;

yields the expected result

  st_perimeter   |      st_area      
-----------------+-------------------
 3.5707963267949 | 0.785082789238688
(1 row)
ewcz
  • 12,819
  • 1
  • 25
  • 47