Link Line Segments with Common Endpoints into Polygons

This example shows how to link line segments into polygons using the polymerge function. polymerge links sets of line segments together by concatenating segments that have matching endpoints. An end point can be either the first or last vertex in a given part. The polymerge function compares endpoints of segments within latitude and longitude vectors to identify endpoints that match exactly or lie within a specified distance. The matching segments are then concatenated, and the process continues until no more coincidental endpoints can be found. For more information, see the polymerge reference page.

Construct column vectors representing coordinate values. The vectors use NaN separators to define four line segments.

lat = [3 2 NaN 1 2 NaN 5 6 NaN 3 4]';
lon = [13 12 NaN 11 12 NaN 15 16 NaN 13 14]';

Concatenate the segments with matching endpoints. Three of the line segments have overlapping end points, so polymerge returns two line segments.

[latm, lonm] = polymerge(lat,lon)
latm = 8×1

     1
     2
     3
     4
   NaN
     5
     6
   NaN

lonm = 8×1

    11
    12
    13
    14
   NaN
    15
    16
   NaN