This PostgreSQL/PostGIS function, reorder_multilinestring
, takes a MULTILINESTRING geometry and returns a new MULTILINESTRING with components reordered and oriented based on geometric criteria. It is particularly useful for GIS applications where the order and orientation of line segments matter, such as routing, network analysis, or pipeline management.
- Component extraction: Splits the input multilinestring into individual lines using
ST_Dump
. - Sequence ordering: Orders line components by minimum distance between consecutive endpoints, creating a coherent path.
- Orientation of components: Ensures each line is oriented correctly to connect smoothly to the previous line.
- Global orientation check: Optionally accepts two reference points. If the first point appears after the second along the assembled line, the entire multilinestring is reversed.