<commit_before>//
//  HumboldMapKit.swift
//
//  Created by <NAME> on 26/05/15.
//  Copyright (c) 2015 andreacremaschi. All rights reserved.
//

import Foundation
import MapKit

/**
A convenience method to convert coordinates in the CoreLocation format.

:param: coord the `Coordinate` object

:returns: A CLLocationCoordinate2D
*/
public func CLLocationCoordinateFromCoordinate(coord: Coordinate) -> CLLocationCoordinate2D {
    let coord = CLLocationCoordinate2DMake(coord.y, coord.x)
    return coord
}

// MARK: - MKShape creation convenience function

public protocol GEOSwiftMapKit {
    /**
    A convenience method to create a `MKShape` ready to be added to a `MKMapView`.
    
    :returns: A MKShape representing this geometry.
    */
    func mapShape() -> MKShape
}

// Declarations in extensions cannot override yet (in Swift 2.0)!
// Solve conformance to protocol with an inelegant (but effective) switch case.

extension Geometry : GEOSwiftMapKit {
    public func mapShape() -> MKShape {
        
        switch self {
            
        case is Waypoint:
            let pointAnno = MKPointAnnotation()
            pointAnno.coordinate = CLLocationCoordinateFromCoordinate((self as! Waypoint).coordinate)
            return pointAnno
            
        case is LineString:
            var coordinates = (self as! LineString).points.map({ (point: Coordinate) ->
                CLLocationCoordinate2D in
                return CLLocationCoordinateFromCoordinate(point)
            })
            let polyline = MKPolyline(coordinates: &coordinates,
                count: coordinates.count)
            return polyline
            
        case is Polygon:
            var exteriorRingCoordinates = (self as! Polygon).exteriorRing.points.map({ (point: Coordinate) ->
                CLLocationCoordinate2D in
                return CLLocationCoordinateFromCoordinate(point)
            })
            
            let interiorRings = (self as! Polygon).interiorRings.map({ (linearRing: LinearRing) ->
                MKPolygon in
                return MKPolygonWithCoordinatesSequence(linearRing.points)
            })
            
            let polygon = MKPolygon(coordinates: &exteriorRingCoordinates, count: exteriorRingCoordinates.count, interiorPolygons: interiorRings)
            return polygon
            
        default:
            let geometryCollectionOverlay = MKShapesCollection(geometryCollection: (self as! GeometryCollection))
            return geometryCollectionOverlay
        }
    }
}

private func MKPolygonWithCoordinatesSequence(coordinates: CoordinatesCollection) -> MKPolygon {
    var coordinates = coordinates.map({ (point: Coordinate) ->
        CLLocationCoordinate2D in
        return CLLocationCoordinateFromCoordinate(point)
    })
    return MKPolygon(coordinates: &coordinates,
        count: coordinates.count)
    
}

/** 
MKShape subclass for GeometryCollections.
The property `shapes` contains MKShape subclasses instances. When drawing shapes on a map be careful to the fact that that these shapes could be overlays OR annotations.
*/<commit_msg>Cast generic collection types via pattern matching<commit_after>//
//  HumboldMapKit.swift
//
//  Created by <NAME> on 26/05/15.
//  Copyright (c) 2015 andreacremaschi. All rights reserved.
//

import Foundation
import MapKit

/**
A convenience method to convert coordinates in the CoreLocation format.

:param: coord the `Coordinate` object

:returns: A CLLocationCoordinate2D
*/
public func CLLocationCoordinateFromCoordinate(coord: Coordinate) -> CLLocationCoordinate2D {
    let coord = CLLocationCoordinate2DMake(coord.y, coord.x)
    return coord
}

// MARK: - MKShape creation convenience function

public protocol GEOSwiftMapKit {
    /**
    A convenience method to create a `MKShape` ready to be added to a `MKMapView`.
    
    :returns: A MKShape representing this geometry.
    */
    func mapShape() -> MKShape
}

// Declarations in extensions cannot override yet (in Swift 2.0)!
// Solve conformance to protocol with an inelegant (but effective) switch case.

extension Geometry : GEOSwiftMapKit {
    public func mapShape() -> MKShape {
        
        switch self {
            
        case is Waypoint:
            let pointAnno = MKPointAnnotation()
            pointAnno.coordinate = CLLocationCoordinateFromCoordinate((self as! Waypoint).coordinate)
            return pointAnno
            
        case is LineString:
            var coordinates = (self as! LineString).points.map({ (point: Coordinate) ->
                CLLocationCoordinate2D in
                return CLLocationCoordinateFromCoordinate(point)
            })
            let polyline = MKPolyline(coordinates: &coordinates,
                count: coordinates.count)
            return polyline
            
        case is Polygon:
            var exteriorRingCoordinates = (self as! Polygon).exteriorRing.points.map({ (point: Coordinate) ->
                CLLocationCoordinate2D in
                return CLLocationCoordinateFromCoordinate(point)
            })
            
            let interiorRings = (self as! Polygon).interiorRings.map({ (linearRing: LinearRing) ->
                MKPolygon in
                return MKPolygonWithCoordinatesSequence(linearRing.points)
            })
            
            let polygon = MKPolygon(coordinates: &exteriorRingCoordinates, count: exteriorRingCoordinates.count, interiorPolygons: interiorRings)
            return polygon
            
        case let gc as GeometryCollection<Waypoint>:
            return MKShapesCollection(geometryCollection: gc)

        case let gc as GeometryCollection<LineString>:
            return MKShapesCollection(geometryCollection: gc)

        case let gc as GeometryCollection<Polygon>:
            return MKShapesCollection(geometryCollection: gc)

        default:
            let geometryCollectionOverlay = MKShapesCollection(geometryCollection: (self as! GeometryCollection))
            return geometryCollectionOverlay
        }
    }
}

private func MKPolygonWithCoordinatesSequence(coordinates: CoordinatesCollection) -> MKPolygon {
    var coordinates = coordinates.map({ (point: Coordinate) ->
        CLLocationCoordinate2D in
        return CLLocationCoordinateFromCoordinate(point)
    })
    return MKPolygon(coordinates: &coordinates,
        count: coordinates.count)
    
}

/** 
MKShape subclass for GeometryCollections.
The property `shapes` contains MKShape subclasses instances. When drawing shapes on a map be careful to the fact that that these shapes could be overlays OR annotations.
*/
{'fraction_non_alphanumeric': 0.07078039927404718, 'fraction_numerical': 0.0054446460980036296, 'mean_word_length': 2.6057797164667393, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1509506', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1683, 'n_words': 502}