如何使用 Postgis 查找我所在城市的所有街道交叉口

How to find all street intersections in my city using Postgis

Hi, I have to find all street intersections, I wrote below code but it returns duplicate rows. Does anyone have an idea why or khnows how to correct it!! Thank you for your help.

DROP TABLE IF EXISTS toto;

CREATE TABLE toto(
gid serial primary key,
nom_voie1 varchar(50),
nom_voie2 varchar(50),
geom_inter geometry(Geometry,4326)
);

CREATE INDEX ON toto using gist (geom_inter);

INSERT INTO toto(nom_voie1, nom_voie2, geom_inter)
SELECT a.nom_voie, b.nom_voie, ST_Intersection(a.geom, b.geom)
FROM reseau_routier AS a , reseau_routier AS b 
WHERE ST_Intersects(a.geom,b.geom) 
  AND ST_Touches(a.geom, b.geom) 
  AND a.gid < b.gid 
  AND a.nom_voie <> b.nom_voie; 

SELECT DISTINCT a.nom_voie, b.nom_voie, ST_Intersection(a.geom, b.geom)

由于 reseau_routier 上的 JOIN,您被复制了。

您可以删除重复项:

INSERT INTO toto(nom_voie1, nom_voie2, geom_inter)
SELECT a.nom_voie, b.nom_voie, ST_Intersection(a.geom, b.geom)
FROM reseau_routier AS a , reseau_routier AS b 
WHERE ST_Intersects(a.geom,b.geom) 
  AND ST_Touches(a.geom, b.geom) 
  AND a.gid < b.gid 
  AND a.nom_voie <> b.nom_voie
GROUP BY  a.nom_voie, b.nom_voie, ST_Intersection(a.geom, b.geom);

但是这些重复的是格式错误的查询的标志。有了完整的数据样本,就可以更轻松地为您提供完美的查询。

使用显式连接而不是隐式连接

INSERT INTO toto(nom_voie1, nom_voie2, geom_inter)
SELECT a.nom_voie, b.nom_voie, ST_Intersection(a.geom, b.geom)
FROM reseau_routier AS a
JOIN reseau_routier AS b on ST_Intersects(a.geom,b.geom)
  AND a.gid < b.gid 
  AND a.nom_voie <> b.nom_voie;