title: Consume MySQL WKB geometry on iOS for MapKit
body: <p>I know how to convert MySQL geometry from binary to text and then transfer/convert it for use in MapKit. However, this seems unnecessarily inefficient. I'm looking for a way to transport the binary shapes (like polygons) to their Mapkit relatives (like MkPolygon) with bandwidth efficiency in mind. </p>
  
  <p>Is there a way of directly consuming these binary shapes and converting in iOS?
  Is the binary format sufficiently easy to parse?
  I'd like to avoid converting to text then back to binary until it's at least on the mobile device, so as to avoid the network bloat of transmitting millions of double precision floats depicted as character strings. </p>
  
  <p>I've found <a href="https://github.com/mweisman/ShapeKit" rel="nofollow">Michael Weismann's ShapeKit</a> on GitHub, which takes GEOS geometry and spits out MapKit objects.  However, ShapeKit relies on WKT as the starting point, unless you have a fully instantiated GEOS object.  Therefore, I hacked it up to build GEOS geometry from WKB bytes (by way of stuffing the MySQL WKB data into NSData), but it seems like the binary returned from MySQL isn't quite what it was expecting.  Perhaps there's an endian issue ... ?</p>
  
  <pre><code>@implementation ShapeKitGeometry
  @synthesize wktGeom,geomType, projDefinition ,geosGeom, numberOfCoords;
  
  - (id) init
  {
      self = [super init];
      if (self != nil) {
          // initialize GEOS library
          handle = initGEOS_r(notice, log_and_exit);
      }
      return self;
  }
  
  -(id)initWithWKB:(const unsigned char *) wkb size:(size_t)wkb_size {
      [self init];
      GEOSWKBReader *WKBReader = GEOSWKBReader_create_r(handle);
      self.geosGeom = GEOSWKBReader_read_r(handle, WKBReader, wkb, wkb_size);
      GEOSWKBReader_destroy_r(handle, WKBReader);
      self.geomType = [NSString stringWithUTF8String:GEOSGeomType_r(handle, geosGeom)];
      return self;
  }
  
  // .... later in my appDelegate:
  - (ShapeKitPolygon *)loadWKBPolygonFromFile:(NSString *)file {
      ShapeKitPolygon * poly = nil;
      NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"plist"];
      NSDictionary *stupidDict = [NSDictionary dictionaryWithContentsOfFile:path];
      NSData *geomData = [stupidDict objectForKey:@"shape"];
      if (geomData &amp;&amp; [geomData length]) {
          poly = [[[ShapeKitPolygon alloc] initWithWKB:[geomData bytes] size:[geomData length]] autorelease];
      }
      return poly;
  }
  </code></pre>
  
  <p>Thoughts?</p>