简体   繁体   中英

Counting points a on linestring

I am trying to counts the number of points on a line for each row in the following table

CREATE TABLE outils.prod(
pk  INTEGER PRIMARY KEY,
cable VARCHAR (25),
PA  VARCHAR (10),       
Art VARCHAR(7),
FT      Numeric,
BT      Numeric
);

INSERT INTO  outils.prod  (pk)
SELECT id_ftth 
FROM outils.cable
WHERE type_cable = '2' ;

SELECT ADDGEOMETRYCOLUMN('outils','prod','geom',2154,'MultiLineString',2);

I have tried to update my line table but i have trouble getting an answer for each row.

UPDATE outils.prod SET FT=(SELECT COUNT( ST_INTERSECTION(outils.prod.geom,outils.ft.geom))
 FROM outils.prod , outils.ft)

With the above code i managed to get the total number of intersection for every line but i would like to have the count by line in my line table.

Thank you , Hugo

You would have to write a sub-query to do the count per line.

Also you don't need to compute the intersection (the geom), but just to check if they intersect, which is much faster.

UPDATE outils.prod 
SET FT= sub.cnt
FROM (
 SELECT pk, count(*) as cnt
 FROM outils.ft
 JOIN outils.prod ON ST_INTERSECTS(prod.geom, ft.geom)
)
WHERE prod.pk = sub.pk;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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