Write geographic data to KML file
The following syntaxes, while still supported, are not recommended.
Use kmlwritepoint instead.
kmlwrite(filename,lat,lon)
kmlwrite(filename,lat,lon,alt)
kmlwrite( writes
the geographic point, line, or polygon data stored in filename,S)S to
the file specified by filename in Keyhole Markup
Language (KML) format. S is a geopoint vector,
a geoshape vector, or geostruct. kmlwrite creates
a KML Placemark in the file and populates the tags in the placemark
with the data in S.
kmlwrite(
writes filename,address)address to the file specified by filename
in KML format. address is a string scalar or character vector
containing freeform address data, that can include street, city, state, country, and/or
postal code. To specify multiple addresses, use a cell array of character vectors or string
scalars. kmlwrite creates a KML Placemark in the file, setting the value
of the address tag. An address is an alternative way to specify a point, instead of using
latitude and longitude.
kmlwrite(___, specifies
name-value pairs that set additional KML feature properties. Parameter
names can be abbreviated and are case-insensitive.Name,Value)
Return point data in geopoint vector.
placenames = gpxread('boston_placenames');Define the name of the KML file you want to create.
filename = 'Boston_Placenames.kml';Define the colors you want to use with the point data.
colors = jet(length(placenames));
Write the point data to the file, using optional parameters to specify names for the points and define the colors used for the icons.
kmlwrite(filename, placenames, 'Name', placenames.Name,'Color',colors );
Read line features into a geoshape vector.
tracks = gpxread('sample_tracks', 'Index', 1:2);
Define the name of the KML file you want to create.
filename = 'tracks.kml';Write the line data to the file, using several optional parameters to specify the color and width of the lines, and their names and descriptions.
colors = {'red', 'green'};
description = tracks.Metadata.Name;
name = {'track1', 'track2'};
kmlwrite(filename, tracks, 'Color', colors, 'LineWidth', 2, ...
'Description', description, 'Name', name);Read geographic data (locations of major European cities) from a shape file, including the names of the cities. This returns a structure.
latlim = [ 30; 75]; lonlim = [-25; 45]; cities = shaperead('worldcities.shp','UseGeoCoords', true, ... 'BoundingBox', [lonlim, latlim]);
Convert the structure to a geopoint vector.
cities = geopoint(cities);
Define the name of the KML file you want to create.
filename = 'European_Cities.kml';Write the geographic data to a KML file. Use the optional Name parameter to include the names of the cities in the placemarks. Remove the default Description table.
kmlwrite(filename, cities, 'Name', cities.Name, 'Description',{});
Create a cell array of unstructured addresses (the names of several Australian cities).
address = {'Perth, Australia', ...
'Melbourne, Australia', ...
'Sydney, Australia'};Define the name of the KML file you want to create.
filename = 'Australian_Cities.kml';Write the unstructured address data to a KML file, using the optional Name parameter to include the names of the cities in the placemarks.
kmlwrite(filename, address, 'Name', address);Read polygon data from file, returned in a structure.
S = shaperead('usastatelo','UseGeoCoords',true);
Convert the structure to a geoshape vector.
S = geoshape(S);
Write the polygon data to a KML file, using optional parameters to specify the colors of the polygon faces and edges.
filename = 'usastatelo.kml'; colors = polcmap(length(S)); kmlwrite(filename,S,'Name',S.Name,'FaceColor',colors,'EdgeColor','k')
filename — Name of output fileName of output file, specified as a string scalar or character vector.
kmlwrite writes the file in the current folder, unless you specify
a full or relative path name. If the file name includes an extension, it must be
.kml.
Data Types: char | string
S — Geographic features to write to fileGeographic features to write to file, specified as a geopoint
vector, a geoshape vector, or geostruct. The attribute fields of S appear
as a table in the Description tag of the Placemark for each element
of S. The attribute fields appear in the table
in the same order as they occur in S.
If S is a geoshape vector, the Geometry field
identifies the type of the data: 'point', 'line',
or 'polygon'.
If S is a geostruct with X and Y fields, kmlwrite returns
an error.
If S contains valid altitude data, kmlwrite writes
the field values to the file as KML altitudes and sets the altitude
interpretation to 'relativeToSeaLevel'. If S does
not contain altitude data, kmlwrite sets the altitude
field in the file to 0 and sets the altitude interpretation
to 'clampToGround'. The altitude
data can be in a field named either Elevation, Altitude,
or Height. If S contains fields
with more than one of these names, kmlwrite issues
a warning and ignores the altitude fields.
address — Location of KML placemarkLocation of KML placemark, specified as a string scalar, character vector or cell array of
character vectors containing freeform address data, such as street, city, state, and
postal code. If address is a cell array, each cell represents a
unique location.
Data Types: char | string | cell
Specify optional
comma-separated pairs of Name,Value arguments. Name is
the argument name and Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN.
'Name','Point Reyes''Name' — Label of object displayed in viewer'Address N', 'Point N',
'Line N', or 'Polygon N', where
N is the index of the feature. (default) | string scalar | character vector | cell array of character vectorsLabel for an object displayed in viewer, specified as a string scalar, character vector, or
cell array of character vectors. If you specify a string scalar or character vector,
kmlwrite applies the name to all objects. If a string vector or
cell array of character vectors, you must specify a name for each feature. That is,
the cell array must be the same length as S or
address. The following describes the default behavior for various
features.
| Feature | Default Name |
|---|---|
| Address | 'Address N' where N is
the index of the feature. |
| Point | 'Point N' where N is
the index of the feature. |
| Multipoint | 'Multipoint N' where N is
the index of the feature. kmlwrite places the points
in the named folder and each point is named 'Point M' where M is
the index of the point. |
| Line | 'Line N' where N is
the index of the feature. If the line data contains NaN values, kmlwrite places
the line segments in a folder named 'Segment M',
where M is the line segment number. |
| Polygon | 'Polygon N' where N is
the index of the feature. If the polygon vertex list contains multiple
outer rings, kmlwrite places each ring in a folder labeled 'Part
M', where M is the number for
that feature. |
Data Types: char | string | cell
'Description' — Content to be displayed in the placemark description balloonContent to be displayed in the placemark description balloon, specified as a string scalar or
character vector, cell array of character vectors, or an attribute specification.
kmlwrite uses this data to set the values of the feature
description tags. The description appears in the description balloon associated with
the feature in Google Earth.
If you specify a string scalar or character vector,
kmlwrite applies the description to all objects.
If you specify a string vector or cell array of character vectors, there must
be one label for each feature; that is, it must be the same length
asS or address.
Description elements can be either plain text or tagged with HTML mark up. When in plain text, google Earth applies basic HTML formatting automatically. For example, Google Earth replaces newlines with line break tags and encloses valid URLs in anchor tags to make them hyperlinks. To see examples of HTML tags that are recognized by Google Earth, view https://earth.google.com.
If you provide an attribute specification, the attribute fields
of S display as a table in the description tag
of the placemark for each element of S, in the
order in which the fields appear in the specification. To construct
an attribute spec, call makeattribspec and
then modify the output to remove attributes or change the Format field
for one or more attributes. The latitude and longitude coordinates
of S are not considered to be attributes. If included
in an attribute spec, kmlwrite ignores them.
Data Types: char | string | cell
'Icon' — File name of custom iconFile name of custom icon, specified as a string scalar, character vector or cell array of character vectors.
If a string scalar or character vector, kmlwrite applies
the value to all icons.
If a string vector or cell array of character vectors, specify an icon for
each feature; that is, the cell array must be the same length as
S or address.
If the string scalar or character vector is an Internet URL, the URL must include the protocol type.
If the icon file name is not in the current folder, or in a folder on the MATLAB path, specify a full or relative path name.
Data Types: char | string | cell
'IconScale' — Scaling factor for iconScaling factor for the icon, specified as a positive numeric scalar or vector.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32
'Color' — Color of icons, lines, or the faces and edges of polygonsColor of icons, lines, or the faces and edges of polygons, specified
as a MATLAB Color Specification (ColorSpec),
which can be a character vector, cell array, or double array
with values in the range [0 1].
If a string scalar or character vector, kmlwrite applies
the color to all features
If a string vector or cell array of character vectors, specify a color for
each feature. That is, the cell array must be the same length as
S or address.
If a double array, specify an M-by-3
array, where M is the same length as S or address.
If S is a polygon geoshape, you
can specify 'none', which indicates that
the polygon is not filled and has no edge. Also, for polygons, Color specifies
the color of polygon faces if FaceColor is not
specified and polygon edges if EdgeColor is not
specified.
Data Types: double | char | cell
'Alpha' — Transparency of the icons, lines, or the faces and edges of polygons1 (fully opaque) (default) | numeric scalar or vector in the range [0 1]Transparency of the icons, lines, or the faces and edges of
polygons, specified as a numeric scalar or vector in the range [0
1]. If a scalar, kmlwrite applies the value to all features.
If a vector, specify a value for each feature; that is, the vector
must be the same length as S or address.
If S is a polygon geoshape, kmlwrite applies
the value to all the polygon faces if FaceAlpha is
not specified and the polygon edges if EdgeAlpha is
not specified.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32
'LineWidth' — Width of lines and polygon edges in pixelsWidth of lines and polygon edges in pixels, specified as a positive numeric scalar or vector.
If a scalar, kmlwrite applies the
value to all polygon edges.
If a vector, specify a value for each feature. That
is, the vector must have the same length as S.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32
'FaceColor' — Color of polygon faces'none'Color of polygon edges, specified as a MATLAB Color Specification
(ColorSpec). A color specification
can be a character vector, cell array or character vectors, or a double array
with values in the range[0 1]. If a character vector, kmlwrite applies
the value to all faces. If a cell array of character vectors, specify
a value for each face. That is, the cell array must be the same length
as S or address. If the
value is a numeric array, it is size M-by-3 where M is
the length of S or address.
If S is a polygon geoshape, you may specify the
value 'none' to indicate that the polygon has no
outline.
'FaceAlpha' — Transparency of polygon faces1 (fully opaque) (default) | numeric scalar or vector in the range [0 1]Transparency of polygon faces, specified as a numeric scalar
or vector in the range [0 1].
If a scalar, kmlwrite applies the
value to all polygon faces.
If a vector, specify a value for each polygon face;
that is, the vector must be the same length as S or address.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32
'EdgeColor' — Color of polygon edges'none'Color of polygon edges, specified as a MATLAB Color Specification
(ColorSpec). A color specification
can be a character vector, a cell array of character vectors, or a double array
with values in the range [0 1].
If a scalar, kmlwrite applies the
value to all polygon edges.
If a vector, specify a value for each polygon edge;
that is, the vector must be the same length as S.
If you specify a double array,
the size must be M-by-3 where M is
the length of S.
The value 'none' indicates that polygons
have no edges.
'EdgeAlpha' — Transparency of the polygon edges1 (fully opaque) (default) | numeric scalar or vector in the range [0 1]Transparency of the polygon edges, specified as a numeric scalar or vector.
If a scalar, kmlwrite applies the
value to all polygon edges.
If a vector, y specify a value for each polygon edge;
that is, the vector must be the same length as S.
If you do not specify EdgeAlpha, kmlwrite uses
the value of Alpha, if specified. If you do not
specify either value, kmlwrite uses the default
value 1 (fully opaque).
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32
'Extrude' — Connect polygons to the groundfalse (default) | true | logical or numeric scalar | logical or numeric vectorConnect polygon to the ground, specified as a logical or numeric
scalar, true (1) or false (0),
or vector. If a scalar, the values applies to all polygons. If a vector,
specify a value for each polygon; that is, the vector must be the
same length as S or address.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
'CutPolygons' — Cut polygon partstrue (default) | falseCut polygon parts, specified as a logical or numeric scalar true (1)
or false (0). If true, kmlwrite cuts
polygon parts at the PolygonCutMeridian value.
If true, and the polygon parts require cutting, kmlwrite returns
an error if the altitude values are nonuniform.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | logical
'PolygonCutMeridian' — Meridian where polygon parts are cut180 (default) | scalar numericMeridian where polygon parts are cut, specified as a scalar numeric.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32
'AltitudeMode' — Interpretation of altitude values'clampToGround' | 'relativeToGround' | 'relativeToSeaLevel'Interpretation of altitude values, specified as one of the following values:
| Value | Description |
|---|---|
'clampToGround' | Ignore altitude values and set the feature on the ground. This is the default interpretation when you do not specify altitude values. |
'relativeToGround' | Set altitude values relative to the actual ground elevation of a particular feature |
'relativeToSeaLevel' | Set altitude values relative to sea level, regardless of the
actual elevation values of the terrain beneath the feature. (Named 'absolute' in
the KML specification.) This is the default interpretation when you
specify altitude values. |
Data Types: char | string
'LookAt' — Position of virtual camera (eye) relative to object being viewedPosition of the virtual camera (eye) relative to the object
being viewed, specified as a geopoint vector. The fields of the geopoint
vector, listed below, define the view. LookAt is
limited to looking down at a feature. To tilt the virtual camera to
look above the horizon into the sky, use the Camera parameter.
| Property Name | Description | Data Type |
|---|---|---|
Latitude | Latitude of the object the camera is looking at, in degrees | Scalar double, from -90 to 90 |
Longitude | Longitude of the object the camera is looking at, in degrees | Scalar double, from −180 to 180 |
Altitude | Altitude of the object the camera is looking at from the Earth's surface, in meters | Scalar numeric |
Heading | Camera direction (azimuth), in degrees (optional) | Scalar numeric [0 360], default 0 (true
North) |
Tilt | Angle between the direction of the LookAt position
and the normal to the surface of the Earth, in degrees (optional)
| Scalar numeric [0 90], default 0 (directly
above) |
Range | Distance in meters from the object specified by latitude, longitude,
and altitude to the location of the camera. | Scalar numeric, default 0 |
AltitudeMode | Interpretation of the camera altitude value (optional) | 'relativeToSeaLevel', 'clampToGround',
(default) 'relativeToGround' |
'Camera' — Position of virtual camera relative to surface of Earth Position of virtual camera (eye) relative to Earth's surface,
specified as a geopoint vector. The fields of the geopoint vector,
listed below, define the view. Camera provides
full six degrees of freedom control over the view, so you can position
the camera in space and then rotate it around the x-, y-,
and z-axes. You can tilt the camera view so that
you're looking above the horizon into the sky.
| Property Name | Description | Data Type |
|---|---|---|
Latitude | Latitude of the virtual camera (eye), in degrees | Scalar double, in the range [-90 90] |
Longitude | Longitude of the virtual camera, in degrees | Scalar double, in the range [-180 180]. |
Altitude | Distance of the virtual camera from the Earth's surface, in meters | Scalar numeric |
Heading | Direction (azimuth) in degrees (optional) | Scalar numeric [0 360], default 0 (true
North) |
Tilt | Camera rotation around the X-axis, in degrees (optional) | Scalar numeric [0 180], default 0 (directly
above) |
Roll | Camera rotation in degrees around the Z-axis (optional) | Scalar numeric, in the range [-180 180],
default 0 |
AltitudeMode | Specifies how camera altitude is interpreted (optional) | 'relativeToSeaLevel', 'clampToGround', 'relativeToGround' (default) |
You can view KML files with the Google Earth™ browser, which must be installed on your computer.
For Windows, use the winopen function:
winopen(filename)
For Linux, if the file name is a partial path, use the following commands:
cmd = 'googleearth ';
fullfilename = fullfile(pwd, filename);
system([cmd fullfilename])
For Mac, if the file name is a partial path, use the following commands:
cmd = 'open -a Google\ Earth '
fullfilename = fullfile(pwd, filename);
system([cmd fullfilename])
You can also view KML files with a Google Maps™ browser. The file must be located on a web server that is accessible from
the Internet. A private intranet server will not suffice because Google’s server must be
able to access the URL that you provide. The following is a template for using Google Maps. Replace your-web-server-path with a real value.
GMAPS_URL = 'http://maps.google.com/maps?q='; KML_URL = 'http://your-web-server-path'; web([GMAPS_URL KML_URL])
kmlwriteline | kmlwritepoint | kmlwritepolygon | makeattribspec | shapewrite
You have a modified version of this example. Do you want to open this example with your edits?