Permalink
Browse files

added method for finding flattened hierarchy of Placemarks in Containers

 * parse all Placemarks via XPath
 * make available via Container property
 * removed Feature parent Container
  * presents parsing recursion problems
  * not sure why we added it in the first place
  • Loading branch information...
1 parent a40de9b commit c10b4bfd23aa4f166983f7673112147f0a7fd0d3 @incanus incanus committed Feb 2, 2011
Showing with 42 additions and 9 deletions.
  1. +4 −2 SimpleKMLContainer.h
  2. +38 −3 SimpleKMLContainer.m
  3. +0 −3 SimpleKMLFeature.h
  4. +0 −1 SimpleKMLFeature.m
View
@@ -38,9 +38,11 @@
@interface SimpleKMLContainer : SimpleKMLFeature
{
- NSArray *features;
+ NSArray *features; // top-level <Feature> elements
+ NSArray *flattenedPlacemarks; // <Placemark> elements at all depths, regardless of hierarchy
}
-@property (nonatomic, retain) NSArray *features;
+@property (nonatomic, retain, readonly) NSArray *features;
+@property (nonatomic, retain, readonly) NSArray *flattenedPlacemarks;
@end
View
@@ -35,44 +35,78 @@
#import "SimpleKMLContainer.h"
#import "SimpleKMLFeature.h"
#import "SimpleKMLDocument.h"
+#import "SimpleKMLPlacemark.h"
@implementation SimpleKMLContainer
@synthesize features;
+@synthesize flattenedPlacemarks;
- (id)initWithXMLNode:(CXMLNode *)node sourceURL:sourceURL error:(NSError **)error
{
self = [super initWithXMLNode:node sourceURL:sourceURL error:error];
if (self != nil)
{
+ NSError *parseError;
+
+ // find child features
+ //
NSMutableArray *featuresArray = [NSMutableArray array];
+
+ NSMutableDictionary *alreadyParsedFeatures = [NSMutableDictionary dictionary];
for (CXMLNode *child in [node children])
{
Class featureClass = NSClassFromString([NSString stringWithFormat:@"SimpleKML%@", [child name]]);
if (featureClass)
{
- NSError *parseError = nil;
+ parseError = nil;
id feature = [[[featureClass alloc] initWithXMLNode:child sourceURL:sourceURL error:&parseError] autorelease];
// only add the feature if it's one we know how to handle
//
if ( ! parseError && [feature isKindOfClass:[SimpleKMLFeature class]])
{
- ((SimpleKMLFeature *)feature).container = self;
-
if ([self isMemberOfClass:[SimpleKMLDocument class]])
((SimpleKMLFeature *)feature).document = (SimpleKMLDocument *)self;
[featuresArray addObject:feature];
+
+ if ([feature isKindOfClass:[SimpleKMLPlacemark class]])
+ [alreadyParsedFeatures setObject:feature forKey:child];
}
}
}
features = [[NSArray arrayWithArray:featuresArray] retain];
+
+ // find all Placemark features, regardless of hierarchy
+ //
+ NSMutableArray *flattenedPlacemarksArray = [NSMutableArray array];
+
+ for (CXMLNode *featureNode in [node nodesForXPath:@"//kml:Placemark"
+ namespaceMappings:[NSDictionary dictionaryWithObject:@"http://earth.google.com/kml/2.0" forKey:@"kml"]
+ error:NULL])
+ {
+
+ if ([alreadyParsedFeatures objectForKey:featureNode])
+ [flattenedPlacemarksArray addObject:[alreadyParsedFeatures objectForKey:featureNode]];
+
+ else
+ {
+ parseError = nil;
+
+ SimpleKMLPlacemark *placemark = [[[SimpleKMLPlacemark alloc] initWithXMLNode:featureNode sourceURL:sourceURL error:&parseError] autorelease];
+
+ if ( ! parseError)
+ [flattenedPlacemarksArray addObject:placemark];
+ }
+ }
+
+ flattenedPlacemarks = [[NSArray arrayWithArray:flattenedPlacemarksArray] retain];
}
return self;
@@ -81,6 +115,7 @@ - (id)initWithXMLNode:(CXMLNode *)node sourceURL:sourceURL error:(NSError **)err
- (void)dealloc
{
[features release];
+ [flattenedPlacemarks release];
[super dealloc];
}
View
@@ -37,7 +37,6 @@
#import "SimpleKMLObject.h"
@class SimpleKMLStyle;
-@class SimpleKMLContainer;
@class SimpleKMLDocument;
@interface SimpleKMLFeature : SimpleKMLObject
@@ -48,7 +47,6 @@
SimpleKMLStyle *sharedStyle;
SimpleKMLStyle *inlineStyle;
SimpleKMLStyle *style;
- SimpleKMLContainer *container;
SimpleKMLDocument *document;
}
@@ -58,7 +56,6 @@
@property (nonatomic, retain, readonly) SimpleKMLStyle *inlineStyle;
@property (nonatomic, retain, readonly) SimpleKMLStyle *style;
@property (nonatomic, assign) SimpleKMLStyle *sharedStyle;
-@property (nonatomic, assign) SimpleKMLContainer *container;
@property (nonatomic, assign) SimpleKMLDocument *document;
@end
View
@@ -44,7 +44,6 @@ @implementation SimpleKMLFeature
@synthesize sharedStyle;
@synthesize inlineStyle;
@synthesize style;
-@synthesize container;
@synthesize document;
- (id)initWithXMLNode:(CXMLNode *)node sourceURL:sourceURL error:(NSError **)error

0 comments on commit c10b4bf

Please sign in to comment.