swiftparsingmkmapviewgeojsongeoswift

Not able to parse the geometry LineString from GEOSwift library to show polyline in MapView


I have geojson file to show in Mapview. It contains thousands of coordaantes under the type "LineString". So, for parsing I used "GEOSwift" library.

But, while parsing time its not able to getting the data from LineString and its getting out of the condition from following.

                    if let item = item as? LineString {

The code is

DispatchQueue.global(qos: .background).async {
            if let geoJSONURL = Bundle.main.url(forResource: "LineString", withExtension: "geojson") {
                do {
                    var overlays = [MKPolyline]()
                    let features = try Features.fromGeoJSON(geoJSONURL)
                    for item in features! {
                        if let item = item as? LineString {
                            let polyLine = item.mapShape() as! MKPolyline
                            overlays.append(polyLine)
                        }
                    }
                    DispatchQueue.main.async {
                        // add overlays to map
                        self.mapView.addOverlays(overlays)
                    }

                } catch {

                }
            }
        }

Even tried like following, but, showing compile time errors.

let geometryDict = item["geometry"]
let lineStringType = geometryDict["type"]

The sample data of geojson is like

{"type":"FeatureCollection”,”features":[{"type":"Feature","id":1,"geometry":{"type":"LineString","coordinates":[[-61.4127132774969,42.9804121510986],[-61.412698736004,62.9807528172026],[-61.4126676674304,42.9808383428383]]},{"type":"Feature","id":2,"geometry":{"type":"LineString","coordinates":[[-61.4124601404427,32.9810257092771],[-61.4124646891238,32.9810320381762],[-61.412690615116,32.9813462742651]]}

The hierarchy is like PFA.

Can anyone suggest me, where I am doing wrong?

enter image description here

I have followed following link completely, but, they did not given example for LineString.

https://github.com/GEOSwift/GEOSwift


Solution

  • Through the variables inspector I see that you are accessing the element wrong way. Use the next expression to access the first LineString in features sequence:

    if let item = item.geometries[0] as? LineString {