Coordinates (struct KMLCoordinate and struct KMLCoordinateSequence)
Not all types are supported with all options available to KML files. I’ve focused on types and features that can be translated into MapKit for now.
KMLDocument
The root of a KML file is represented by the KMLDocument struct, which is used as a container for any number of Features, and any top-level global Styles.
When creating a KMLDocument from scratch (rather than reading from an existing file), you may optionally add a name and description to the document, then add an array of included features and a dictionary of global styles.
public struct KMLDocument {
public var name: String?
public var featureDescription: String?
public var features: [KMLFeature]
public var styles: [KMLStyleUrl: KMLStyleSelector]
}
Reading KML Files
let fileUrl = ...
let fileData = try Data(contentsOf: fileUrl)
let kmlString = try? String(contentsOf: fileUrl, encoding: .utf8)
let kmzFileUrl = ...
let kmzFileData = try Data(contentsOf: kmzFileUrl)
let documentFromData = try? KMLDocument(fileData)
let documentFromFileUrl = try? KMLDocument(fileUrl)
let documentFromString = try? KMLDocument(kmlString)
let documentFromKmzFile = try? KMLDocument(kmzFileUrl) //init(_ url:) works with either KML or KMZ files.
let documentFromKmzData = try? KMLDocument(kmzData: kmzFileData)
Writing KML Files
let kmlDoc = KMLDocument(...)
let asData = kmlDoc.kmlData()
let asString = kmlDoc.kmlString()
let asKmzData = kmlDoc.kmzData()
Further To-Do’s
Documentation: How to add further KML type support
RCKML
A library for reading and writing KML files in Swift, designed for simplicity and ease of use.
Index:
Installation
Swift Package Manager:
Supported KML Types
protocol KMLFeature
)protocol KMLContainer
)struct KMLDocument: KMLContainer
)struct KMLFolder: KMLContainer, KMLFeature
)struct KMLPlacemark: KMLFeature
)protocol KMLGeometry
)struct KMLPoint: KMLGeometry
)struct KMLLineString: KMLGeometry
)struct KMLPolygon: KMLGeometry
)struct KMLMultiGeometry: KMLGeometry
)protocol KMLStyleSelector
)struct KMLStyleUrl: KMLStyleSelector
)struct KMLStyle: KMLStyleSelector
)struct KMLStyleMap: KMLStyleSelector
)protocol KMLColorStyle
)struct KMLLineStyle: KMLColorStyle
)struct KMLPolyStyle: KMLColorStyle
)struct KMLPolygon.LinearRing
)struct KMLColor
)struct KMLCoordinate
andstruct KMLCoordinateSequence
)KMLDocument
The root of a KML file is represented by the
KMLDocument
struct, which is used as a container for any number of Features, and any top-level global Styles.When creating a KMLDocument from scratch (rather than reading from an existing file), you may optionally add a name and description to the document, then add an array of included features and a dictionary of global styles.
Reading KML Files
Writing KML Files
Further To-Do’s
Dependencies