Code Monkey home page Code Monkey logo

h3kit's Introduction

Using Uber H3 cells with Swift/iOS

What is H3?

Uber maintains an open source library called H3 which they use in their mobile apps. H3 is based on the concept of dividing the surface of the planet into a grid of hexigons or 'cells'. Each call has a unique identifier and a fixed size and location. This type of grid system is much more powerful than a simple grid of latitude/longitude which produces a set of 'rectangles'. The H3 code library can be used in various ways:

  • Given a latitude/longitude point, find the index of the containing H3 cell
  • Given an H3 index, find the latitude/longitude cell center
  • Given an H3 index, determine the cell boundary in latitude/longitude coordinates
  • Find the coordinates of the 6 points for any cell
  • Find the distance from a point to the cell edge

Furthermore, H3 defines more than one grid size for the planet. In fact H3 defines 16 distince 'resolutions' ranging from 0.0000009 km^2 up to 4,250,546.8477000 km^2. There is a parent/child relationship between two cells at adjacent resolutions.

Read more about the H3 grid system here: https://h3geo.org

Installation via Cocoapods

Create a Podfile with the following specification:

pod 'H3', :git => '[email protected]:ehmjaysee/h3-ios.git'

Usage with Swift

import H3

func testH3() {
    let latitude = 40.1234
    let longitude = 98.5432
    let point = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let index = point.h3CellIndex(resolution: resolution)
    let hex = String(index, radix: 16, uppercase: true)
    print(hex)
    
    let neighbors = point.h3Neighbors(resolution: 6, ringLevel: 1)
    for item in neighbors {  
        print(String(item, radix: 16, uppercase: true)) 
    }
}

Usage with C interface

The C librarary has a rich set of APIs documented here: https://h3geo.org/docs/api/indexing You can access the entire API from your Swift based project. Here are some of the main functions:

H3Index geoToH3(const GeoCoord *g, int res);
void h3ToGeo(H3Index h3, GeoCoord *g);
void h3ToGeoBoundary(H3Index h3, GeoBoundary *gp);
void kRing(H3Index origin, int k, H3Index* out);
int maxKringSize(int k);
h3Line

Example showing how to take a set of 2D coordinates and generate the set of cells sourrounding that position.

// Determine the set of nearby H3 cells based on location
let latitude = degsToRads( 41.3343 )   // MUST convert to radians
let longitude = degsToRads( 101.1188 )
var location = GeoCoord(lat: latitude, lon: longitude)
let index = geoToH3(&location, 6)
let count = Int(maxKringSize(1))
var neighbors = Array(repeating: H3Index(), count: count)
kRing(index, 1, &neighbors);        

h3kit's People

Contributors

ehmjaysee avatar long1eu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.