Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wboykinm/9678706 to your computer and use it in GitHub Desktop.
Save wboykinm/9678706 to your computer and use it in GitHub Desktop.
Leaving off here for the night.
DROP FUNCTION IF EXISTS Faraday_JoyDivision(
field TEXT,
grid_tolerance FLOAT,
offset_mult FLOAT,
offset_correction FLOAT
);
CREATE FUNCTION Faraday_JoyDivision(
field TEXT,
grid_tolerance FLOAT DEFAULT 0.002,
offset_mult FLOAT DEFAULT 0.00004,
offset_correction FLOAT DEFAULT 0.06
) RETURNS TABLE(
the_geom GEOMETRY
)
AS $$
DECLARE
query TEXT;
BEGIN
query := '
WITH
grid AS (
SELECT
-- note transform here
ST_SnapToGrid(ST_Transform(the_geom, 4326), $1) the_geom,
'|| field ||'
FROM
joydivisiontest
WHERE
'|| field ||' IS NOT NULL
),
stats AS (
SELECT
the_geom,
avg('|| field ||') avg_field,
ST_Y(the_geom) pulserow,
ST_X(the_geom) pulseorder
FROM
grid
GROUP BY
the_geom
),
offsets AS (
SELECT
pulserow,
pulseorder,
CASE WHEN ST_Y(ST_Translate(the_geom, 0, (avg_field * $2)-$3)) > pulserow THEN ST_Translate(the_geom, 0, (avg_field * $2)-$3)
ELSE the_geom
END AS the_geom
FROM
stats
),
lines AS (
SELECT
pulserow,
ST_MakeLine(the_geom ORDER BY pulseorder ASC) the_geom
FROM
offsets
GROUP BY
pulserow
),
hulls AS (
SELECT
round(pulserow::numeric,1)::text || round(pulseorder::numeric,1)::text groupHULL,
St_Buffer(ST_ConcaveHull(ST_Collect(the_geom), 0.99),0.01) the_geom
FROM
offsets
GROUP BY
groupHull
)
SELECT
st_intersection(a.the_geom, b.the_geom) the_geom
FROM
hulls a, lines b
WHERE
st_intersects(b.the_geom, a.the_geom)';
RETURN QUERY EXECUTE query USING grid_tolerance, offset_mult, offset_correction;
END
$$ LANGUAGE 'plpgsql' STABLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment