RCKML

1.0.2

A library for reading and writing KML files in Swift
kml
RCCoop/RCKML

What's New

Version 1.0.2

2023-02-15T22:59:04Z

Fixed compression for KMZ files.

RCKML

GitHub

A library for reading and writing KML files in Swift, designed for simplicity and ease of use.

Index:

Installation

Swift Package Manager:

.package(url: "https://github.com/RCCoop/RCKML.git", .upToNextMajor(from: "1.0.0"))

Supported KML Types

  • Feature (protocol KMLFeature)
    • Container (protocol KMLContainer)
      • Document (struct KMLDocument: KMLContainer)
      • Folder (struct KMLFolder: KMLContainer, KMLFeature)
    • Placemark (struct KMLPlacemark: KMLFeature)
  • Geometry (protocol KMLGeometry)
    • Point (struct KMLPoint: KMLGeometry)
    • LineString (struct KMLLineString: KMLGeometry)
    • Polygon (struct KMLPolygon: KMLGeometry)
    • Multigeometry (struct KMLMultiGeometry: KMLGeometry)
  • StyleSelector (protocol KMLStyleSelector)
    • Style URL (struct KMLStyleUrl: KMLStyleSelector)
    • Style (struct KMLStyle: KMLStyleSelector)
    • StyleMap (struct KMLStyleMap: KMLStyleSelector)
  • ColorStyle (protocol KMLColorStyle)
    • LineStyle (struct KMLLineStyle: KMLColorStyle)
    • PolyStyle (struct KMLPolyStyle: KMLColorStyle)
  • Sub-formats
    • LinearRing (struct KMLPolygon.LinearRing)
    • KML Color (struct KMLColor)
    • 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

Dependencies

  • AEXML for reading and writing XML files
  • ZipFoundation for dealing with compression for KMZ data.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

Last updated: Sat Mar 16 2024 06:41:52 GMT-0900 (Hawaii-Aleutian Daylight Time)