Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote a function that creates arbitrary quadrants within a polygon

SELECT (quadrant(20,0.0,90)).*

The first parameter is the polygon ID, the second one is the start angle and with the third one the quadrant is finally spanned.

First the TYPE

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
    (ST_Centroid(accumulation.geom)) AS vertex
FROM accumulation
    WHERE partnumber=$1
newline AS
          ST_X(vertex), ST_Y(vertex)),
ST_SRID(vertex)) AS geom
FROM centroid, generate_series(0,$3,$3) AS s(a)
span AS
    ST_LineMerge(ST_Union(newline.geom)) AS geom
FROM newline, centroid
    GROUP BY partnumber),
multiobject AS
    ST_Split(accumulation.geom,span.geom) AS geom,
    generate_series(1,100) AS n
FROM span, accumulation
    WHERE accumulation.partnumber=$1),
objects AS
    ST_GeometryN(multiobject.geom,n) AS geom
FROM multiobject
    WHERE n <= ST_NumGeometries(multiobject.geom))

    partnumber AS id,
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.

enter image description here

share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.