From anyObject to array of coordinates

58 Views Asked by At

New to Swift and json

I want to extract lng,lat from anyObject elements. Here a part of my code to extract from json file:

for (key,value) in geoFeature! as [String: AnyObject]  {print("key=",key, ptvir, value)}
let geoCoords = geoFeature?["coordinates"] as AnyObject

Resulting to :

Printing description of geoCoords:
▿ 1 element
  ▿ 0 : 11 elements
    ▿ 0 : 2 elements
      - 0 : -73.596408
      - 1 : 45.453657
    ▿ 1 : 2 elements
      - 0 : -73.595466
      - 1 : 45.451156
    ▿ 2 : 2 elements
      - 0 : -73.59532
      - 1 : 45.450786
    ▿ 3 : 2 elements
      - 0 : -73.596114
      - 1 : 45.450639
    ▿ 4 : 2 elements
      - 0 : -73.596616
      - 1 : 45.450549
    ▿ 5 : 2 elements
      - 0 : -73.596746
      - 1 : 45.450911
    ▿ 6 : 2 elements
      - 0 : -73.596867
      - 1 : 45.451248
    ▿ 7 : 2 elements
      - 0 : -73.59716
      - 1 : 45.452082
    ▿ 8 : 2 elements
      - 0 : -73.597514
      - 1 : 45.45307
    ▿ 9 : 2 elements
      - 0 : -73.597638
      - 1 : 45.453437
    ▿ 10 : 2 elements
      - 0 : -73.596408
      - 1 : 45.453657

From here, I did not find the code to create an array of coordinates

2

There are 2 best solutions below

0
F. Charbonneau On

More details:

From a json file like this part:

{ "type": "FeatureCollection", "name": "MtlAires2016Bref", "features": [ { "type": "Feature", "properties": { "ADIDU": "24661006" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.596408, 45.453657 ], [ -73.595466, 45.451156 ], [ -73.59532, 45.450786 ], [ -73.596114, 45.450639 ], [ -73.596616, 45.450549 ], [ -73.596746, 45.450911 ], [ -73.596867, 45.451248 ], [ -73.59716, 45.452082 ], [ -73.597514, 45.45307 ], [ -73.597638, 45.453437 ], [ -73.596408, 45.453657 ] ] ] } } ] }

My code extract properties for each Feature

The problem is to extract all the polygon coordinates with the result of these 2 lines of code:

for (key,value) in geoFeature! as [String: AnyObject] {print("key=",key, ";", value)} let geoCoords = geoFeature?["coordinates"] as AnyObject print("geoCoords=", geoCoords as Any)

result of this print:

geoCoords= ( ( ( "-73.596408", "45.453657" ), ( "-73.595466", "45.451156" ), ( "-73.59532", "45.450786" ), ( "-73.596114", "45.450639" ), ( "-73.596616", "45.450549" ), ( "-73.596746", "45.450911" ), ( "-73.596867", "45.451248" ), ( "-73.59716", "45.452082" ), ( "-73.597514", "45.45307" ), ( "-73.597638", "45.453437" ), ( "-73.596408", "45.453657" ) ) )

I did not find how to iterate in geoCoords and append coordinates to an array.

Next line give me the same listing; not array

for element in geoCoords as! Array { //print("elem=",element)}

but if I ask for the type of the variable like this

String(describing: type(of: coordsData))

it's = Array

the next line give an empty array

let tblC = coordsData as [CLLocationCoordinate2D]

1
vadian On

Please learn to read JSON. It's pretty easy. There are only two collection types, array ([]) and dictionary ({}). So the value for key coordinates is a nested array of Double ([[[Double]]]).

It's impossible to cast a double array to CLLocationCoordinate2D because the type is not related.

And the unspecified JSON type is Any, never AnyObject

if let geoCoords = geoFeature["coordinates"] as? [[[Double]]] {
    for outerCoords in geoCoords {
       for innerCoords in outerCoords {
           print(innerCoords)
       }
    }
}