some issues related to write kml files

38 views (last 30 days)
lang
lang on 19 Oct 2025 at 7:36
Edited: lang on 31 Oct 2025 at 7:16
I have encountered an issue regarding the ordering of polygon vertices and writing to KML files. As demonstrated in the following code, I need to convert the polygon vertices to a counterclockwise order ([lonb, latb] = poly2ccw(lonb, latb); this is mandated by some data services). However, the output KML file appears somewhat strange (Figure 2). How can I obtain a KML file similar to Figure 1? I have limited knowledge in this area, and any advice would be greatly appreciated.
The lonb and latb are vectors. I noticed that after using poly2ccw to change the order of vertices, the holes within the polygon were filled. This is because MATLAB defines clockwise-arranged points as the outer boundary and counterclockwise-arranged points as the inner boundary. In short, I gave up continuing to use the KML format and switched to using shapefiles to save polygon files, as the latter seems not to require me to change the order of vertices. The relevant code is as follows, and I hope it is helpful.
% load coastline
load('coast_polygon_c.mat', 'lonb', 'latb');
% check direction
ispolycw(lonb, latb)
% plot
shape = geopolyshape(latb, lonb);
shape.GeographicCRS = geocrs(4326);
figure;geoplot(shape);
% write shapefile
GT = table(shape, VariableNames={'Shape'});
shapewrite(GT, 'coast_polygon_c');
% convert to counter-clockwise
[lonb, latb] = poly2ccw(lonb, latb);
% save and write kml
kmlwritepolygon('coast_polygon_c.kml', latb, lonb);
% plot
GT=readgeotable('coast_polygon_c.kml');
figure;geoplot(GT);
geolimits([min(latb)-1, max(latb)+1], [min(lonb)-1, max(lonb)+1]);

Accepted Answer

Nithin
Nithin on 31 Oct 2025 at 5:26
Hi @lang,
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:

More Answers (0)

Products


Release

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!