Hole within polygon in Mapbox iOS SDK


In Mapbox iOS SDK v3.3.1 it’s very easy to draw holes in simple polygons. Check out PR #5110 for technical detail.

Drawing

< PR #5110 includes support for holes in MGLPolygon >

Demo

Drawing
< Demo: a polygon with a hole >


Your geojson data will look like this:

{
      "type": "Feature",
      "properties": {
        ...
      },
      "geometry": {
        "coordinates": [ [ [Double] ] ]
      }
    }
  ]
}

It is not clear whether the winding order matters but your life will be easy if you follow the right hand rule(RHR). RHR means clockwise for exterior ring, counterclockwise for interior rings. With Swift read the geojson file like following:

let geoJSON = // load your geojson. I use SwiftyJSON
let features = geoJSON["features"]

func getPolygon(coordinates longitudeLatitude: [[Double]]) -> MGLPolygon {
    var coordinates: [CLLocationCoordinate2D] = longitudeLatitude.map{
        coord -> CLLocationCoordinate2D in
        return CLLocationCoordinate2D(latitude: coord[1], longitude: coord[0])
    }

    return MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
}

let polygons = features.map {
    (_, feature: JSON) -> Room? in
    let id: String = feature["properties"].string ?? ""
    let type = feature["properties", "type"].string ?? ""
    let name = feature["properties", "name"].string ?? ""
    if let listsOfPolygonsAndHoles = feature["geometry", "coordinates"].rawValue as? [[[Double]]] {
        let coordinatesFlattened = listsOfPolygonsAndHoles.flatMap { $0 } 
        return getPolygon(coordinates: coordinatesFlattened)
    } else {
        print("No coordinate")
        return nil
    }
}.flatMap { $0 }  // Remove nil and unwrap Optional

If you have any question, just email me!