Permalink
Browse files

added basic support for GroundOverlay

  • Loading branch information...
incanus committed Jul 22, 2011
1 parent 90acbf4 commit 2696510e181330ff3826b7f1f5175295093983c2
Showing with 388 additions and 0 deletions.
  1. +56 −0 SimpleKMLGroundOverlay.h
  2. +131 −0 SimpleKMLGroundOverlay.m
  3. +50 −0 SimpleKMLOverlay.h
  4. +151 −0 SimpleKMLOverlay.m
View
@@ -0,0 +1,56 @@
+//
+// SimpleKMLGroundOverlay.h
+//
+// Created by Justin R. Miller on 7/22/11
+// Copyright 2011, Development Seed, Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// * Neither the names of Code Sorcery Workshop, LLC or Development Seed,
+// Inc., nor the names of its contributors may be used to endorse or
+// promote products derived from this software without specific prior
+// written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// http://code.google.com/apis/kml/documentation/kmlreference.html#groundoverlay
+//
+
+#import "SimpleKMLOverlay.h"
+
+#import <CoreLocation/CoreLocation.h>
+
+@interface SimpleKMLGroundOverlay : SimpleKMLOverlay
+{
+ CLLocationDegrees north;
+ CLLocationDegrees south;
+ CLLocationDegrees east;
+ CLLocationDegrees west;
+ CGFloat rotation;
+}
+
+@property (nonatomic, assign, readonly) CLLocationDegrees north;
+@property (nonatomic, assign, readonly) CLLocationDegrees south;
+@property (nonatomic, assign, readonly) CLLocationDegrees east;
+@property (nonatomic, assign, readonly) CLLocationDegrees west;
+@property (nonatomic, assign, readonly) CGFloat rotation;
+
+@end
View
@@ -0,0 +1,131 @@
+//
+// SimpleKMLGroundOverlay.m
+//
+// Created by Justin R. Miller on 7/22/11
+// Copyright 2011, Development Seed, Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// * Neither the names of Code Sorcery Workshop, LLC or Development Seed,
+// Inc., nor the names of its contributors may be used to endorse or
+// promote products derived from this software without specific prior
+// written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "SimpleKMLGroundOverlay.h"
+
+@implementation SimpleKMLGroundOverlay
+
+@synthesize north;
+@synthesize south;
+@synthesize east;
+@synthesize west;
+@synthesize rotation;
+
+- (id)initWithXMLNode:(CXMLNode *)node sourceURL:sourceURL error:(NSError **)error
+{
+ self = [super initWithXMLNode:node sourceURL:sourceURL error:error];
+
+ if (self != nil)
+ {
+ for (CXMLNode *child in [node children])
+ {
+ if ([[child name] isEqualToString:@"LatLonBox"])
+ {
+ CXMLNode *northNode = nil;
+ CXMLNode *southNode = nil;
+ CXMLNode *eastNode = nil;
+ CXMLNode *westNode = nil;
+ CXMLNode *rotationNode = nil;
+
+ for (CXMLNode *grandchild in [child children])
+ {
+ if ([grandchild kind] == CXMLElementKind)
+ {
+ if ([[grandchild name] isEqualToString:@"north"])
+ northNode = grandchild;
+
+ else if ([[grandchild name] isEqualToString:@"south"])
+ southNode = grandchild;
+
+ else if ([[grandchild name] isEqualToString:@"east"])
+ eastNode = grandchild;
+
+ else if ([[grandchild name] isEqualToString:@"west"])
+ westNode = grandchild;
+
+ else if ([[grandchild name] isEqualToString:@"rotation"])
+ rotationNode = grandchild;
+ }
+ }
+
+ if ( ! (northNode && southNode && eastNode && westNode))
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (north, south, east, and west are required for GroundOverlay LatLonBox)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+
+ north = [[northNode stringValue] doubleValue];
+ south = [[southNode stringValue] doubleValue];
+ east = [[eastNode stringValue] doubleValue];
+ west = [[westNode stringValue] doubleValue];
+
+ rotation = (rotationNode ? [[rotationNode stringValue] floatValue] : 0.0);
+
+ if (north < -90 || north > 90 ||
+ south < -90 || south > 90 ||
+ east < -180 || east > 180 ||
+ west < -180 || west > 180)
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (out-of-range north, south, east, or west specified for GroundOverlay LatLonBox)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+
+ if (rotation < -180 || rotation > 180)
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (out-of-range rotation specified for GroundOverlay LatLonBox)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+ }
+ }
+ }
+
+ return self;
+}
+
+@end
View
@@ -0,0 +1,50 @@
+//
+// SimpleKMLOverlay.h
+//
+// Created by Justin R. Miller on 7/22/11
+// Copyright 2011, Development Seed, Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// * Neither the names of Code Sorcery Workshop, LLC or Development Seed,
+// Inc., nor the names of its contributors may be used to endorse or
+// promote products derived from this software without specific prior
+// written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// http://code.google.com/apis/kml/documentation/kmlreference.html#overlay
+//
+
+#import "SimpleKMLFeature.h"
+
+@interface SimpleKMLOverlay : SimpleKMLFeature
+{
+ UIColor *color;
+ UIImage *icon;
+}
+
+@property (nonatomic, retain, readonly) UIColor *color;
+@property (nonatomic, retain, readonly) UIImage *icon;
+
+// abstract class
+
+@end
View
@@ -0,0 +1,151 @@
+//
+// SimpleKMLOverlay.m
+//
+// Created by Justin R. Miller on 7/22/11
+// Copyright 2011, Development Seed, Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// * Neither the names of Code Sorcery Workshop, LLC or Development Seed,
+// Inc., nor the names of its contributors may be used to endorse or
+// promote products derived from this software without specific prior
+// written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "SimpleKMLOverlay.h"
+
+@implementation SimpleKMLOverlay
+
+@synthesize color;
+@synthesize icon;
+
+- (id)initWithXMLNode:(CXMLNode *)node sourceURL:sourceURL error:(NSError **)error
+{
+ self = [super initWithXMLNode:node sourceURL:sourceURL error:error];
+
+ if (self != nil)
+ {
+ color = nil;
+ icon = nil;
+
+ for (CXMLNode *child in [node children])
+ {
+ if ([[child name] isEqualToString:@"color"])
+ {
+ NSString *colorString = [child stringValue];
+
+ color = [[SimpleKML colorForString:colorString] retain];
+ }
+ else if ([[child name] isEqualToString:@"Icon"])
+ {
+ CXMLNode *href = nil;
+
+ for (CXMLNode *grandchild in [child children])
+ {
+ if ([grandchild kind] == CXMLElementKind)
+ {
+ href = grandchild;
+ break;
+ }
+ }
+
+ if ( ! href)
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (no href specified for Overlay Icon)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+
+ NSData *data = nil;
+
+ if ([self cacheObjectForKey:[href stringValue]])
+ data = [self cacheObjectForKey:[href stringValue]];
+
+ else
+ {
+ NSURL *imageURL = [NSURL URLWithString:[href stringValue]];
+
+ if ( ! imageURL)
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (invalid Icon URL specified in Overlay)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+
+ if ([imageURL scheme])
+ data = [NSData dataWithContentsOfURL:imageURL];
+
+ else if ([[sourceURL relativePath] hasSuffix:@".kmz"])
+ data = [SimpleKML dataFromArchiveAtPath:[sourceURL relativePath] withFilePath:[imageURL relativePath]];
+
+ if (data)
+ [self setCacheObject:data forKey:[href stringValue]];
+
+ else
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (invalid Icon URL specified in Overlay)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+ }
+
+ icon = [[UIImage imageWithData:data] retain];
+
+ if ( ! icon)
+ {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Improperly formed KML (unable to retrieve Icon specified for Overlay)"
+ forKey:NSLocalizedFailureReasonErrorKey];
+
+ if (error)
+ *error = [NSError errorWithDomain:SimpleKMLErrorDomain code:SimpleKMLParseError userInfo:userInfo];
+
+ return nil;
+ }
+ }
+ }
+ }
+
+ return self;
+}
+
+- (void)dealloc
+{
+ [color release];
+ [icon release];
+
+ [super dealloc];
+}
+
+@end

0 comments on commit 2696510

Please sign in to comment.