some issues related to write kml files
Show older comments
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
More Answers (0)
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!