Main Content

Write WMS Layers to KML File

Some WMS server implementations, such as GeoServer, can render their maps in a non-image format, such as KML. KML is an XML dialect used by Google Earth™ and Google Maps™ browsers. The WebMapServer.getMap method and the wmsread function do not allow you to use the KML format because they import only standard graphics image formats. Work around this limitation by using the WMSMapRequest.RequestURL property.

Get the capabilities document for the MassGIS WMS server. Get the list of layers from the capabilities document. Then, refine the layers to find a FEMA Flood Zone layer.

geoserver = wmsinfo("https://gis-prod.digital.mass.gov/geoserver/wms?");
massgis = geoserver.Layer;
floodzone = refine(massgis,'FEMA Flood Zones','SearchField','LayerTitle');
floodzone = floodzone(1);

Specify geographic limits for a region around Boston, Massachusetts.

latlim = [ 42.305  42.417];
lonlim = [-71.131 -70.99];

Create a WMSMapRequest object and set the geographic limits.

request = WMSMapRequest(floodzone);
request.Latlim = latlim;
request.Lonlim = lonlim;

Read the graphics image from the server.

[A,R] = wmsread(request.RequestURL);

Display the image on a map.

figure
usamap(A,R)
geoshow(A,R)

Request an image format that opens in Google Earth.

request.ImageFormat = 'application/vnd.google-earth.kml+xml';

Use the websave function to write out a KML file.

filename = 'floodzone.kml';
websave(filename,request.RequestURL);

Open the file with Google Earth to view. On Windows® platforms, you can display the KML file by uncommenting this code.

% winopen(filename)

For UNIX® and Mac users, you can display the KML file by uncommenting this code.

% cmd = 'googleearth ';
% fullfilename = fullfile(pwd, filename);   
% system([cmd fullfilename])

See Also

| | |

Related Topics