fangpenlin / Proj4Swift

Proj.4 wrapper in Swift 2 language
MIT License
10 stars 8 forks source link

Proj4Swift

Build Status Carthage compatible GitHub license

Proj.4 wrapper in Swift 2 language

Install with Carthage

To install with Carthage, add Proj4Swift to your Cartfile:

github "fangpenlin/Proj4Swift"

Usage

Just create Projections with Proj.4 parameters, and create Point3Ds for the input points, then call Projection.transform on the source Projection with the Point3Ds and the destination Projection.

let projWGS84 = try! Projection(parameters: "+proj=longlat +ellps=WGS84 +no_defs")
let projMerc = try! Projection(parameters: "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs")

let points = [
    Point3D(
        x: Double(-122.389349) * Projection.degToRad,
        y: Double(37.778441) * Projection.degToRad,
        z: 0
    )
]
let resultPoints = try! projWGS84.transform(points, toProjection: projMerc)
print(resultPoints)

Remember the lat and long you passed in should be in radian instead of degree. To convert degree to radian, you can multiply Projection.degToRad.