UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Storing Polygons from geoJSON objects

Forums > SwiftUI

Hi!

I have a geoJSON decoder bit of code below. How can I save the MKMultiPolygons and render them into my MapKit View?

I am following this guide: https://jajackleon.medium.com/ios-macos-mapkit-swift-drawing-shapes-on-map-f8f4b4d314ee

But trying to follow allong I have issues when it gets to storing the multipolygons into a codable object....

extension Bundle {
    func decodegeoJSON(_ file: String) -> [MKGeoJSONObject] {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("Failed to locate \(file) in bundle.")
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.")
        }

        let decoder = MKGeoJSONDecoder()

        guard let loaded = try? decoder.decode(data) else {
            fatalError("Failed to decode \(file) from bundle.")
        }
        var geoJSONObjects = [MKGeoJSONObject]()
        geoJSONObjects = loaded

        //save the polygons in the mkgeojsonobject into the an array of polygoninfo class
        //THIS BIT OF CODE SHOULD CREATE A FOR LOOP THROUGH THE [MKGeoJSONObject] and save each multipolygon as a codable struct which will be rendered later in my mapview.

        return geoJSONObjects

    }
}

2      

I'm not sure what trouble you are having exactly, but the article you linked to demonstrates how to do this.

However, you have the decodegeoJSON(_:) function returning an array of MKGeoJSONObject items, which you've already got, so you shouldn't be transforming them into anything. Using the function you have posted, you would return [MKGeoJSONObject] to, say, a ViewModel object and then you would transform each MKGeoJSONObject into a Codable model object.

2      


let geoJSONObjects:[MKGeoJSONObject] = Bundle.main.decodegeoJSON("countrydiabetes.geojson")

Would I then create a for each loop? How would I assign each of these objects to my codable model object?

struct PolygonInfo: Codable {
    var FID: Int
    let latitude, longitude: Double
    let NAME_E: String

    enum CodingKeys: String, CodingKey {
        case FID = "FID"
        case NAME_E = "NAME_E"
        case latitude = "latitude"
        case longitude = "longitude"
    }
}

2      

GeoJSON. GeoJSON is a format for encoding a variety of geographic data structures. GeoJSON supports the following geometry types: Point , LineString , Polygon , MultiPoint , MultiLineString , and MultiPolygon . So more details to contact on my site Loulouka Formula

2      

Would I then create a for each loop? How would I assign each of these objects to my codable model object?

Yes, you would need a for each or a map to loop through the MKGeoJSONObjects returned by your decode function and transform them into your PolygonInfo structs.

This is how the author of that article did it. You would try something similar:

for item in geoJson {
    if let feature = item as? MKGeoJSONFeature {
        let geometry = feature.geometry.first
        let propData = feature.properties!

        if let polygon = geometry as? MKPolygon {
            let polygonInfo = try? JSONDecoder.init().decode(PolygonInfo.self, from: propData)   
        }
    }
}

If you could post a sample of your GeoJSON, I or someone else could maybe work up an example of how to do it.

2      

So!

Update: @roosterboy

I just ran the code that was in the article and run into an error here:

 for item in geoJson {
            if let feature = item as? MKGeoJSONFeature {
                let geometry = feature.geometry.first
                let propData = feature.properties!

                if let polygon = geometry as? MKPolygon {
                    let polygonInfo = try? JSONDecoder.init().decode(PolygonInfo.self, from: propData)
                    //call render function to render our polygon shape
                    self.render(overlay: polygon, info: polygonInfo) <-------- cannot find 'self' in scope
                }

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.