简体   繁体   English

如何使用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; 

在此处输入图片说明

You are getting duplicated because of the JOIN on reseau_routier . 由于reseau_routier上的JOIN reseau_routier您变得重复。

You can remove duplicated with: 您可以使用以下方法删除重复项:

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);

But these duplicated are the sign of a malformed query. 但是这些重复是畸形查询的征兆。 With a full data sample it would be easier to give you the perfect query. 使用完整的数据样本,可以更轻松地为您提供完美的查询。

Use explicit join instead of implicite 使用显式联接而不是隐式联接

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; 
SELECT DISTINCT a.nom_voie, b.nom_voie, ST_Intersection(a.geom, b.geom)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM