Skip to content

Instantly share code, notes, and snippets.

@dim
Created May 20, 2019 12:46
Show Gist options
  • Save dim/314e7e18e439264175ef4bf716979b22 to your computer and use it in GitHub Desktop.
Save dim/314e7e18e439264175ef4bf716979b22 to your computer and use it in GitHub Desktop.
S2 Geometry Cell Sizes

S2 Geometry Cell Sizes - http://s2geometry.io/

Script

package main

import (
	"flag"
	"fmt"
	"strconv"

	"github.com/golang/geo/s2"
)

const earthRadius = 6378100.0

func main() {
	lat, _ := strconv.ParseFloat(flag.Arg(0), 64)
	lng, _ := strconv.ParseFloat(flag.Arg(1), 64)

	i0 := s2.CellIDFromLatLng(s2.LatLngFromDegrees(lat, lng))
	for i := 0; i <= 30; i++ {
		c := s2.CellFromCellID(i0.Parent(i))
		w := c.Vertex(0).Distance(c.Vertex(1)).Radians() * earthRadius * 1000
		h := c.Vertex(0).Distance(c.Vertex(3)).Radians() * earthRadius * 1000
		fmt.Printf("level=%02d width=%s height=%s\n", c.Level(), formatDist(w), formatDist(h))
	}
}

func formatDist(n float64) string {
	if n > 10000000 {
		return fmt.Sprintf("%.0fkm", n/1000000)
	} else if n > 700000 {
		return fmt.Sprintf("%.1fkm", n/1000000)
	} else if n > 6000 {
		return fmt.Sprintf("%.0fm", n/1000)
	} else if n > 700 {
		return fmt.Sprintf("%.1fm", n/1000)
	} else if n > 100 {
		return fmt.Sprintf("%.0fcm", n/10)
	}
	return fmt.Sprintf("%.1fcm", n/10)
}

Equator (latitude=0, longitude=30)

level=00 width=7851km height=7851km
level=01 width=5009km height=5009km
level=02 width=2491km height=2342km
level=03 width=1323km height=1093km
level=04 width=650km height=494km
level=05 width=329km height=240km
level=06 width=165km height=118km
level=07 width=82km height=58km
level=08 width=41km height=29km
level=09 width=21km height=14km
level=10 width=10km height=7.2km
level=11 width=5.1km height=3.6km
level=12 width=2.6km height=1.8km
level=13 width=1.3km height=0.9km
level=14 width=643m height=450m
level=15 width=322m height=225m
level=16 width=161m height=112m
level=17 width=80m height=56m
level=18 width=40m height=28m
level=19 width=20m height=14m
level=20 width=10m height=7m
level=21 width=5.0m height=3.5m
level=22 width=2.5m height=1.8m
level=23 width=1.3m height=0.9m
level=24 width=63cm height=44cm
level=25 width=31cm height=22cm
level=26 width=16cm height=11cm
level=27 width=7.9cm height=5.5cm
level=28 width=3.9cm height=2.7cm
level=29 width=2.0cm height=1.4cm
level=30 width=1.0cm height=0.7cm

UK (latitude=51.5, longitude=0)

level=00 width=7851km height=7851km
level=01 width=5009km height=5009km
level=02 width=2518km height=2518km
level=03 width=1182km height=1182km
level=04 width=563km height=563km
level=05 width=274km height=274km
level=06 width=135km height=135km
level=07 width=67km height=67km
level=08 width=33km height=33km
level=09 width=17km height=17km
level=10 width=8.3km height=8.3km
level=11 width=4.2km height=4.2km
level=12 width=2.1km height=2.1km
level=13 width=1.0km height=1.0km
level=14 width=519m height=519m
level=15 width=260m height=260m
level=16 width=130m height=130m
level=17 width=65m height=65m
level=18 width=32m height=32m
level=19 width=16m height=16m
level=20 width=8m height=8m
level=21 width=4.1m height=4.1m
level=22 width=2.0m height=2.0m
level=23 width=1.0m height=1.0m
level=24 width=51cm height=51cm
level=25 width=25cm height=25cm
level=26 width=13cm height=13cm
level=27 width=6.3cm height=6.3cm
level=28 width=3.2cm height=3.2cm
level=29 width=1.6cm height=1.6cm
level=30 width=0.8cm height=0.8cm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment