The reason for the difference in the KML-read image is that KML expects coordinates in (longitude, latitude) order. Additionally, the "kmlwritepolygon" function expects vectors (not cell arrays) and uses NaN-separated polygons when there are multiple polygons.
When you use "poly2ccw", your polygon might be split into multiple parts (such as holes and outer rings). The way you write these to KML is important.
To fix this, check if your output from "poly2ccw" is a cell array or a matrix. If it’s a cell array, you need to concatenate the polygons with "NaN" separators. Also, when reading the KML back, make sure to zoom to the correct region or plot only the polygon, not the entire world.
Please refer to the code below for a better understanding:
% Load data
load('coast_polygon_c.mat', 'lonb', 'latb');
% Convert to counter-clockwise
[lonb_ccw, latb_ccw] = poly2ccw(lonb, latb);
% Concatenate with NaN if needed
if iscell(lonb_ccw)
lonb_kml = [];
latb_kml = [];
for k = 1:length(lonb_ccw)
lonb_kml = [lonb_kml; lonb_ccw{k}; NaN];
latb_kml = [latb_kml; latb_ccw{k}; NaN];
end
else
lonb_kml = lonb_ccw;
latb_kml = latb_ccw;
end
% Write to KML
kmlwritepolygon('coast_polygon_c.kml', latb_kml, lonb_kml);
% Read and plot
GT = readgeotable('coast_polygon_c.kml');
figure; geoplot(GT);
geolimits([min(latb_kml)-1, max(latb_kml)+1], [min(lonb_kml)-1, max(lonb_kml)+1]);
For more information, refer to the following MathWorks documentation:
