-
Notifications
You must be signed in to change notification settings - Fork 10
/
OSMParserHandlerDefault.m
126 lines (107 loc) · 2.76 KB
/
OSMParserHandlerDefault.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
//
// OSMParserHandlerDefault.m
// OSMImporter
//
// Created by y0n3l http://www.twitter.com/y0n3l on 1/17/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "OSMParserHandlerDefault.h"
@interface OSMParserHandlerDefault (privateAPI)
-(BOOL) checkForWaysFlush;
-(BOOL) checkForNodesFlush;
@end
@implementation OSMParserHandlerDefault
@synthesize ignoreNodes;
@synthesize bufferMaxSize;
@synthesize optimizeOnFinished;
-(id) initWithOutputFilePath:(NSString*)output {
return [self initWithOutputFilePath:output overrideIfExists:YES];
}
-(id) initWithOutputFilePath:(NSString*)filePath overrideIfExists:(BOOL)override {
if (self!=[super init])
return nil;
outputDao=[[OSMDAO alloc] initWithFilePath:filePath overrideIfExists:override];
nodesBuffer=[[NSMutableArray alloc] init];
waysBuffer=[[NSMutableArray alloc] init];
bufferMaxSize=30000;
optimizeOnFinished = YES;
return self;
}
-(void) dealloc {
[outputDao release];
[super dealloc];
}
#pragma mark -
#pragma mark Parser delegate
-(void) didStartParsingNodes {
NSLog(@"[NOW PARSING NODES]");
}
-(void) didStartParsingWays {
[self checkForNodesFlush];
NSLog(@"[NOW PARSING WAYS]");
}
-(void) didStartParsingRelations {
[self checkForWaysFlush];
NSLog(@"[NOW PARSING RELATIONS]");
}
-(void) onNodeFound:(Node *)node {
if (!ignoreNodes) {
//[roadNetwork addNodes:[NSArray arrayWithObject:node]];
[nodesBuffer addObject:node];
//if (node.tags)
// NSLog(@"this node has tags : %@", node.tags);
nodesCounter++;
if (nodesCounter%bufferMaxSize==0) {
[self checkForNodesFlush];
}
}
}
-(void) onWayFound:(Way *)way {
[waysBuffer addObject:way];
if ([way.nodesIds count]==0)
NSLog(@"WARNING No Node for WAY %@i", way.wayId);
//NSLog(@"Way %i has nodes : %@", way.wayId, way.nodesIds);
waysCounter++;
if (waysCounter%(bufferMaxSize/20)==0) {
[self checkForWaysFlush];
}
}
-(void) onRelationFound:(Relation *)relation {
//NSLog(@"relation found");
[outputDao addRelation:relation];
}
-(BOOL) checkForNodesFlush {
if ([nodesBuffer count]!=0) {
NSLog(@"parsed %i nodes", nodesCounter);
[outputDao addNodes:nodesBuffer];
[nodesBuffer removeAllObjects];
return YES;
} else {
return NO;
}
}
-(BOOL) checkForWaysFlush {
if ([waysBuffer count]!=0) {
NSLog(@"parsed %i ways", waysCounter);
NSLog(@"now populating corresponding nodes");
for (int i=0; i<[waysBuffer count]; i++) {
Way* w =[waysBuffer objectAtIndex:i];
NSArray* n =[outputDao getNodesForWay:w];
w.nodes=n;
}
NSLog(@"Nodes populated, now flushing...");
[outputDao addWays:waysBuffer];
NSLog(@"Flush !");
[waysBuffer removeAllObjects];
return YES;
}else {
return NO;
}
}
-(void) parsingStart {
}
-(void) parsingEnd {
if (optimizeOnFinished)
[outputDao optimizeDB];
}
@end