MATLAB Examples

Extracting and Joining Polygons or Line Segments

This example shows how to identify line or patch segments once they have been combined into large NaN-clipped vectors. You can separate these polygon or line vectors into their component segments using the polysplit function, which takes column vectors as inputs. To join together individual polygon or line vectors use polyjoin.

Create two NaN-delimited arrays in the form of column vectors.

lat = [45.6 -23.47 78 NaN 43.9 -67.14 90 -89]';
lon = [13 -97.45 165 NaN 0 -114.2 -18 0]';

Split the column vectors into individual line segment cell arrays at the NaN separators using polysplit.

[latc,lonc] = polysplit(lat,lon)
latc =

  2x1 cell array

    {3x1 double}
    {4x1 double}


lonc =

  2x1 cell array

    {3x1 double}
    {4x1 double}

Inspect the contents of the cell arrays. Note that each cell array element contains a segment of the original line.

[latc{1} lonc{1}]
[latc{2} lonc{2}]
ans =

   45.6000   13.0000
  -23.4700  -97.4500
   78.0000  165.0000


ans =

   43.9000         0
  -67.1400 -114.2000
   90.0000  -18.0000
  -89.0000         0

To reverse the process, use polyjoin.

[lat2,lon2] = polyjoin(latc,lonc);

Perform a logical comparison of the joined segments. Note that they are identical with the initial lat and lon arrays. The logical comparison is false for the NaN delimiters, by definition.

[lat lon] == [lat2 lon2]
ans =

  8x2 logical array

   1   1
   1   1
   1   1
   0   0
   1   1
   1   1
   1   1
   1   1

Test for global equality, including NaN values.

isequaln(lat,lat2) & isequaln(lon,lon2)
ans =

  logical

   1