Created
June 15, 2012 01:58
-
-
Save springmeyer/2934223 to your computer and use it in GitHub Desktop.
mapnik on ios (objc++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// mrender | |
// | |
// Created by Dane Springmeyer on 6/13/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import "ViewController.h" | |
#import <iostream> | |
#import <string> | |
// https://svn.boost.org/trac/boost/ticket/5010 | |
#undef nil | |
#undef Nil | |
#import <mapnik/graphics.hpp> | |
#import <mapnik/color.hpp> | |
#import <mapnik/image_util.hpp> | |
#include <mapnik/agg_renderer.hpp> | |
#include <mapnik/load_map.hpp> | |
#include <mapnik/datasource_cache.hpp> | |
#define nil __DARWIN_NULL | |
#define Nil __DARWIN_NULL | |
@implementation ViewController | |
#pragma mark - View lifecycle | |
- (void)updateMethod:(NSTimer *)theTimer | |
{ | |
try { | |
// set up renderable image and map canvas | |
mapnik::image_32 im(500,400); | |
mapnik::Map m(im.width(),im.height()); | |
NSString *style_path = [[NSBundle mainBundle] | |
pathForResource:@"style" | |
ofType:@"xml"]; | |
mapnik::load_map(m,[style_path UTF8String]); | |
unsigned red = static_cast<unsigned>(rand() % 256); | |
unsigned green = static_cast<unsigned>(rand() % 256); | |
unsigned blue = static_cast<unsigned>(rand() % 256); | |
float alpha = static_cast<float>(rand() % 2); | |
std::string style = [[NSString stringWithFormat:@"<Map><Style name='style'><Rule><PolygonSymbolizer gamma='.5' fill='rgba(%i,%i,%i,%f)' /></Rule></Style></Map>",red,green,blue,alpha] UTF8String]; | |
mapnik::load_map_string(m, style); | |
m.zoom_all(); | |
mapnik::agg_renderer<mapnik::image_32> ren(m,im); | |
ren.apply(); | |
// convert mapnik image to UIImage - there must be a better way... | |
// https://github.com/PaulSolt/UIImage-Conversion/blob/master/ImageHelper.m | |
size_t im_size = im.width() * im.height() * 4; | |
size_t bitsPerComponent = 8; | |
size_t bitsPerPixel = 32; | |
size_t bytesPerRow = 4 * im.width(); | |
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, im.raw_data(), im_size, NULL); | |
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); | |
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast; | |
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; | |
CGImageRef iref = CGImageCreate(im.width(), im.height(), | |
bitsPerComponent, bitsPerPixel, bytesPerRow, | |
colorSpaceRef, bitmapInfo, provider, | |
NULL, YES, renderingIntent); | |
CGContextRef context = CGBitmapContextCreate(im.raw_data(), im.width(), im.height(), | |
bitsPerComponent, bytesPerRow, colorSpaceRef, bitmapInfo); | |
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, im.width(), im.height()), iref); | |
CGImageRef imageRef = CGBitmapContextCreateImage(context); | |
UIImage *image = nil; | |
if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) { | |
float scale = [[UIScreen mainScreen] scale]; | |
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp]; | |
} else { | |
image = [UIImage imageWithCGImage:imageRef]; | |
} | |
// cleanup | |
CGImageRelease(imageRef); | |
CGContextRelease(context); | |
CGColorSpaceRelease(colorSpaceRef); | |
CGImageRelease(iref); | |
CGDataProviderRelease(provider); | |
NSLog(@"resultImg width:%f, height:%f",image.size.width,image.size.height); | |
// push UIImage into view | |
UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; | |
[self.view addSubview: imageView]; | |
} | |
catch (std::exception const& ex) | |
{ | |
NSLog(@"error: %s",ex.what()); | |
} | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[NSTimer scheduledTimerWithTimeInterval:0.01f | |
target:self | |
selector:@selector(updateMethod:) | |
userInfo:nil | |
repeats:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I see you are working on mapnik on iOS, is there any project I can check ? Thanks .. (Sorry if the question looks odd but I'm new of this Git/Gist)