A library for reading and writing KML files in Swift, designed for simplicity and ease of use.
- Installation
- Supported KML Types
- KMLDocument
- Reading KML Files
- Writing KML Files
- Further To-Do's
- Dependencies
Swift Package Manager:
.package(url: "https://github.com/RCCoop/RCKML.git", .upToNextMajor(from: "1.0.0"))
- Feature (
protocol KMLFeature
)- Container (
protocol KMLContainer
)- Document (
struct KMLDocument: KMLContainer
) - Folder (
struct KMLFolder: KMLContainer, KMLFeature
)
- Document (
- Placemark (
struct KMLPlacemark: KMLFeature
)
- Container (
- Geometry (
protocol KMLGeometry
)- Point (
struct KMLPoint: KMLGeometry
) - LineString (
struct KMLLineString: KMLGeometry
) - Polygon (
struct KMLPolygon: KMLGeometry
) - Multigeometry (
struct KMLMultiGeometry: KMLGeometry
)
- Point (
- StyleSelector (
protocol KMLStyleSelector
)- Style URL (
struct KMLStyleUrl: KMLStyleSelector
) - Style (
struct KMLStyle: KMLStyleSelector
) - StyleMap (
struct KMLStyleMap: KMLStyleSelector
)
- Style URL (
- ColorStyle (
protocol KMLColorStyle
)- LineStyle (
struct KMLLineStyle: KMLColorStyle
) - PolyStyle (
struct KMLPolyStyle: KMLColorStyle
)
- LineStyle (
- Sub-formats
- LinearRing (
struct KMLPolygon.LinearRing
) - KML Color (
struct KMLColor
) - Coordinates (
struct KMLCoordinate
andstruct KMLCoordinateSequence
)
- LinearRing (
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.
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]
}
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)
let kmlDoc = KMLDocument(...)
let asData = kmlDoc.kmlData()
let asString = kmlDoc.kmlString()
let asKmzData = kmlDoc.kmzData()
- Documentation: How to add further KML type support
- AEXML for reading and writing XML files
- ZipFoundation for dealing with compression for KMZ data.