-
Notifications
You must be signed in to change notification settings - Fork 23
/
BBGeoJSONParser.m
213 lines (187 loc) · 5.02 KB
/
BBGeoJSONParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* benCoding.Map
* Copyright (c) 2009-2012 by Ben Bahrenburg. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "BBGeoJSONParser.h"
@implementation BBGeoJSONParser
@synthesize Code,Name,Polygons;
- (id) initWithFilePath:(NSString*)filePath
{
if (self = [super init])
{
jsonFilePath=filePath;
}
return self;
}
-(void)dealloc
{
if(Polygons!=nil)
{
RELEASE_TO_NIL(Polygons);
}
if(Name!=nil)
{
RELEASE_TO_NIL(Name);
}
if(Code!=nil)
{
RELEASE_TO_NIL(Code);
}
[super dealloc];
}
-(void)addPoint:(NSMutableArray*)points
{
NSUInteger pointsCount = [points count];
if(pointsCount==0)
{
return;
}
//Create the number of points provided
CLLocationCoordinate2D pointsToAdd[pointsCount];
for (int iLoop = 0; iLoop < pointsCount; iLoop++)
{
pointsToAdd[iLoop]= CLLocationCoordinate2DMake([[points objectAtIndex:iLoop] coordinate].latitude,
[[points objectAtIndex:iLoop] coordinate].longitude);
}
MKPolygon* polygonToAdd = [MKPolygon polygonWithCoordinates:pointsToAdd count:pointsCount];
// NSLog(@"Adding polygon with points %u",[points count]);
if(jsonPolygons==nil)
{
jsonPolygons = [[NSMutableArray alloc] init];
}
[jsonPolygons addObject:polygonToAdd];
}
-(BOOL)hasNoChild:(NSArray*)coord
{
NSString * coordType = NSStringFromClass ([coord class]);
if ([coordType rangeOfString:@"array" options:NSCaseInsensitiveSearch].location == NSNotFound)
{
return YES;
}
NSString *childCheck = NSStringFromClass ([[coord objectAtIndex:0] class]);
if ([childCheck rangeOfString:@"array" options:NSCaseInsensitiveSearch].location == NSNotFound)
{
return YES;
}
else
{
return NO;
}
}
-(void)parseCoords:(NSArray*)coordinates
{
NSUInteger nodeCount = [coordinates count];
NSMutableArray *points =[[[NSMutableArray alloc] init] autorelease];
for (int iLoop = 0; iLoop < nodeCount; iLoop++)
{
if([self hasNoChild:[coordinates objectAtIndex:iLoop]]==YES)
{
CLLocation *point = [[[CLLocation alloc] initWithLatitude:[[[coordinates objectAtIndex:iLoop] objectAtIndex:1] doubleValue]
longitude:[[[coordinates objectAtIndex:iLoop] objectAtIndex:0]doubleValue]] autorelease];
[points addObject:point];
}
else
{
[self parseCoords:[coordinates objectAtIndex:iLoop]];
}
}
if([points count]>0)
{
[self addPoint:points];
}
}
-(NSString*) findName:(NSDictionary*)json
{
NSDictionary * properties = [json objectForKey:@"properties"];
NSString * name = [properties objectForKey:@"name"];
return name;
}
-(NSString*) findCode:(NSDictionary*)json
{
NSString * code = [json objectForKey:@"id"];
return code;
}
-(BOOL) validRoot:(NSArray *) root
{
if(root == nil)
{
return NO;
}
if([root count]==0)
{
return NO;
}
else
{
return YES;
}
}
- (NSString *)Name
{
return jsonName;
}
- (NSString *)Code
{
return jsonCode;
}
- (NSArray *)Polygons
{
return jsonPolygons;
}
-(void) Parse
{
@try {
if(jsonPolygons!=nil)
{
RELEASE_TO_NIL(jsonPolygons);
}
if(jsonName!=nil)
{
RELEASE_TO_NIL(jsonName);
}
if(jsonCode!=nil)
{
RELEASE_TO_NIL(jsonCode);
}
if(Polygons!=nil)
{
RELEASE_TO_NIL(Polygons);
}
if(Name!=nil)
{
RELEASE_TO_NIL(Name);
}
if(Code!=nil)
{
RELEASE_TO_NIL(Code);
}
NSData* jsonData = [NSData dataWithContentsOfFile: jsonFilePath];
JSONDecoder* decoder = [[[JSONDecoder alloc]
initWithParseOptions:JKParseOptionLooseUnicode] autorelease];
NSDictionary* json = [decoder objectWithData:jsonData];
if(json==nil)
{
NSLog(@"Invalid JSON format, unable to continue");
return;
}
NSArray * root = [json objectForKey:@"features"];
if([self validRoot:root]==NO)
{
NSLog(@"Invalid Geo JSON provided, unable to continue");
return;
}
//Parse the Name and Code Properties
jsonName = [self findName:[root objectAtIndex:0]];
jsonCode = [self findCode:[root objectAtIndex:0]];
//Find the Coordinates Node
NSArray * coordinates = [[[root objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"coordinates"];
//Parse the coordinates into polygons
[self parseCoords:coordinates];
}
@catch (id theException) {
NSLog(@"Parser Error %@", theException);
}
}
@end