I wrote a function that creates arbitrary quadrants within a polygon
The first parameter is the polygon ID, the second one is the start angle and with the third one the quadrant is finally spanned.
CREATE TYPE quadrant AS (id smallint,geom geometry(polygon,31468))
Here is the function
CREATE OR REPLACE FUNCTION quadrant(id integer,start double precision, stop integer) RETURNS SETOF quadrant AS $$ WITH centroid AS (SELECT accumulation.partnumber, (ST_Centroid(accumulation.geom)) AS vertex FROM accumulation WHERE partnumber=$1 ), newline AS (SELECT ST_SetSRID(ST_Translate( ST_Rotate( ST_MakeLine( ST_MakePoint(0.0,2000.0), ST_MakePoint(0.0,0.0)), radians(($2+s.a)*-1)), ST_X(vertex), ST_Y(vertex)), ST_SRID(vertex)) AS geom FROM centroid, generate_series(0,$3,$3) AS s(a) ), span AS (SELECT centroid.partnumber, ST_LineMerge(ST_Union(newline.geom)) AS geom FROM newline, centroid GROUP BY partnumber), multiobject AS (SELECT span.partnumber, ST_Split(accumulation.geom,span.geom) AS geom, generate_series(1,100) AS n FROM span, accumulation WHERE accumulation.partnumber=$1), objects AS (SELECT n, ST_GeometryN(multiobject.geom,n) AS geom FROM multiobject WHERE n <= ST_NumGeometries(multiobject.geom)) SELECT partnumber AS id, objects.geom FROM objects, multiobject WHERE multiobject.n <= ST_NumGeometries(multiobject.geom) ORDER BY LEAST(ST_Area(ST_GeometryN(objects.geom,multiobject.n))) LIMIT 1 $$ LANGUAGE 'sql';
I applied this function for 300 polygons and I noticed a problem.
My last query results the smallest piece from the split. But in some cases, when the first newline (see subquery
newline) crosses the polygon more than one time, there is a smaller piece that represents the quadrant (see the most right object in the picture below).
The query doesn't calculate the right quadrant for the left object in the picture. In this case the smaller area of the split is not the quadrant, because the polygon is plumped to the northeast direction.
So my question is, how I can enhance my query to avoid these incorrect quadrants. Maybe I have to overthink the function generally.