Skip to content

Commit

Permalink
Add option to strip trailing zeros from cell IDs
Browse files Browse the repository at this point in the history
Add option to strip trailing zeros from cell IDs in location_to_cellid,
add tests for location_to_cellid and location_to_token, fix
polyline_encoder test.
  • Loading branch information
Noctem committed May 31, 2017
1 parent 4f11e6c commit 09b9774
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
31 changes: 27 additions & 4 deletions include/bitscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@

#if __GNUC__ >= 4 || HAS_BUILTIN(__builtin_clzll)
#define leadingZeros(x) __builtin_clzll(x)
#define trailingZeros(x) __builtin_ctzll(x)
#endif

#ifndef leadingZeros
#ifdef _MSC_VER
#include <intrin.h>
#ifdef __LZCNT__ && !defined(DEPLOYMENT)
#define leadingZeros(x) __lzcnt64(x)
#else
inline unsigned long leadingZeros(unsigned __int64 x) {
unsigned long result;
#ifdef _WIN64
Expand All @@ -31,13 +29,38 @@ inline unsigned long leadingZeros(unsigned __int64 x) {
#endif // _WIN64
return 63 - result;
}
#endif // __LZCNT__

inline unsigned long trailingZeros(unsigned __int64 x) {
unsigned long result;
#ifdef _WIN64
_BitScanForward64(&result, x);
return result;
#else
// Scan the Low Word.
if (_BitScanForward(&result, static_cast<unsigned long>(x))) return result;

// Scan the High Word.
_BitScanForward(&result, static_cast<unsigned long>(x >> 32));
return result + 32;
#endif // _WIN64
}
#else
inline unsigned long leadingZeros(unsigned long long x) {
unsigned long r = 0;
while (x >>= 1) r++;
return 64 - r;
}

inline unsigned long trailingZeros(unsigned long long x) {
unsigned long r;
for (r = 0; x != 0; x >>= 1) {
if (x & 01)
break;
else
r++;
}
return r;
}
#endif // _MSC_VER
#endif // leadingZeros

Expand Down
2 changes: 1 addition & 1 deletion pogeo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = 'pogeo'
__version__ = '0.4.0'
__version__ = '0.4.0b0'
__author__ = 'David Christenson'
__license__ = 'Apache License'
__copyright__ = 'Copyright (c) 2017 David Christenson <https://github.com/Noctem>'
Expand Down
1 change: 1 addition & 0 deletions pogeo/_bitscan.pxd
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cdef extern from "bitscan.h" nogil:
inline unsigned long leadingZeros(unsigned long long x)
inline unsigned long trailingZeros(unsigned long long x)
7 changes: 4 additions & 3 deletions pogeo/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from libcpp.string cimport string
from libcpp.vector cimport vector

from ._array cimport array, clone
from ._bitscan cimport leadingZeros
from ._bitscan cimport leadingZeros, trailingZeros
from ._cpython cimport _PyTime_GetSystemClock, _PyTime_GetMonotonicClock
from .const cimport AXIS_HEIGHT, DEG_TO_RAD, EARTH_RADIUS_KILOMETERS, EARTH_RADIUS_METERS, EARTH_RADIUS_MILES, RAD_TO_DEG
from .location cimport Location
Expand Down Expand Up @@ -122,8 +122,9 @@ def token_to_coords(unicode t):
return s2point_to_lat(p), s2point_to_lon(p)


def location_to_cellid(Location p, int level=S2_LEVEL):
return S2CellId.FromPoint(p.point).parent(level).id()
def location_to_cellid(Location p, int level=S2_LEVEL, strip_trailing=True):
cdef uint64_t cellid = S2CellId.FromPoint(p.point).parent(level).id()
return cellid >> trailingZeros(cellid) if strip_trailing else cellid


def location_to_token(Location p, int level=S2_LEVEL):
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
if platform == 'win32':
libraries = ['pthreadVC2']
macros.append(('PTW32_STATIC_LIB', None))
if 'APPVEYOR' in environ:
macros.append(('DEPLOYMENT', None))
c_args = cpp_args = None
elif platform == 'darwin':
c_args = ['-O3']
Expand Down Expand Up @@ -232,7 +230,7 @@
exts = cythonize(exts)

setup(name='pogeo',
version='0.4.0',
version='0.4.0b0',
description='Fast geography package.',
long_description='A fast C++ extension for calculating cell IDs and distances.',
url="https://github.com/Noctem/pogeo",
Expand Down
16 changes: 14 additions & 2 deletions test/test_pogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pogeo import CellCache, Location, Loop, Polygon, Rectangle
from pogeo.altitude import AltitudeCache
from pogeo.geocoder import geocode
from pogeo.polyline import encode_single, encode_multiple
from pogeo.polyline_encoder import encode_single, encode_multiple
from pogeo.utils import *


Expand Down Expand Up @@ -142,7 +142,7 @@ def test_json(self):
self.assertEqual(polygon.json, expected)


class TestPolyline(TestCase):
class TestPolylineEncoder(TestCase):
def test_single(self):
self.assertEqual(encode_single(Location(40.761731, -111.901111)), 'ygxwF|t~iT')

Expand Down Expand Up @@ -216,6 +216,18 @@ def test_token_to_location(self):
self.assertAlmostEqual(lat, 40.78931, places=5)
self.assertAlmostEqual(lon, -111.93888, places=5)

def test_location_to_cellid(self):
loc = Location(40.2637, -111.639794)
raw = location_to_cellid(loc, 20, False)
stripped = location_to_cellid(loc, 20, True)
self.assertEqual(raw, 9749608109427392512)
self.assertEqual(stripped, 9297950848987)

def test_location_to_token(self):
loc = Location(40.2637, -111.639794)
token = location_to_token(loc, 20)
self.assertEqual(token, '874d90eb7db')

def test_get_bearing(self):
loc1 = Location(40.239416, -111.643654)
loc2 = Location(40.248302, -111.620660)
Expand Down
4 changes: 2 additions & 2 deletions travis/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
set -e

macbuild() {
pip3 install -U setuptools wheel cython twine
pip3 install -U setuptools wheel cython twine cyrandom
rm -rf dist build
if [[ "$1" = "sdist" && "$SOURCE" = TRUE ]]; then
python3 setup.py sdist bdist_wheel
python3 setup.py install
python3 test_pogeo.py
python3 test/test_pogeo.py
twine upload --skip-existing dist/*.whl dist/*.tar.*
else
python3 setup.py bdist_wheel
Expand Down

0 comments on commit 09b9774

Please sign in to comment.