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.

Search the WMS Database for layers on any GeoServer. Refine the search to include only the layers from the MassGIS server. Refine the search again to return a FEMA Flood Zone layer.

geoserver = wmsfind('geoserver','SearchField','any');
massgis = refine(geoserver,'massgis*wms','SearchField',...
floodzone = refine(massgis,'FEMA Flood Zones','SearchField',...
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.


Request an image format that opens in Google Earth.

request.ImageFormat = 'application/';

Use the websave function to write out a KML file.

filename = 'floodzone.kml';

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