closePolygonParts

Close all rings in multipart polygon

Syntax

[xdata, ydata] = closePolygonParts(xdata, ydata)
[lat, lon] = closePolygonParts(lat, lon, angleunits)

Description

[xdata, ydata] = closePolygonParts(xdata, ydata) ensures that each ring in a multipart (NaN-separated) polygon is "closed" by repeating the start point at the end of each ring, unless the start and end points are already identical. Coordinate vectors xdata and ydata must match in size and have identical NaN locations.

[lat, lon] = closePolygonParts(lat, lon, angleunits) works with latitude-longitude data and accounts for longitude wrapping with a period of 360 if angleunits is 'degrees' and 2*pi if angleunits is 'radians'. For a ring to be considered closed, the latitudes of its first and last vertices must match exactly, but their longitudes need only match modulo the appropriate period. Such rings are returned unaltered.

Examples

Closing a polygon in plane coordinates

xOpen = [1 0 2 NaN 0.5 0.5 1 1];
yOpen = [0 1 2 NaN 0.8 1 1 0.8];
[xClosed, yClosed] = closePolygonParts(xOpen, yOpen)
xClosed =
  Columns 1 through 7 
    1.0000     0    2.0000   1.0000   NaN   0.5000   0.5000
Columns 8 through 10 
1.0000    1.0000    0.5000

yClosed =
Columns 1 through 7 
0    1.0000    2.0000         0       NaN    0.8000    1.0000
Columns 8 through 10 
1.0000    0.8000    0.8000

whos
  Name         Size            Bytes  Class     Attributes

  xClosed      1x10               80  double              
  xOpen        1x8                64  double              
  yClosed      1x10               80  double              
  yOpen        1x8                64  double              

Closing a polygon in latitude-longitude coordinates

% Construct a two-part polygon based on coast.mat.  The first ring
% is Antarctica.  The longitude of its first vertex is -180 and the
% longitude of its last vertex is 180.  The second ring is a small
% island from which the last vertex, a replica of the first vertex,
% is removed.
c = load('coast.mat');
[latparts, lonparts] = polysplit(c.lat, c.long);
latparts{2}(end) = [];
lonparts{2}(end) = [];
latparts(3:end) = [];
lonparts(3:end) = [];
[lat, lon] = polyjoin(latparts, lonparts);
 
% Examine how closePolygonParts treats the two rings.  In both
% cases, the first and last vertices differ.  However, Antarctica
% remains unchanged while the small island is closed back up.
[latClosed, lonClosed] = closePolygonParts(lat, lon, 'degrees');
[latpartsClosed, lonpartsClosed] = polysplit(latClosed, lonClosed);
lonpartsClosed{1}(end) - lonpartsClosed{1}(1)  % Result is 360
lonpartsClosed{2}(end) - lonpartsClosed{2}(1)  % Result is 0
Was this topic helpful?