ST_StartPoint — Returns the first point of a LineString, CircularLineString, or NURBSCurve.
geometry ST_StartPoint(geometry geomA);
Returns the first point of a LINESTRING, CIRCULARLINESTRING, or NURBSCURVE geometry as a POINT. For other geometries, returns the first point in coordinate order.
Cette méthode implémente la spécification SQL/MM. SQL-MM 3 : 7.1.3
Cette fonction prend en charge la 3D et ne supprime pas l'indice z.
Cette méthode prend en charge les types Circular String et Curve.
Amélioré : 3.2.0 renvoie un point pour toutes les géométries. Le comportement précédent renvoyait NULL si l'entrée n'était pas une LineString.
Modifié : 2.0.0 ne fonctionne plus avec les MultiLineStrings à géométrie unique. Dans les anciennes versions de PostGIS, une MultiLineString à géométrie unique fonctionnait sans problème avec cette fonction et renvoyait le point de départ. Dans la version 2.0.0, elle renvoie simplement NULL comme toute autre MultiLineString. L'ancien comportement était une fonctionnalité non documentée, mais les personnes qui supposaient que leurs données étaient stockées en tant que LINESTRING peuvent voir ces données retourner NULL dans la version 2.0.0.
Point de départ d'une LineString
SELECT ST_StartPoint('LINESTRING(0 1,0 2)'::geometry);
POINT(0 1)
Start point of a Point is the Point itself
SELECT ST_StartPoint('POINT(0 1)'::geometry);
POINT(0 1)
Point de départ d'une LineString 3D
SELECT ST_StartPoint('LINESTRING(0 1 1,0 2 2)'::geometry);
POINT(0 1 1)
Point de départ d'une CircularString
SELECT ST_StartPoint('CIRCULARSTRING(5 2,-3 2,-2 1,-4 2,6 3)'::geometry);
POINT(5 2)
Start point of a NURBSCurve
SELECT ST_StartPoint('NURBSCURVE(2, (0 0, 1 1, 2 0))'::geometry);
POINT(0 0)
Build edge node references from the start and end points of a LineString table. The example uses exact endpoint equality, so linework with nearly coincident endpoints should be snapped or otherwise cleaned first.
WITH roads(road_id, geom) AS (
VALUES
(1, 'LINESTRING(0 0, 1 0)'::geometry),
(2, 'LINESTRING(1 0, 1 1)'::geometry),
(3, 'LINESTRING(1 0, 2 0)'::geometry)
),
endpoints AS (
SELECT road_id, 'from' AS end_name, ST_StartPoint(geom) AS geom,
ST_AsEWKB(ST_StartPoint(geom)) AS endpoint_key
FROM roads
UNION ALL
SELECT road_id, 'to' AS end_name, ST_EndPoint(geom) AS geom,
ST_AsEWKB(ST_EndPoint(geom)) AS endpoint_key
FROM roads
),
nodes AS (
SELECT dense_rank() OVER (ORDER BY ST_X(geom), ST_Y(geom), endpoint_key) AS node_id,
endpoint_key
FROM (
SELECT DISTINCT ON (endpoint_key) endpoint_key, geom
FROM endpoints
ORDER BY endpoint_key
) AS distinct_endpoints
)
SELECT e.road_id,
max(n.node_id) FILTER (WHERE e.end_name = 'from') AS from_node,
max(n.node_id) FILTER (WHERE e.end_name = 'to') AS to_node
FROM endpoints AS e
JOIN nodes AS n USING (endpoint_key)
GROUP BY e.road_id
ORDER BY e.road_id;
road_id | from_node | to_node
---------+-----------+---------
1 | 1 | 2
2 | 2 | 3
3 | 2 | 4