Skip to content

Instantly share code, notes, and snippets.

@Nub
Created November 4, 2011 21:56
Show Gist options
  • Select an option

  • Save Nub/1340593 to your computer and use it in GitHub Desktop.

Select an option

Save Nub/1340593 to your computer and use it in GitHub Desktop.
//
// main.m
// CountryPolygonGenerator
//
// Created by Zachry Thayer on 11/4/11.
// Copyright (c) 2011 Zachry Thayer. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "JSONKit.h"
#import "gpc.h"
#define kDefaultGeolocationDataFile @"geolocationData.json"
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSData *geolocationData = [NSData dataWithContentsOfFile:kDefaultGeolocationDataFile];
NSDictionary *geolocationDictionary = [geolocationData objectFromJSONData];
geolocationData = nil;//Free from mem
NSDictionary *geolocationCountries = [geolocationDictionary objectForKey:@"Countries"];
BOOL first = NO;
for(NSString *countryName in geolocationCountries){
NSDictionary *country = [geolocationCountries objectForKey:countryName];
gpc_polygon *countryPolygon;
NSLog(@"Country:%@", country);
NSDictionary *countryRegions = [country objectForKey:@"Regions"];
for (NSString *regionName in countryRegions) {
NSDictionary *region = [countryRegions objectForKey:regionName];
NSArray *polygonVertices = [region objectForKey:@"Polygon"];
gpc_vertex_list *regionVertices = (gpc_vertex_list*)malloc(sizeof(gpc_vertex_list));
regionVertices->num_vertices = [polygonVertices count];
regionVertices->vertex = (gpc_vertex*)malloc(sizeof(gpc_vertex) * regionVertices->num_vertices);
int i = 0;
for (NSDictionary *vertex in polygonVertices) {// Generate vertex list
regionVertices->vertex[i].x = [[vertex objectForKey:@"lat"] doubleValue];
regionVertices->vertex[i].y = [[vertex objectForKey:@"lon"] doubleValue];
i++;
}
gpc_polygon *regionPolygon = (gpc_polygon*)malloc(sizeof(gpc_polygon));
regionPolygon->num_contours = 1;
regionPolygon->hole = 0;
regionPolygon->contour = regionVertices;
if (first) {
countryPolygon = regionPolygon;
}else{
gpc_polygon_clip(GPC_UNION, countryPolygon, regionPolygon, countryPolygon);
}
gpc_free_polygon(regionPolygon);
}
for (int i = 0; i < countryPolygon->num_contours; i++) {
for (int j = 0; j < countryPolygon->contour[i].num_vertices; j++) {
//Add point to dictionary
}
}
gpc_free_polygon(countryPolygon);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment