Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outer query introduces duplicate rows #4

Open
jelmervdl opened this issue Aug 7, 2018 · 0 comments
Open

Outer query introduces duplicate rows #4

jelmervdl opened this issue Aug 7, 2018 · 0 comments
Labels

Comments

@jelmervdl
Copy link
Collaborator

The following query should yield five rows, but results in 10 instead:

WITH
	line as (
		SELECT
			ST_SetSRID(ST_MakeLine(ARRAY[
				ST_MakePoint(10, 80),
				ST_MakePoint(50, 80),
				ST_MakePoint(10, 30),
				ST_MakePoint(90, 30)
			]), 28992) as geom
	),
	points AS (
		SELECT
			t.id,
			ST_SetSRID(t.geom, 28992) as geom
		FROM
			(VALUES
				(1, ST_MakePoint(40, 60)),
				-- (2, ST_MakePoint(50, 50)),
				(3, ST_MakePoint(60, 40))
			) AS t (id, geom)
	),
	circles AS (
		SELECT
			p.id as point_id,
			p.geom as point_geom,
			ST_Buffer(p.geom, 40) as circle_geom
		FROM
			points p
	),
	intersections_as_sets AS (
		SELECT
			c.*,
			ST_Dump(ST_Intersection(c.circle_geom, l.geom)) as intersection
		FROM
			circles c,
			line l
	),
	intersections_as_lines AS (
		SELECT
			i.point_id,
			i.point_geom,
			i.point_id::text || ':' || (i.intersection).path::text || ':' || s.index::text as id,
			ST_SetSRID(ST_MakeLine(
				ST_PointN((i.intersection).geom, s.index),
				ST_PointN((i.intersection).geom, s.index + 1)
			), 28992) as intersection_geom
		FROM
			intersections_as_sets i,
			LATERAL (
				SELECT
					generate_series(1, ST_NPoints((i.intersection).geom) - 1) as index
			) s
	),
	intersections_as_rows AS (
		SELECT
			i.*,
			ST_LineLocatePoint(l.geom, i.point_geom) as pos,
			ST_LineLocatePoint(l.geom, ST_StartPoint(i.intersection_geom)) as pos_start,
			ST_LineLocatePoint(l.geom, ST_EndPoint(i.intersection_geom)) as pos_end,
			ST_Distance(l.geom, i.point_geom) as distance
		FROM
			intersections_as_lines i,
			line l
	)
SELECT * FROM intersections_as_rows
@jelmervdl jelmervdl added the bug label Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant